bsg.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. /*
  2. * bsg.c - block layer implementation of the sg v3 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. /*
  13. * TODO
  14. * - Should this get merged, block/scsi_ioctl.c will be migrated into
  15. * this file. To keep maintenance down, it's easier to have them
  16. * seperated right now.
  17. *
  18. */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/file.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/poll.h>
  24. #include <linux/cdev.h>
  25. #include <linux/percpu.h>
  26. #include <linux/uio.h>
  27. #include <linux/bsg.h>
  28. #include <scsi/scsi.h>
  29. #include <scsi/scsi_ioctl.h>
  30. #include <scsi/scsi_cmnd.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_driver.h>
  33. #include <scsi/sg.h>
  34. const static char bsg_version[] = "block layer sg (bsg) 0.4";
  35. struct bsg_device {
  36. request_queue_t *queue;
  37. spinlock_t lock;
  38. struct list_head busy_list;
  39. struct list_head done_list;
  40. struct hlist_node dev_list;
  41. atomic_t ref_count;
  42. int minor;
  43. int queued_cmds;
  44. int done_cmds;
  45. wait_queue_head_t wq_done;
  46. wait_queue_head_t wq_free;
  47. char name[BUS_ID_SIZE];
  48. int max_queue;
  49. unsigned long flags;
  50. };
  51. enum {
  52. BSG_F_BLOCK = 1,
  53. BSG_F_WRITE_PERM = 2,
  54. };
  55. #define BSG_DEFAULT_CMDS 64
  56. #define BSG_MAX_DEVS 32768
  57. #undef BSG_DEBUG
  58. #ifdef BSG_DEBUG
  59. #define dprintk(fmt, args...) printk(KERN_ERR "%s: " fmt, __FUNCTION__, ##args)
  60. #else
  61. #define dprintk(fmt, args...)
  62. #endif
  63. /*
  64. * just for testing
  65. */
  66. #define BSG_MAJOR (240)
  67. static DEFINE_MUTEX(bsg_mutex);
  68. static int bsg_device_nr, bsg_minor_idx;
  69. #define BSG_LIST_ARRAY_SIZE 8
  70. #define bsg_list_idx(minor) ((minor) & (BSG_LIST_ARRAY_SIZE - 1))
  71. static struct hlist_head bsg_device_list[BSG_LIST_ARRAY_SIZE];
  72. static struct class *bsg_class;
  73. static LIST_HEAD(bsg_class_list);
  74. static struct kmem_cache *bsg_cmd_cachep;
  75. /*
  76. * our internal command type
  77. */
  78. struct bsg_command {
  79. struct bsg_device *bd;
  80. struct list_head list;
  81. struct request *rq;
  82. struct bio *bio;
  83. struct bio *bidi_bio;
  84. int err;
  85. struct sg_io_v4 hdr;
  86. struct sg_io_v4 __user *uhdr;
  87. char sense[SCSI_SENSE_BUFFERSIZE];
  88. };
  89. static void bsg_free_command(struct bsg_command *bc)
  90. {
  91. struct bsg_device *bd = bc->bd;
  92. unsigned long flags;
  93. kmem_cache_free(bsg_cmd_cachep, bc);
  94. spin_lock_irqsave(&bd->lock, flags);
  95. bd->queued_cmds--;
  96. spin_unlock_irqrestore(&bd->lock, flags);
  97. wake_up(&bd->wq_free);
  98. }
  99. static struct bsg_command *bsg_alloc_command(struct bsg_device *bd)
  100. {
  101. struct bsg_command *bc = ERR_PTR(-EINVAL);
  102. spin_lock_irq(&bd->lock);
  103. if (bd->queued_cmds >= bd->max_queue)
  104. goto out;
  105. bd->queued_cmds++;
  106. spin_unlock_irq(&bd->lock);
  107. bc = kmem_cache_zalloc(bsg_cmd_cachep, GFP_KERNEL);
  108. if (unlikely(!bc)) {
  109. spin_lock_irq(&bd->lock);
  110. bd->queued_cmds--;
  111. bc = ERR_PTR(-ENOMEM);
  112. goto out;
  113. }
  114. bc->bd = bd;
  115. INIT_LIST_HEAD(&bc->list);
  116. dprintk("%s: returning free cmd %p\n", bd->name, bc);
  117. return bc;
  118. out:
  119. spin_unlock_irq(&bd->lock);
  120. return bc;
  121. }
  122. static inline void
  123. bsg_add_done_cmd(struct bsg_device *bd, struct bsg_command *bc)
  124. {
  125. }
  126. static int bsg_io_schedule(struct bsg_device *bd)
  127. {
  128. DEFINE_WAIT(wait);
  129. int ret = 0;
  130. spin_lock_irq(&bd->lock);
  131. BUG_ON(bd->done_cmds > bd->queued_cmds);
  132. /*
  133. * -ENOSPC or -ENODATA? I'm going for -ENODATA, meaning "I have no
  134. * work to do", even though we return -ENOSPC after this same test
  135. * during bsg_write() -- there, it means our buffer can't have more
  136. * bsg_commands added to it, thus has no space left.
  137. */
  138. if (bd->done_cmds == bd->queued_cmds) {
  139. ret = -ENODATA;
  140. goto unlock;
  141. }
  142. if (!test_bit(BSG_F_BLOCK, &bd->flags)) {
  143. ret = -EAGAIN;
  144. goto unlock;
  145. }
  146. prepare_to_wait(&bd->wq_done, &wait, TASK_UNINTERRUPTIBLE);
  147. spin_unlock_irq(&bd->lock);
  148. io_schedule();
  149. finish_wait(&bd->wq_done, &wait);
  150. return ret;
  151. unlock:
  152. spin_unlock_irq(&bd->lock);
  153. return ret;
  154. }
  155. static int blk_fill_sgv4_hdr_rq(request_queue_t *q, struct request *rq,
  156. struct sg_io_v4 *hdr, int has_write_perm)
  157. {
  158. memset(rq->cmd, 0, BLK_MAX_CDB); /* ATAPI hates garbage after CDB */
  159. if (copy_from_user(rq->cmd, (void *)(unsigned long)hdr->request,
  160. hdr->request_len))
  161. return -EFAULT;
  162. if (hdr->subprotocol == BSG_SUB_PROTOCOL_SCSI_CMD) {
  163. if (blk_verify_command(rq->cmd, has_write_perm))
  164. return -EPERM;
  165. } else if (!capable(CAP_SYS_RAWIO))
  166. return -EPERM;
  167. /*
  168. * fill in request structure
  169. */
  170. rq->cmd_len = hdr->request_len;
  171. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  172. rq->timeout = (hdr->timeout * HZ) / 1000;
  173. if (!rq->timeout)
  174. rq->timeout = q->sg_timeout;
  175. if (!rq->timeout)
  176. rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
  177. return 0;
  178. }
  179. /*
  180. * Check if sg_io_v4 from user is allowed and valid
  181. */
  182. static int
  183. bsg_validate_sgv4_hdr(request_queue_t *q, struct sg_io_v4 *hdr, int *rw)
  184. {
  185. int ret = 0;
  186. if (hdr->guard != 'Q')
  187. return -EINVAL;
  188. if (hdr->request_len > BLK_MAX_CDB)
  189. return -EINVAL;
  190. if (hdr->dout_xfer_len > (q->max_sectors << 9) ||
  191. hdr->din_xfer_len > (q->max_sectors << 9))
  192. return -EIO;
  193. switch (hdr->protocol) {
  194. case BSG_PROTOCOL_SCSI:
  195. switch (hdr->subprotocol) {
  196. case BSG_SUB_PROTOCOL_SCSI_CMD:
  197. case BSG_SUB_PROTOCOL_SCSI_TRANSPORT:
  198. break;
  199. default:
  200. ret = -EINVAL;
  201. }
  202. break;
  203. default:
  204. ret = -EINVAL;
  205. }
  206. *rw = hdr->dout_xfer_len ? WRITE : READ;
  207. return ret;
  208. }
  209. /*
  210. * map sg_io_v4 to a request.
  211. */
  212. static struct request *
  213. bsg_map_hdr(struct bsg_device *bd, struct sg_io_v4 *hdr)
  214. {
  215. request_queue_t *q = bd->queue;
  216. struct request *rq, *next_rq = NULL;
  217. int ret, rw;
  218. unsigned int dxfer_len;
  219. void *dxferp = NULL;
  220. dprintk("map hdr %llx/%u %llx/%u\n", (unsigned long long) hdr->dout_xferp,
  221. hdr->dout_xfer_len, (unsigned long long) hdr->din_xferp,
  222. hdr->din_xfer_len);
  223. ret = bsg_validate_sgv4_hdr(q, hdr, &rw);
  224. if (ret)
  225. return ERR_PTR(ret);
  226. /*
  227. * map scatter-gather elements seperately and string them to request
  228. */
  229. rq = blk_get_request(q, rw, GFP_KERNEL);
  230. if (!rq)
  231. return ERR_PTR(-ENOMEM);
  232. ret = blk_fill_sgv4_hdr_rq(q, rq, hdr, test_bit(BSG_F_WRITE_PERM,
  233. &bd->flags));
  234. if (ret)
  235. goto out;
  236. if (rw == WRITE && hdr->din_xfer_len) {
  237. if (!test_bit(QUEUE_FLAG_BIDI, &q->queue_flags)) {
  238. ret = -EOPNOTSUPP;
  239. goto out;
  240. }
  241. next_rq = blk_get_request(q, READ, GFP_KERNEL);
  242. if (!next_rq) {
  243. ret = -ENOMEM;
  244. goto out;
  245. }
  246. rq->next_rq = next_rq;
  247. dxferp = (void*)(unsigned long)hdr->din_xferp;
  248. ret = blk_rq_map_user(q, next_rq, dxferp, hdr->din_xfer_len);
  249. if (ret)
  250. goto out;
  251. }
  252. if (hdr->dout_xfer_len) {
  253. dxfer_len = hdr->dout_xfer_len;
  254. dxferp = (void*)(unsigned long)hdr->dout_xferp;
  255. } else if (hdr->din_xfer_len) {
  256. dxfer_len = hdr->din_xfer_len;
  257. dxferp = (void*)(unsigned long)hdr->din_xferp;
  258. } else
  259. dxfer_len = 0;
  260. if (dxfer_len) {
  261. ret = blk_rq_map_user(q, rq, dxferp, dxfer_len);
  262. if (ret)
  263. goto out;
  264. }
  265. return rq;
  266. out:
  267. blk_put_request(rq);
  268. if (next_rq) {
  269. blk_rq_unmap_user(next_rq->bio);
  270. blk_put_request(next_rq);
  271. }
  272. return ERR_PTR(ret);
  273. }
  274. /*
  275. * async completion call-back from the block layer, when scsi/ide/whatever
  276. * calls end_that_request_last() on a request
  277. */
  278. static void bsg_rq_end_io(struct request *rq, int uptodate)
  279. {
  280. struct bsg_command *bc = rq->end_io_data;
  281. struct bsg_device *bd = bc->bd;
  282. unsigned long flags;
  283. dprintk("%s: finished rq %p bc %p, bio %p stat %d\n",
  284. bd->name, rq, bc, bc->bio, uptodate);
  285. bc->hdr.duration = jiffies_to_msecs(jiffies - bc->hdr.duration);
  286. spin_lock_irqsave(&bd->lock, flags);
  287. list_move_tail(&bc->list, &bd->done_list);
  288. bd->done_cmds++;
  289. spin_unlock_irqrestore(&bd->lock, flags);
  290. wake_up(&bd->wq_done);
  291. }
  292. /*
  293. * do final setup of a 'bc' and submit the matching 'rq' to the block
  294. * layer for io
  295. */
  296. static void bsg_add_command(struct bsg_device *bd, request_queue_t *q,
  297. struct bsg_command *bc, struct request *rq)
  298. {
  299. rq->sense = bc->sense;
  300. rq->sense_len = 0;
  301. /*
  302. * add bc command to busy queue and submit rq for io
  303. */
  304. bc->rq = rq;
  305. bc->bio = rq->bio;
  306. if (rq->next_rq)
  307. bc->bidi_bio = rq->next_rq->bio;
  308. bc->hdr.duration = jiffies;
  309. spin_lock_irq(&bd->lock);
  310. list_add_tail(&bc->list, &bd->busy_list);
  311. spin_unlock_irq(&bd->lock);
  312. dprintk("%s: queueing rq %p, bc %p\n", bd->name, rq, bc);
  313. rq->end_io_data = bc;
  314. blk_execute_rq_nowait(q, NULL, rq, 1, bsg_rq_end_io);
  315. }
  316. static struct bsg_command *bsg_next_done_cmd(struct bsg_device *bd)
  317. {
  318. struct bsg_command *bc = NULL;
  319. spin_lock_irq(&bd->lock);
  320. if (bd->done_cmds) {
  321. bc = list_entry(bd->done_list.next, struct bsg_command, list);
  322. list_del(&bc->list);
  323. bd->done_cmds--;
  324. }
  325. spin_unlock_irq(&bd->lock);
  326. return bc;
  327. }
  328. /*
  329. * Get a finished command from the done list
  330. */
  331. static struct bsg_command *bsg_get_done_cmd(struct bsg_device *bd)
  332. {
  333. struct bsg_command *bc;
  334. int ret;
  335. do {
  336. bc = bsg_next_done_cmd(bd);
  337. if (bc)
  338. break;
  339. if (!test_bit(BSG_F_BLOCK, &bd->flags)) {
  340. bc = ERR_PTR(-EAGAIN);
  341. break;
  342. }
  343. ret = wait_event_interruptible(bd->wq_done, bd->done_cmds);
  344. if (ret) {
  345. bc = ERR_PTR(-ERESTARTSYS);
  346. break;
  347. }
  348. } while (1);
  349. dprintk("%s: returning done %p\n", bd->name, bc);
  350. return bc;
  351. }
  352. static int blk_complete_sgv4_hdr_rq(struct request *rq, struct sg_io_v4 *hdr,
  353. struct bio *bio, struct bio *bidi_bio)
  354. {
  355. int ret = 0;
  356. dprintk("rq %p bio %p %u\n", rq, bio, rq->errors);
  357. /*
  358. * fill in all the output members
  359. */
  360. hdr->device_status = status_byte(rq->errors);
  361. hdr->transport_status = host_byte(rq->errors);
  362. hdr->driver_status = driver_byte(rq->errors);
  363. hdr->info = 0;
  364. if (hdr->device_status || hdr->transport_status || hdr->driver_status)
  365. hdr->info |= SG_INFO_CHECK;
  366. hdr->din_resid = rq->data_len;
  367. hdr->response_len = 0;
  368. if (rq->sense_len && hdr->response) {
  369. int len = min_t(unsigned int, hdr->max_response_len,
  370. rq->sense_len);
  371. ret = copy_to_user((void*)(unsigned long)hdr->response,
  372. rq->sense, len);
  373. if (!ret)
  374. hdr->response_len = len;
  375. else
  376. ret = -EFAULT;
  377. }
  378. if (rq->next_rq) {
  379. blk_rq_unmap_user(bidi_bio);
  380. blk_put_request(rq->next_rq);
  381. }
  382. blk_rq_unmap_user(bio);
  383. blk_put_request(rq);
  384. return ret;
  385. }
  386. static int bsg_complete_all_commands(struct bsg_device *bd)
  387. {
  388. struct bsg_command *bc;
  389. int ret, tret;
  390. dprintk("%s: entered\n", bd->name);
  391. set_bit(BSG_F_BLOCK, &bd->flags);
  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. request_queue_t *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. bc->uhdr = (struct sg_io_v4 __user *) buf;
  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,
  622. &bsg_device_list[bd->minor & (BSG_LIST_ARRAY_SIZE - 1)]);
  623. strncpy(bd->name, rq->bsg_dev.class_dev->class_id, sizeof(bd->name) - 1);
  624. dprintk("bound to <%s>, max queue %d\n",
  625. format_dev_t(buf, inode->i_rdev), bd->max_queue);
  626. mutex_unlock(&bsg_mutex);
  627. return bd;
  628. }
  629. static struct bsg_device *__bsg_get_device(int minor)
  630. {
  631. struct hlist_head *list;
  632. struct bsg_device *bd = NULL;
  633. struct hlist_node *entry;
  634. mutex_lock(&bsg_mutex);
  635. list = &bsg_device_list[minor & (BSG_LIST_ARRAY_SIZE - 1)];
  636. hlist_for_each(entry, list) {
  637. bd = hlist_entry(entry, struct bsg_device, dev_list);
  638. if (bd->minor == minor) {
  639. atomic_inc(&bd->ref_count);
  640. break;
  641. }
  642. bd = NULL;
  643. }
  644. mutex_unlock(&bsg_mutex);
  645. return bd;
  646. }
  647. static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file)
  648. {
  649. struct bsg_device *bd = __bsg_get_device(iminor(inode));
  650. struct bsg_class_device *bcd, *__bcd;
  651. if (bd)
  652. return bd;
  653. /*
  654. * find the class device
  655. */
  656. bcd = NULL;
  657. mutex_lock(&bsg_mutex);
  658. list_for_each_entry(__bcd, &bsg_class_list, list) {
  659. if (__bcd->minor == iminor(inode)) {
  660. bcd = __bcd;
  661. break;
  662. }
  663. }
  664. mutex_unlock(&bsg_mutex);
  665. if (!bcd)
  666. return ERR_PTR(-ENODEV);
  667. return bsg_add_device(inode, bcd->queue, file);
  668. }
  669. static int bsg_open(struct inode *inode, struct file *file)
  670. {
  671. struct bsg_device *bd = bsg_get_device(inode, file);
  672. if (IS_ERR(bd))
  673. return PTR_ERR(bd);
  674. file->private_data = bd;
  675. return 0;
  676. }
  677. static int bsg_release(struct inode *inode, struct file *file)
  678. {
  679. struct bsg_device *bd = file->private_data;
  680. file->private_data = NULL;
  681. return bsg_put_device(bd);
  682. }
  683. static unsigned int bsg_poll(struct file *file, poll_table *wait)
  684. {
  685. struct bsg_device *bd = file->private_data;
  686. unsigned int mask = 0;
  687. poll_wait(file, &bd->wq_done, wait);
  688. poll_wait(file, &bd->wq_free, wait);
  689. spin_lock_irq(&bd->lock);
  690. if (!list_empty(&bd->done_list))
  691. mask |= POLLIN | POLLRDNORM;
  692. if (bd->queued_cmds >= bd->max_queue)
  693. mask |= POLLOUT;
  694. spin_unlock_irq(&bd->lock);
  695. return mask;
  696. }
  697. static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  698. {
  699. struct bsg_device *bd = file->private_data;
  700. int __user *uarg = (int __user *) arg;
  701. switch (cmd) {
  702. /*
  703. * our own ioctls
  704. */
  705. case SG_GET_COMMAND_Q:
  706. return put_user(bd->max_queue, uarg);
  707. case SG_SET_COMMAND_Q: {
  708. int queue;
  709. if (get_user(queue, uarg))
  710. return -EFAULT;
  711. if (queue < 1)
  712. return -EINVAL;
  713. spin_lock_irq(&bd->lock);
  714. bd->max_queue = queue;
  715. spin_unlock_irq(&bd->lock);
  716. return 0;
  717. }
  718. /*
  719. * SCSI/sg ioctls
  720. */
  721. case SG_GET_VERSION_NUM:
  722. case SCSI_IOCTL_GET_IDLUN:
  723. case SCSI_IOCTL_GET_BUS_NUMBER:
  724. case SG_SET_TIMEOUT:
  725. case SG_GET_TIMEOUT:
  726. case SG_GET_RESERVED_SIZE:
  727. case SG_SET_RESERVED_SIZE:
  728. case SG_EMULATED_HOST:
  729. case SCSI_IOCTL_SEND_COMMAND: {
  730. void __user *uarg = (void __user *) arg;
  731. return scsi_cmd_ioctl(file, bd->queue, NULL, cmd, uarg);
  732. }
  733. case SG_IO: {
  734. struct request *rq;
  735. struct bio *bio, *bidi_bio = NULL;
  736. struct sg_io_v4 hdr;
  737. if (copy_from_user(&hdr, uarg, sizeof(hdr)))
  738. return -EFAULT;
  739. rq = bsg_map_hdr(bd, &hdr);
  740. if (IS_ERR(rq))
  741. return PTR_ERR(rq);
  742. bio = rq->bio;
  743. if (rq->next_rq)
  744. bidi_bio = rq->next_rq->bio;
  745. blk_execute_rq(bd->queue, NULL, rq, 0);
  746. blk_complete_sgv4_hdr_rq(rq, &hdr, bio, bidi_bio);
  747. if (copy_to_user(uarg, &hdr, sizeof(hdr)))
  748. return -EFAULT;
  749. return 0;
  750. }
  751. /*
  752. * block device ioctls
  753. */
  754. default:
  755. #if 0
  756. return ioctl_by_bdev(bd->bdev, cmd, arg);
  757. #else
  758. return -ENOTTY;
  759. #endif
  760. }
  761. }
  762. static struct file_operations bsg_fops = {
  763. .read = bsg_read,
  764. .write = bsg_write,
  765. .poll = bsg_poll,
  766. .open = bsg_open,
  767. .release = bsg_release,
  768. .unlocked_ioctl = bsg_ioctl,
  769. .owner = THIS_MODULE,
  770. };
  771. void bsg_unregister_queue(struct request_queue *q)
  772. {
  773. struct bsg_class_device *bcd = &q->bsg_dev;
  774. WARN_ON(!bcd->class_dev);
  775. mutex_lock(&bsg_mutex);
  776. sysfs_remove_link(&q->kobj, "bsg");
  777. class_device_destroy(bsg_class, MKDEV(BSG_MAJOR, bcd->minor));
  778. bcd->class_dev = NULL;
  779. list_del_init(&bcd->list);
  780. bsg_device_nr--;
  781. mutex_unlock(&bsg_mutex);
  782. }
  783. EXPORT_SYMBOL_GPL(bsg_unregister_queue);
  784. int bsg_register_queue(struct request_queue *q, const char *name)
  785. {
  786. struct bsg_class_device *bcd, *__bcd;
  787. dev_t dev;
  788. int ret = -EMFILE;
  789. struct class_device *class_dev = NULL;
  790. /*
  791. * we need a proper transport to send commands, not a stacked device
  792. */
  793. if (!q->request_fn)
  794. return 0;
  795. bcd = &q->bsg_dev;
  796. memset(bcd, 0, sizeof(*bcd));
  797. INIT_LIST_HEAD(&bcd->list);
  798. mutex_lock(&bsg_mutex);
  799. if (bsg_device_nr == BSG_MAX_DEVS) {
  800. printk(KERN_ERR "bsg: too many bsg devices\n");
  801. goto err;
  802. }
  803. retry:
  804. list_for_each_entry(__bcd, &bsg_class_list, list) {
  805. if (__bcd->minor == bsg_minor_idx) {
  806. bsg_minor_idx++;
  807. if (bsg_minor_idx == BSG_MAX_DEVS)
  808. bsg_minor_idx = 0;
  809. goto retry;
  810. }
  811. }
  812. bcd->minor = bsg_minor_idx++;
  813. if (bsg_minor_idx == BSG_MAX_DEVS)
  814. bsg_minor_idx = 0;
  815. bcd->queue = q;
  816. dev = MKDEV(BSG_MAJOR, bcd->minor);
  817. class_dev = class_device_create(bsg_class, NULL, dev, bcd->dev, "%s", name);
  818. if (IS_ERR(class_dev)) {
  819. ret = PTR_ERR(class_dev);
  820. goto err;
  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 err;
  827. }
  828. list_add_tail(&bcd->list, &bsg_class_list);
  829. bsg_device_nr++;
  830. mutex_unlock(&bsg_mutex);
  831. return 0;
  832. err:
  833. if (class_dev)
  834. class_device_destroy(bsg_class, MKDEV(BSG_MAJOR, bcd->minor));
  835. mutex_unlock(&bsg_mutex);
  836. return ret;
  837. }
  838. EXPORT_SYMBOL_GPL(bsg_register_queue);
  839. static int bsg_add(struct class_device *cl_dev, struct class_interface *cl_intf)
  840. {
  841. int ret;
  842. struct scsi_device *sdp = to_scsi_device(cl_dev->dev);
  843. struct request_queue *rq = sdp->request_queue;
  844. if (rq->kobj.parent)
  845. ret = bsg_register_queue(rq, kobject_name(rq->kobj.parent));
  846. else
  847. ret = bsg_register_queue(rq, kobject_name(&sdp->sdev_gendev.kobj));
  848. return ret;
  849. }
  850. static void bsg_remove(struct class_device *cl_dev, struct class_interface *cl_intf)
  851. {
  852. bsg_unregister_queue(to_scsi_device(cl_dev->dev)->request_queue);
  853. }
  854. static struct class_interface bsg_intf = {
  855. .add = bsg_add,
  856. .remove = bsg_remove,
  857. };
  858. static struct cdev bsg_cdev = {
  859. .kobj = {.name = "bsg", },
  860. .owner = THIS_MODULE,
  861. };
  862. static int __init bsg_init(void)
  863. {
  864. int ret, i;
  865. bsg_cmd_cachep = kmem_cache_create("bsg_cmd",
  866. sizeof(struct bsg_command), 0, 0, NULL, NULL);
  867. if (!bsg_cmd_cachep) {
  868. printk(KERN_ERR "bsg: failed creating slab cache\n");
  869. return -ENOMEM;
  870. }
  871. for (i = 0; i < BSG_LIST_ARRAY_SIZE; i++)
  872. INIT_HLIST_HEAD(&bsg_device_list[i]);
  873. bsg_class = class_create(THIS_MODULE, "bsg");
  874. if (IS_ERR(bsg_class)) {
  875. kmem_cache_destroy(bsg_cmd_cachep);
  876. return PTR_ERR(bsg_class);
  877. }
  878. ret = register_chrdev_region(MKDEV(BSG_MAJOR, 0), BSG_MAX_DEVS, "bsg");
  879. if (ret) {
  880. kmem_cache_destroy(bsg_cmd_cachep);
  881. class_destroy(bsg_class);
  882. return ret;
  883. }
  884. cdev_init(&bsg_cdev, &bsg_fops);
  885. ret = cdev_add(&bsg_cdev, MKDEV(BSG_MAJOR, 0), BSG_MAX_DEVS);
  886. if (ret) {
  887. kmem_cache_destroy(bsg_cmd_cachep);
  888. class_destroy(bsg_class);
  889. unregister_chrdev_region(MKDEV(BSG_MAJOR, 0), BSG_MAX_DEVS);
  890. return ret;
  891. }
  892. ret = scsi_register_interface(&bsg_intf);
  893. if (ret) {
  894. printk(KERN_ERR "bsg: failed register scsi interface %d\n", ret);
  895. kmem_cache_destroy(bsg_cmd_cachep);
  896. class_destroy(bsg_class);
  897. unregister_chrdev(BSG_MAJOR, "bsg");
  898. return ret;
  899. }
  900. printk(KERN_INFO "%s loaded\n", bsg_version);
  901. return 0;
  902. }
  903. MODULE_AUTHOR("Jens Axboe");
  904. MODULE_DESCRIPTION("Block layer SGSI generic (sg) driver");
  905. MODULE_LICENSE("GPL");
  906. device_initcall(bsg_init);