bsg.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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. 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. #define list_entry_bc(entry) list_entry((entry), struct bsg_command, list)
  64. /*
  65. * just for testing
  66. */
  67. #define BSG_MAJOR (240)
  68. static DEFINE_MUTEX(bsg_mutex);
  69. static int bsg_device_nr, bsg_minor_idx;
  70. #define BSG_LIST_SIZE (8)
  71. #define bsg_list_idx(minor) ((minor) & (BSG_LIST_SIZE - 1))
  72. static struct hlist_head bsg_device_list[BSG_LIST_SIZE];
  73. static struct class *bsg_class;
  74. static LIST_HEAD(bsg_class_list);
  75. static struct kmem_cache *bsg_cmd_cachep;
  76. /*
  77. * our internal command type
  78. */
  79. struct bsg_command {
  80. struct bsg_device *bd;
  81. struct list_head list;
  82. struct request *rq;
  83. struct bio *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 = NULL;
  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_alloc(bsg_cmd_cachep, GFP_USER);
  108. if (unlikely(!bc)) {
  109. spin_lock_irq(&bd->lock);
  110. bd->queued_cmds--;
  111. goto out;
  112. }
  113. memset(bc, 0, sizeof(*bc));
  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_del_done_cmd(struct bsg_device *bd, struct bsg_command *bc)
  124. {
  125. bd->done_cmds--;
  126. list_del(&bc->list);
  127. }
  128. static inline void
  129. bsg_add_done_cmd(struct bsg_device *bd, struct bsg_command *bc)
  130. {
  131. bd->done_cmds++;
  132. list_add_tail(&bc->list, &bd->done_list);
  133. wake_up(&bd->wq_done);
  134. }
  135. static inline int bsg_io_schedule(struct bsg_device *bd, int state)
  136. {
  137. DEFINE_WAIT(wait);
  138. int ret = 0;
  139. spin_lock_irq(&bd->lock);
  140. BUG_ON(bd->done_cmds > bd->queued_cmds);
  141. /*
  142. * -ENOSPC or -ENODATA? I'm going for -ENODATA, meaning "I have no
  143. * work to do", even though we return -ENOSPC after this same test
  144. * during bsg_write() -- there, it means our buffer can't have more
  145. * bsg_commands added to it, thus has no space left.
  146. */
  147. if (bd->done_cmds == bd->queued_cmds) {
  148. ret = -ENODATA;
  149. goto unlock;
  150. }
  151. if (!test_bit(BSG_F_BLOCK, &bd->flags)) {
  152. ret = -EAGAIN;
  153. goto unlock;
  154. }
  155. prepare_to_wait(&bd->wq_done, &wait, state);
  156. spin_unlock_irq(&bd->lock);
  157. io_schedule();
  158. finish_wait(&bd->wq_done, &wait);
  159. if ((state == TASK_INTERRUPTIBLE) && signal_pending(current))
  160. ret = -ERESTARTSYS;
  161. return ret;
  162. unlock:
  163. spin_unlock_irq(&bd->lock);
  164. return ret;
  165. }
  166. /*
  167. * get a new free command, blocking if needed and specified
  168. */
  169. static struct bsg_command *bsg_get_command(struct bsg_device *bd)
  170. {
  171. struct bsg_command *bc;
  172. int ret;
  173. do {
  174. bc = __bsg_alloc_command(bd);
  175. if (bc)
  176. break;
  177. ret = bsg_io_schedule(bd, TASK_INTERRUPTIBLE);
  178. if (ret) {
  179. bc = ERR_PTR(ret);
  180. break;
  181. }
  182. } while (1);
  183. return bc;
  184. }
  185. static int blk_fill_sgv4_hdr_rq(request_queue_t *q, struct request *rq,
  186. struct sg_io_v4 *hdr, int has_write_perm)
  187. {
  188. memset(rq->cmd, 0, BLK_MAX_CDB); /* ATAPI hates garbage after CDB */
  189. if (copy_from_user(rq->cmd, (void *)(unsigned long)hdr->request,
  190. hdr->request_len))
  191. return -EFAULT;
  192. if (blk_verify_command(rq->cmd, has_write_perm))
  193. return -EPERM;
  194. /*
  195. * fill in request structure
  196. */
  197. rq->cmd_len = hdr->request_len;
  198. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  199. rq->timeout = (hdr->timeout * HZ) / 1000;
  200. if (!rq->timeout)
  201. rq->timeout = q->sg_timeout;
  202. if (!rq->timeout)
  203. rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
  204. return 0;
  205. }
  206. /*
  207. * Check if sg_io_v4 from user is allowed and valid
  208. */
  209. static int
  210. bsg_validate_sgv4_hdr(request_queue_t *q, struct sg_io_v4 *hdr, int *rw)
  211. {
  212. if (hdr->guard != 'Q')
  213. return -EINVAL;
  214. if (hdr->request_len > BLK_MAX_CDB)
  215. return -EINVAL;
  216. if (hdr->dout_xfer_len > (q->max_sectors << 9) ||
  217. hdr->din_xfer_len > (q->max_sectors << 9))
  218. return -EIO;
  219. /* not supported currently */
  220. if (hdr->protocol || hdr->subprotocol)
  221. return -EINVAL;
  222. /*
  223. * looks sane, if no data then it should be fine from our POV
  224. */
  225. if (!hdr->dout_xfer_len && !hdr->din_xfer_len)
  226. return 0;
  227. /* not supported currently */
  228. if (hdr->dout_xfer_len && hdr->din_xfer_len)
  229. return -EINVAL;
  230. *rw = hdr->dout_xfer_len ? WRITE : READ;
  231. return 0;
  232. }
  233. /*
  234. * map sg_io_v4 to a request.
  235. */
  236. static struct request *
  237. bsg_map_hdr(struct bsg_device *bd, struct sg_io_v4 *hdr)
  238. {
  239. request_queue_t *q = bd->queue;
  240. struct request *rq;
  241. int ret, rw = 0; /* shut up gcc */
  242. unsigned int dxfer_len;
  243. void *dxferp = NULL;
  244. dprintk("map hdr %llx/%u %llx/%u\n", (unsigned long long) hdr->dout_xferp,
  245. hdr->dout_xfer_len, (unsigned long long) hdr->din_xferp,
  246. hdr->din_xfer_len);
  247. ret = bsg_validate_sgv4_hdr(q, hdr, &rw);
  248. if (ret)
  249. return ERR_PTR(ret);
  250. /*
  251. * map scatter-gather elements seperately and string them to request
  252. */
  253. rq = blk_get_request(q, rw, GFP_KERNEL);
  254. ret = blk_fill_sgv4_hdr_rq(q, rq, hdr, test_bit(BSG_F_WRITE_PERM,
  255. &bd->flags));
  256. if (ret) {
  257. blk_put_request(rq);
  258. return ERR_PTR(ret);
  259. }
  260. if (hdr->dout_xfer_len) {
  261. dxfer_len = hdr->dout_xfer_len;
  262. dxferp = (void*)(unsigned long)hdr->dout_xferp;
  263. } else if (hdr->din_xfer_len) {
  264. dxfer_len = hdr->din_xfer_len;
  265. dxferp = (void*)(unsigned long)hdr->din_xferp;
  266. } else
  267. dxfer_len = 0;
  268. if (dxfer_len) {
  269. ret = blk_rq_map_user(q, rq, dxferp, dxfer_len);
  270. if (ret) {
  271. dprintk("failed map at %d\n", ret);
  272. blk_put_request(rq);
  273. rq = ERR_PTR(ret);
  274. }
  275. }
  276. return rq;
  277. }
  278. /*
  279. * async completion call-back from the block layer, when scsi/ide/whatever
  280. * calls end_that_request_last() on a request
  281. */
  282. static void bsg_rq_end_io(struct request *rq, int uptodate)
  283. {
  284. struct bsg_command *bc = rq->end_io_data;
  285. struct bsg_device *bd = bc->bd;
  286. unsigned long flags;
  287. dprintk("%s: finished rq %p bc %p, bio %p stat %d\n",
  288. bd->name, rq, bc, bc->bio, uptodate);
  289. bc->hdr.duration = jiffies_to_msecs(jiffies - bc->hdr.duration);
  290. spin_lock_irqsave(&bd->lock, flags);
  291. list_del(&bc->list);
  292. bsg_add_done_cmd(bd, bc);
  293. spin_unlock_irqrestore(&bd->lock, flags);
  294. }
  295. /*
  296. * do final setup of a 'bc' and submit the matching 'rq' to the block
  297. * layer for io
  298. */
  299. static void bsg_add_command(struct bsg_device *bd, request_queue_t *q,
  300. struct bsg_command *bc, struct request *rq)
  301. {
  302. rq->sense = bc->sense;
  303. rq->sense_len = 0;
  304. /*
  305. * add bc command to busy queue and submit rq for io
  306. */
  307. bc->rq = rq;
  308. bc->bio = rq->bio;
  309. bc->hdr.duration = jiffies;
  310. spin_lock_irq(&bd->lock);
  311. list_add_tail(&bc->list, &bd->busy_list);
  312. spin_unlock_irq(&bd->lock);
  313. dprintk("%s: queueing rq %p, bc %p\n", bd->name, rq, bc);
  314. rq->end_io_data = bc;
  315. blk_execute_rq_nowait(q, NULL, rq, 1, bsg_rq_end_io);
  316. }
  317. static inline struct bsg_command *bsg_next_done_cmd(struct bsg_device *bd)
  318. {
  319. struct bsg_command *bc = NULL;
  320. spin_lock_irq(&bd->lock);
  321. if (bd->done_cmds) {
  322. bc = list_entry_bc(bd->done_list.next);
  323. bsg_del_done_cmd(bd, bc);
  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, int state)
  332. {
  333. struct bsg_command *bc;
  334. int ret;
  335. do {
  336. bc = bsg_next_done_cmd(bd);
  337. if (bc)
  338. break;
  339. ret = bsg_io_schedule(bd, state);
  340. if (ret) {
  341. bc = ERR_PTR(ret);
  342. break;
  343. }
  344. } while (1);
  345. dprintk("%s: returning done %p\n", bd->name, bc);
  346. return bc;
  347. }
  348. static struct bsg_command *
  349. bsg_get_done_cmd(struct bsg_device *bd, const struct iovec *iov)
  350. {
  351. return __bsg_get_done_cmd(bd, TASK_INTERRUPTIBLE);
  352. }
  353. static struct bsg_command *
  354. bsg_get_done_cmd_nosignals(struct bsg_device *bd)
  355. {
  356. return __bsg_get_done_cmd(bd, TASK_UNINTERRUPTIBLE);
  357. }
  358. static int blk_complete_sgv4_hdr_rq(struct request *rq, struct sg_io_v4 *hdr,
  359. struct bio *bio)
  360. {
  361. int ret = 0;
  362. dprintk("rq %p bio %p %u\n", rq, bio, rq->errors);
  363. /*
  364. * fill in all the output members
  365. */
  366. hdr->device_status = status_byte(rq->errors);
  367. hdr->transport_status = host_byte(rq->errors);
  368. hdr->driver_status = driver_byte(rq->errors);
  369. hdr->info = 0;
  370. if (hdr->device_status || hdr->transport_status || hdr->driver_status)
  371. hdr->info |= SG_INFO_CHECK;
  372. hdr->din_resid = rq->data_len;
  373. hdr->response_len = 0;
  374. if (rq->sense_len && hdr->response) {
  375. int len = min((unsigned int) hdr->max_response_len,
  376. rq->sense_len);
  377. ret = copy_to_user((void*)(unsigned long)hdr->response,
  378. rq->sense, len);
  379. if (!ret)
  380. hdr->response_len = len;
  381. else
  382. ret = -EFAULT;
  383. }
  384. blk_rq_unmap_user(bio);
  385. blk_put_request(rq);
  386. return ret;
  387. }
  388. static int bsg_complete_all_commands(struct bsg_device *bd)
  389. {
  390. struct bsg_command *bc;
  391. int ret, tret;
  392. dprintk("%s: entered\n", bd->name);
  393. set_bit(BSG_F_BLOCK, &bd->flags);
  394. /*
  395. * wait for all commands to complete
  396. */
  397. ret = 0;
  398. do {
  399. ret = bsg_io_schedule(bd, TASK_UNINTERRUPTIBLE);
  400. /*
  401. * look for -ENODATA specifically -- we'll sometimes get
  402. * -ERESTARTSYS when we've taken a signal, but we can't
  403. * return until we're done freeing the queue, so ignore
  404. * it. The signal will get handled when we're done freeing
  405. * the bsg_device.
  406. */
  407. } while (ret != -ENODATA);
  408. /*
  409. * discard done commands
  410. */
  411. ret = 0;
  412. do {
  413. bc = bsg_get_done_cmd_nosignals(bd);
  414. /*
  415. * we _must_ complete before restarting, because
  416. * bsg_release can't handle this failing.
  417. */
  418. if (PTR_ERR(bc) == -ERESTARTSYS)
  419. continue;
  420. if (IS_ERR(bc)) {
  421. ret = PTR_ERR(bc);
  422. break;
  423. }
  424. tret = blk_complete_sgv4_hdr_rq(bc->rq, &bc->hdr, bc->bio);
  425. if (!ret)
  426. ret = tret;
  427. bsg_free_command(bc);
  428. } while (1);
  429. return ret;
  430. }
  431. typedef struct bsg_command *(*bsg_command_callback)(struct bsg_device *bd, const struct iovec *iov);
  432. static ssize_t
  433. __bsg_read(char __user *buf, size_t count, bsg_command_callback get_bc,
  434. struct bsg_device *bd, const struct iovec *iov, ssize_t *bytes_read)
  435. {
  436. struct bsg_command *bc;
  437. int nr_commands, ret;
  438. if (count % sizeof(struct sg_io_v4))
  439. return -EINVAL;
  440. ret = 0;
  441. nr_commands = count / sizeof(struct sg_io_v4);
  442. while (nr_commands) {
  443. bc = get_bc(bd, iov);
  444. if (IS_ERR(bc)) {
  445. ret = PTR_ERR(bc);
  446. break;
  447. }
  448. /*
  449. * this is the only case where we need to copy data back
  450. * after completing the request. so do that here,
  451. * bsg_complete_work() cannot do that for us
  452. */
  453. ret = blk_complete_sgv4_hdr_rq(bc->rq, &bc->hdr, bc->bio);
  454. if (copy_to_user(buf, (char *) &bc->hdr, sizeof(bc->hdr)))
  455. ret = -EFAULT;
  456. bsg_free_command(bc);
  457. if (ret)
  458. break;
  459. buf += sizeof(struct sg_io_v4);
  460. *bytes_read += sizeof(struct sg_io_v4);
  461. nr_commands--;
  462. }
  463. return ret;
  464. }
  465. static inline void bsg_set_block(struct bsg_device *bd, struct file *file)
  466. {
  467. if (file->f_flags & O_NONBLOCK)
  468. clear_bit(BSG_F_BLOCK, &bd->flags);
  469. else
  470. set_bit(BSG_F_BLOCK, &bd->flags);
  471. }
  472. static inline void bsg_set_write_perm(struct bsg_device *bd, struct file *file)
  473. {
  474. if (file->f_mode & FMODE_WRITE)
  475. set_bit(BSG_F_WRITE_PERM, &bd->flags);
  476. else
  477. clear_bit(BSG_F_WRITE_PERM, &bd->flags);
  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, bsg_get_done_cmd,
  495. 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 ssize_t __bsg_write(struct bsg_device *bd, const char __user *buf,
  502. size_t count, ssize_t *bytes_read)
  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. request_queue_t *q = bd->queue;
  515. bc = bsg_get_command(bd);
  516. if (!bc)
  517. break;
  518. if (IS_ERR(bc)) {
  519. ret = PTR_ERR(bc);
  520. bc = NULL;
  521. break;
  522. }
  523. bc->uhdr = (struct sg_io_v4 __user *) buf;
  524. if (copy_from_user(&bc->hdr, buf, sizeof(bc->hdr))) {
  525. ret = -EFAULT;
  526. break;
  527. }
  528. /*
  529. * get a request, fill in the blanks, and add to request queue
  530. */
  531. rq = bsg_map_hdr(bd, &bc->hdr);
  532. if (IS_ERR(rq)) {
  533. ret = PTR_ERR(rq);
  534. rq = NULL;
  535. break;
  536. }
  537. bsg_add_command(bd, q, bc, rq);
  538. bc = NULL;
  539. rq = NULL;
  540. nr_commands--;
  541. buf += sizeof(struct sg_io_v4);
  542. *bytes_read += sizeof(struct sg_io_v4);
  543. }
  544. if (bc)
  545. bsg_free_command(bc);
  546. return ret;
  547. }
  548. static ssize_t
  549. bsg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  550. {
  551. struct bsg_device *bd = file->private_data;
  552. ssize_t bytes_read;
  553. int ret;
  554. dprintk("%s: write %Zd bytes\n", bd->name, count);
  555. bsg_set_block(bd, file);
  556. bsg_set_write_perm(bd, file);
  557. bytes_read = 0;
  558. ret = __bsg_write(bd, buf, count, &bytes_read);
  559. *ppos = bytes_read;
  560. /*
  561. * return bytes written on non-fatal errors
  562. */
  563. if (!bytes_read || (bytes_read && err_block_err(ret)))
  564. bytes_read = ret;
  565. dprintk("%s: returning %Zd\n", bd->name, bytes_read);
  566. return bytes_read;
  567. }
  568. static struct bsg_device *bsg_alloc_device(void)
  569. {
  570. struct bsg_device *bd;
  571. bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL);
  572. if (unlikely(!bd))
  573. return NULL;
  574. spin_lock_init(&bd->lock);
  575. bd->max_queue = BSG_DEFAULT_CMDS;
  576. INIT_LIST_HEAD(&bd->busy_list);
  577. INIT_LIST_HEAD(&bd->done_list);
  578. INIT_HLIST_NODE(&bd->dev_list);
  579. init_waitqueue_head(&bd->wq_free);
  580. init_waitqueue_head(&bd->wq_done);
  581. return bd;
  582. }
  583. static int bsg_put_device(struct bsg_device *bd)
  584. {
  585. int ret = 0;
  586. mutex_lock(&bsg_mutex);
  587. if (!atomic_dec_and_test(&bd->ref_count))
  588. goto out;
  589. dprintk("%s: tearing down\n", bd->name);
  590. /*
  591. * close can always block
  592. */
  593. set_bit(BSG_F_BLOCK, &bd->flags);
  594. /*
  595. * correct error detection baddies here again. it's the responsibility
  596. * of the app to properly reap commands before close() if it wants
  597. * fool-proof error detection
  598. */
  599. ret = bsg_complete_all_commands(bd);
  600. blk_put_queue(bd->queue);
  601. hlist_del(&bd->dev_list);
  602. kfree(bd);
  603. out:
  604. mutex_unlock(&bsg_mutex);
  605. return ret;
  606. }
  607. static struct bsg_device *bsg_add_device(struct inode *inode,
  608. struct request_queue *rq,
  609. struct file *file)
  610. {
  611. struct bsg_device *bd = NULL;
  612. #ifdef BSG_DEBUG
  613. unsigned char buf[32];
  614. #endif
  615. bd = bsg_alloc_device();
  616. if (!bd)
  617. return ERR_PTR(-ENOMEM);
  618. bd->queue = rq;
  619. kobject_get(&rq->kobj);
  620. bsg_set_block(bd, file);
  621. atomic_set(&bd->ref_count, 1);
  622. bd->minor = iminor(inode);
  623. mutex_lock(&bsg_mutex);
  624. hlist_add_head(&bd->dev_list, &bsg_device_list[bsg_list_idx(bd->minor)]);
  625. strncpy(bd->name, rq->bsg_dev.class_dev->class_id, sizeof(bd->name) - 1);
  626. dprintk("bound to <%s>, max queue %d\n",
  627. format_dev_t(buf, inode->i_rdev), bd->max_queue);
  628. mutex_unlock(&bsg_mutex);
  629. return bd;
  630. }
  631. static struct bsg_device *__bsg_get_device(int minor)
  632. {
  633. struct hlist_head *list = &bsg_device_list[bsg_list_idx(minor)];
  634. struct bsg_device *bd = NULL;
  635. struct hlist_node *entry;
  636. mutex_lock(&bsg_mutex);
  637. hlist_for_each(entry, list) {
  638. bd = hlist_entry(entry, struct bsg_device, dev_list);
  639. if (bd->minor == minor) {
  640. atomic_inc(&bd->ref_count);
  641. break;
  642. }
  643. bd = NULL;
  644. }
  645. mutex_unlock(&bsg_mutex);
  646. return bd;
  647. }
  648. static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file)
  649. {
  650. struct bsg_device *bd = __bsg_get_device(iminor(inode));
  651. struct bsg_class_device *bcd, *__bcd;
  652. if (bd)
  653. return bd;
  654. /*
  655. * find the class device
  656. */
  657. bcd = NULL;
  658. mutex_lock(&bsg_mutex);
  659. list_for_each_entry(__bcd, &bsg_class_list, list) {
  660. if (__bcd->minor == iminor(inode)) {
  661. bcd = __bcd;
  662. break;
  663. }
  664. }
  665. mutex_unlock(&bsg_mutex);
  666. if (!bcd)
  667. return ERR_PTR(-ENODEV);
  668. return bsg_add_device(inode, bcd->queue, file);
  669. }
  670. static int bsg_open(struct inode *inode, struct file *file)
  671. {
  672. struct bsg_device *bd = bsg_get_device(inode, file);
  673. if (IS_ERR(bd))
  674. return PTR_ERR(bd);
  675. file->private_data = bd;
  676. return 0;
  677. }
  678. static int bsg_release(struct inode *inode, struct file *file)
  679. {
  680. struct bsg_device *bd = file->private_data;
  681. file->private_data = NULL;
  682. return bsg_put_device(bd);
  683. }
  684. static unsigned int bsg_poll(struct file *file, poll_table *wait)
  685. {
  686. struct bsg_device *bd = file->private_data;
  687. unsigned int mask = 0;
  688. poll_wait(file, &bd->wq_done, wait);
  689. poll_wait(file, &bd->wq_free, wait);
  690. spin_lock_irq(&bd->lock);
  691. if (!list_empty(&bd->done_list))
  692. mask |= POLLIN | POLLRDNORM;
  693. if (bd->queued_cmds >= bd->max_queue)
  694. mask |= POLLOUT;
  695. spin_unlock_irq(&bd->lock);
  696. return mask;
  697. }
  698. static int
  699. bsg_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  700. unsigned long arg)
  701. {
  702. struct bsg_device *bd = file->private_data;
  703. int __user *uarg = (int __user *) arg;
  704. if (!bd)
  705. return -ENXIO;
  706. switch (cmd) {
  707. /*
  708. * our own ioctls
  709. */
  710. case SG_GET_COMMAND_Q:
  711. return put_user(bd->max_queue, uarg);
  712. case SG_SET_COMMAND_Q: {
  713. int queue;
  714. if (get_user(queue, uarg))
  715. return -EFAULT;
  716. if (queue < 1)
  717. return -EINVAL;
  718. spin_lock_irq(&bd->lock);
  719. bd->max_queue = queue;
  720. spin_unlock_irq(&bd->lock);
  721. return 0;
  722. }
  723. /*
  724. * SCSI/sg ioctls
  725. */
  726. case SG_GET_VERSION_NUM:
  727. case SCSI_IOCTL_GET_IDLUN:
  728. case SCSI_IOCTL_GET_BUS_NUMBER:
  729. case SG_SET_TIMEOUT:
  730. case SG_GET_TIMEOUT:
  731. case SG_GET_RESERVED_SIZE:
  732. case SG_SET_RESERVED_SIZE:
  733. case SG_EMULATED_HOST:
  734. case SCSI_IOCTL_SEND_COMMAND: {
  735. void __user *uarg = (void __user *) arg;
  736. return scsi_cmd_ioctl(file, bd->queue, NULL, cmd, uarg);
  737. }
  738. case SG_IO: {
  739. struct request *rq;
  740. struct bio *bio;
  741. struct sg_io_v4 hdr;
  742. if (copy_from_user(&hdr, uarg, sizeof(hdr)))
  743. return -EFAULT;
  744. rq = bsg_map_hdr(bd, &hdr);
  745. if (IS_ERR(rq))
  746. return PTR_ERR(rq);
  747. bio = rq->bio;
  748. blk_execute_rq(bd->queue, NULL, rq, 0);
  749. blk_complete_sgv4_hdr_rq(rq, &hdr, bio);
  750. if (copy_to_user(uarg, &hdr, sizeof(hdr)))
  751. return -EFAULT;
  752. return 0;
  753. }
  754. /*
  755. * block device ioctls
  756. */
  757. default:
  758. #if 0
  759. return ioctl_by_bdev(bd->bdev, cmd, arg);
  760. #else
  761. return -ENOTTY;
  762. #endif
  763. }
  764. }
  765. static struct file_operations bsg_fops = {
  766. .read = bsg_read,
  767. .write = bsg_write,
  768. .poll = bsg_poll,
  769. .open = bsg_open,
  770. .release = bsg_release,
  771. .ioctl = bsg_ioctl,
  772. .owner = THIS_MODULE,
  773. };
  774. void bsg_unregister_queue(struct request_queue *q)
  775. {
  776. struct bsg_class_device *bcd = &q->bsg_dev;
  777. if (!bcd->class_dev)
  778. return;
  779. mutex_lock(&bsg_mutex);
  780. sysfs_remove_link(&q->kobj, "bsg");
  781. class_device_destroy(bsg_class, MKDEV(BSG_MAJOR, bcd->minor));
  782. bcd->class_dev = NULL;
  783. list_del_init(&bcd->list);
  784. bsg_device_nr--;
  785. mutex_unlock(&bsg_mutex);
  786. }
  787. EXPORT_SYMBOL_GPL(bsg_unregister_queue);
  788. int bsg_register_queue(struct request_queue *q, const char *name)
  789. {
  790. struct bsg_class_device *bcd, *__bcd;
  791. dev_t dev;
  792. int ret = -EMFILE;
  793. struct class_device *class_dev = NULL;
  794. /*
  795. * we need a proper transport to send commands, not a stacked device
  796. */
  797. if (!q->request_fn)
  798. return 0;
  799. bcd = &q->bsg_dev;
  800. memset(bcd, 0, sizeof(*bcd));
  801. INIT_LIST_HEAD(&bcd->list);
  802. mutex_lock(&bsg_mutex);
  803. if (bsg_device_nr == BSG_MAX_DEVS) {
  804. printk(KERN_ERR "bsg: too many bsg devices\n");
  805. goto err;
  806. }
  807. retry:
  808. list_for_each_entry(__bcd, &bsg_class_list, list) {
  809. if (__bcd->minor == bsg_minor_idx) {
  810. bsg_minor_idx++;
  811. if (bsg_minor_idx == BSG_MAX_DEVS)
  812. bsg_minor_idx = 0;
  813. goto retry;
  814. }
  815. }
  816. bcd->minor = bsg_minor_idx++;
  817. if (bsg_minor_idx == BSG_MAX_DEVS)
  818. bsg_minor_idx = 0;
  819. bcd->queue = q;
  820. dev = MKDEV(BSG_MAJOR, bcd->minor);
  821. class_dev = class_device_create(bsg_class, NULL, dev, bcd->dev, "%s", name);
  822. if (IS_ERR(class_dev)) {
  823. ret = PTR_ERR(class_dev);
  824. goto err;
  825. }
  826. bcd->class_dev = class_dev;
  827. if (q->kobj.dentry) {
  828. ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg");
  829. if (ret)
  830. goto err;
  831. }
  832. list_add_tail(&bcd->list, &bsg_class_list);
  833. bsg_device_nr++;
  834. mutex_unlock(&bsg_mutex);
  835. return 0;
  836. err:
  837. if (class_dev)
  838. class_device_destroy(bsg_class, MKDEV(BSG_MAJOR, bcd->minor));
  839. mutex_unlock(&bsg_mutex);
  840. return ret;
  841. }
  842. EXPORT_SYMBOL_GPL(bsg_register_queue);
  843. static int bsg_add(struct class_device *cl_dev, struct class_interface *cl_intf)
  844. {
  845. int ret;
  846. struct scsi_device *sdp = to_scsi_device(cl_dev->dev);
  847. struct request_queue *rq = sdp->request_queue;
  848. if (rq->kobj.parent)
  849. ret = bsg_register_queue(rq, kobject_name(rq->kobj.parent));
  850. else
  851. ret = bsg_register_queue(rq, kobject_name(&sdp->sdev_gendev.kobj));
  852. return ret;
  853. }
  854. static void bsg_remove(struct class_device *cl_dev, struct class_interface *cl_intf)
  855. {
  856. bsg_unregister_queue(to_scsi_device(cl_dev->dev)->request_queue);
  857. }
  858. static struct class_interface bsg_intf = {
  859. .add = bsg_add,
  860. .remove = bsg_remove,
  861. };
  862. static struct cdev bsg_cdev = {
  863. .kobj = {.name = "bsg", },
  864. .owner = THIS_MODULE,
  865. };
  866. static int __init bsg_init(void)
  867. {
  868. int ret, i;
  869. bsg_cmd_cachep = kmem_cache_create("bsg_cmd",
  870. sizeof(struct bsg_command), 0, 0, NULL, NULL);
  871. if (!bsg_cmd_cachep) {
  872. printk(KERN_ERR "bsg: failed creating slab cache\n");
  873. return -ENOMEM;
  874. }
  875. for (i = 0; i < BSG_LIST_SIZE; i++)
  876. INIT_HLIST_HEAD(&bsg_device_list[i]);
  877. bsg_class = class_create(THIS_MODULE, "bsg");
  878. if (IS_ERR(bsg_class)) {
  879. kmem_cache_destroy(bsg_cmd_cachep);
  880. return PTR_ERR(bsg_class);
  881. }
  882. ret = register_chrdev_region(MKDEV(BSG_MAJOR, 0), BSG_MAX_DEVS, "bsg");
  883. if (ret) {
  884. kmem_cache_destroy(bsg_cmd_cachep);
  885. class_destroy(bsg_class);
  886. return ret;
  887. }
  888. cdev_init(&bsg_cdev, &bsg_fops);
  889. ret = cdev_add(&bsg_cdev, MKDEV(BSG_MAJOR, 0), BSG_MAX_DEVS);
  890. if (ret) {
  891. kmem_cache_destroy(bsg_cmd_cachep);
  892. class_destroy(bsg_class);
  893. unregister_chrdev_region(MKDEV(BSG_MAJOR, 0), BSG_MAX_DEVS);
  894. return ret;
  895. }
  896. ret = scsi_register_interface(&bsg_intf);
  897. if (ret) {
  898. printk(KERN_ERR "bsg: failed register scsi interface %d\n", ret);
  899. kmem_cache_destroy(bsg_cmd_cachep);
  900. class_destroy(bsg_class);
  901. unregister_chrdev(BSG_MAJOR, "bsg");
  902. return ret;
  903. }
  904. printk(KERN_INFO "%s loaded\n", bsg_version);
  905. return 0;
  906. }
  907. MODULE_AUTHOR("Jens Axboe");
  908. MODULE_DESCRIPTION("Block layer SGSI generic (sg) driver");
  909. MODULE_LICENSE("GPL");
  910. device_initcall(bsg_init);