sg.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700
  1. /*
  2. * History:
  3. * Started: Aug 9 by Lawrence Foard (entropy@world.std.com),
  4. * to allow user process control of SCSI devices.
  5. * Development Sponsored by Killy Corp. NY NY
  6. *
  7. * Original driver (sg.c):
  8. * Copyright (C) 1992 Lawrence Foard
  9. * Version 2 and 3 extensions to driver:
  10. * Copyright (C) 1998 - 2014 Douglas Gilbert
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. */
  18. static int sg_version_num = 30536; /* 2 digits for each component */
  19. #define SG_VERSION_STR "3.5.36"
  20. /*
  21. * D. P. Gilbert (dgilbert@interlog.com), notes:
  22. * - scsi logging is available via SCSI_LOG_TIMEOUT macros. First
  23. * the kernel/module needs to be built with CONFIG_SCSI_LOGGING
  24. * (otherwise the macros compile to empty statements).
  25. *
  26. */
  27. #include <linux/module.h>
  28. #include <linux/fs.h>
  29. #include <linux/kernel.h>
  30. #include <linux/sched.h>
  31. #include <linux/string.h>
  32. #include <linux/mm.h>
  33. #include <linux/errno.h>
  34. #include <linux/mtio.h>
  35. #include <linux/ioctl.h>
  36. #include <linux/slab.h>
  37. #include <linux/fcntl.h>
  38. #include <linux/init.h>
  39. #include <linux/poll.h>
  40. #include <linux/moduleparam.h>
  41. #include <linux/cdev.h>
  42. #include <linux/idr.h>
  43. #include <linux/seq_file.h>
  44. #include <linux/blkdev.h>
  45. #include <linux/delay.h>
  46. #include <linux/blktrace_api.h>
  47. #include <linux/mutex.h>
  48. #include <linux/atomic.h>
  49. #include <linux/ratelimit.h>
  50. #include <linux/uio.h>
  51. #include "scsi.h"
  52. #include <scsi/scsi_dbg.h>
  53. #include <scsi/scsi_host.h>
  54. #include <scsi/scsi_driver.h>
  55. #include <scsi/scsi_ioctl.h>
  56. #include <scsi/sg.h>
  57. #include "scsi_logging.h"
  58. #ifdef CONFIG_SCSI_PROC_FS
  59. #include <linux/proc_fs.h>
  60. static char *sg_version_date = "20140603";
  61. static int sg_proc_init(void);
  62. static void sg_proc_cleanup(void);
  63. #endif
  64. #define SG_ALLOW_DIO_DEF 0
  65. #define SG_MAX_DEVS 32768
  66. /* SG_MAX_CDB_SIZE should be 260 (spc4r37 section 3.1.30) however the type
  67. * of sg_io_hdr::cmd_len can only represent 255. All SCSI commands greater
  68. * than 16 bytes are "variable length" whose length is a multiple of 4
  69. */
  70. #define SG_MAX_CDB_SIZE 252
  71. /*
  72. * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
  73. * Then when using 32 bit integers x * m may overflow during the calculation.
  74. * Replacing muldiv(x) by muldiv(x)=((x % d) * m) / d + int(x / d) * m
  75. * calculates the same, but prevents the overflow when both m and d
  76. * are "small" numbers (like HZ and USER_HZ).
  77. * Of course an overflow is inavoidable if the result of muldiv doesn't fit
  78. * in 32 bits.
  79. */
  80. #define MULDIV(X,MUL,DIV) ((((X % DIV) * MUL) / DIV) + ((X / DIV) * MUL))
  81. #define SG_DEFAULT_TIMEOUT MULDIV(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ)
  82. int sg_big_buff = SG_DEF_RESERVED_SIZE;
  83. /* N.B. This variable is readable and writeable via
  84. /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
  85. of this size (or less if there is not enough memory) will be reserved
  86. for use by this file descriptor. [Deprecated usage: this variable is also
  87. readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into
  88. the kernel (i.e. it is not a module).] */
  89. static int def_reserved_size = -1; /* picks up init parameter */
  90. static int sg_allow_dio = SG_ALLOW_DIO_DEF;
  91. static int scatter_elem_sz = SG_SCATTER_SZ;
  92. static int scatter_elem_sz_prev = SG_SCATTER_SZ;
  93. #define SG_SECTOR_SZ 512
  94. static int sg_add_device(struct device *, struct class_interface *);
  95. static void sg_remove_device(struct device *, struct class_interface *);
  96. static DEFINE_IDR(sg_index_idr);
  97. static DEFINE_RWLOCK(sg_index_lock); /* Also used to lock
  98. file descriptor list for device */
  99. static struct class_interface sg_interface = {
  100. .add_dev = sg_add_device,
  101. .remove_dev = sg_remove_device,
  102. };
  103. typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */
  104. unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */
  105. unsigned sglist_len; /* size of malloc'd scatter-gather list ++ */
  106. unsigned bufflen; /* Size of (aggregate) data buffer */
  107. struct page **pages;
  108. int page_order;
  109. char dio_in_use; /* 0->indirect IO (or mmap), 1->dio */
  110. unsigned char cmd_opcode; /* first byte of command */
  111. } Sg_scatter_hold;
  112. struct sg_device; /* forward declarations */
  113. struct sg_fd;
  114. typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */
  115. struct sg_request *nextrp; /* NULL -> tail request (slist) */
  116. struct sg_fd *parentfp; /* NULL -> not in use */
  117. Sg_scatter_hold data; /* hold buffer, perhaps scatter list */
  118. sg_io_hdr_t header; /* scsi command+info, see <scsi/sg.h> */
  119. unsigned char sense_b[SCSI_SENSE_BUFFERSIZE];
  120. char res_used; /* 1 -> using reserve buffer, 0 -> not ... */
  121. char orphan; /* 1 -> drop on sight, 0 -> normal */
  122. char sg_io_owned; /* 1 -> packet belongs to SG_IO */
  123. /* done protected by rq_list_lock */
  124. char done; /* 0->before bh, 1->before read, 2->read */
  125. struct request *rq;
  126. struct bio *bio;
  127. struct execute_work ew;
  128. } Sg_request;
  129. typedef struct sg_fd { /* holds the state of a file descriptor */
  130. struct list_head sfd_siblings; /* protected by device's sfd_lock */
  131. struct sg_device *parentdp; /* owning device */
  132. wait_queue_head_t read_wait; /* queue read until command done */
  133. rwlock_t rq_list_lock; /* protect access to list in req_arr */
  134. int timeout; /* defaults to SG_DEFAULT_TIMEOUT */
  135. int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */
  136. Sg_scatter_hold reserve; /* buffer held for this file descriptor */
  137. unsigned save_scat_len; /* original length of trunc. scat. element */
  138. Sg_request *headrp; /* head of request slist, NULL->empty */
  139. struct fasync_struct *async_qp; /* used by asynchronous notification */
  140. Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */
  141. char low_dma; /* as in parent but possibly overridden to 1 */
  142. char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
  143. char cmd_q; /* 1 -> allow command queuing, 0 -> don't */
  144. unsigned char next_cmd_len; /* 0: automatic, >0: use on next write() */
  145. char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
  146. char mmap_called; /* 0 -> mmap() never called on this fd */
  147. struct kref f_ref;
  148. struct execute_work ew;
  149. } Sg_fd;
  150. typedef struct sg_device { /* holds the state of each scsi generic device */
  151. struct scsi_device *device;
  152. wait_queue_head_t open_wait; /* queue open() when O_EXCL present */
  153. struct mutex open_rel_lock; /* held when in open() or release() */
  154. int sg_tablesize; /* adapter's max scatter-gather table size */
  155. u32 index; /* device index number */
  156. struct list_head sfds;
  157. rwlock_t sfd_lock; /* protect access to sfd list */
  158. atomic_t detaching; /* 0->device usable, 1->device detaching */
  159. bool exclude; /* 1->open(O_EXCL) succeeded and is active */
  160. int open_cnt; /* count of opens (perhaps < num(sfds) ) */
  161. char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
  162. struct gendisk *disk;
  163. struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
  164. struct kref d_ref;
  165. } Sg_device;
  166. /* tasklet or soft irq callback */
  167. static void sg_rq_end_io(struct request *rq, int uptodate);
  168. static int sg_start_req(Sg_request *srp, unsigned char *cmd);
  169. static int sg_finish_rem_req(Sg_request * srp);
  170. static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
  171. static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
  172. Sg_request * srp);
  173. static ssize_t sg_new_write(Sg_fd *sfp, struct file *file,
  174. const char __user *buf, size_t count, int blocking,
  175. int read_only, int sg_io_owned, Sg_request **o_srp);
  176. static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
  177. unsigned char *cmnd, int timeout, int blocking);
  178. static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
  179. static void sg_remove_scat(Sg_fd * sfp, Sg_scatter_hold * schp);
  180. static void sg_build_reserve(Sg_fd * sfp, int req_size);
  181. static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
  182. static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
  183. static Sg_fd *sg_add_sfp(Sg_device * sdp);
  184. static void sg_remove_sfp(struct kref *);
  185. static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
  186. static Sg_request *sg_add_request(Sg_fd * sfp);
  187. static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
  188. static int sg_res_in_use(Sg_fd * sfp);
  189. static Sg_device *sg_get_dev(int dev);
  190. static void sg_device_destroy(struct kref *kref);
  191. #define SZ_SG_HEADER sizeof(struct sg_header)
  192. #define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
  193. #define SZ_SG_IOVEC sizeof(sg_iovec_t)
  194. #define SZ_SG_REQ_INFO sizeof(sg_req_info_t)
  195. #define sg_printk(prefix, sdp, fmt, a...) \
  196. sdev_prefix_printk(prefix, (sdp)->device, \
  197. (sdp)->disk->disk_name, fmt, ##a)
  198. static int sg_allow_access(struct file *filp, unsigned char *cmd)
  199. {
  200. struct sg_fd *sfp = filp->private_data;
  201. if (sfp->parentdp->device->type == TYPE_SCANNER)
  202. return 0;
  203. return blk_verify_command(cmd, filp->f_mode & FMODE_WRITE);
  204. }
  205. static int
  206. open_wait(Sg_device *sdp, int flags)
  207. {
  208. int retval = 0;
  209. if (flags & O_EXCL) {
  210. while (sdp->open_cnt > 0) {
  211. mutex_unlock(&sdp->open_rel_lock);
  212. retval = wait_event_interruptible(sdp->open_wait,
  213. (atomic_read(&sdp->detaching) ||
  214. !sdp->open_cnt));
  215. mutex_lock(&sdp->open_rel_lock);
  216. if (retval) /* -ERESTARTSYS */
  217. return retval;
  218. if (atomic_read(&sdp->detaching))
  219. return -ENODEV;
  220. }
  221. } else {
  222. while (sdp->exclude) {
  223. mutex_unlock(&sdp->open_rel_lock);
  224. retval = wait_event_interruptible(sdp->open_wait,
  225. (atomic_read(&sdp->detaching) ||
  226. !sdp->exclude));
  227. mutex_lock(&sdp->open_rel_lock);
  228. if (retval) /* -ERESTARTSYS */
  229. return retval;
  230. if (atomic_read(&sdp->detaching))
  231. return -ENODEV;
  232. }
  233. }
  234. return retval;
  235. }
  236. /* Returns 0 on success, else a negated errno value */
  237. static int
  238. sg_open(struct inode *inode, struct file *filp)
  239. {
  240. int dev = iminor(inode);
  241. int flags = filp->f_flags;
  242. struct request_queue *q;
  243. Sg_device *sdp;
  244. Sg_fd *sfp;
  245. int retval;
  246. nonseekable_open(inode, filp);
  247. if ((flags & O_EXCL) && (O_RDONLY == (flags & O_ACCMODE)))
  248. return -EPERM; /* Can't lock it with read only access */
  249. sdp = sg_get_dev(dev);
  250. if (IS_ERR(sdp))
  251. return PTR_ERR(sdp);
  252. SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
  253. "sg_open: flags=0x%x\n", flags));
  254. /* This driver's module count bumped by fops_get in <linux/fs.h> */
  255. /* Prevent the device driver from vanishing while we sleep */
  256. retval = scsi_device_get(sdp->device);
  257. if (retval)
  258. goto sg_put;
  259. retval = scsi_autopm_get_device(sdp->device);
  260. if (retval)
  261. goto sdp_put;
  262. /* scsi_block_when_processing_errors() may block so bypass
  263. * check if O_NONBLOCK. Permits SCSI commands to be issued
  264. * during error recovery. Tread carefully. */
  265. if (!((flags & O_NONBLOCK) ||
  266. scsi_block_when_processing_errors(sdp->device))) {
  267. retval = -ENXIO;
  268. /* we are in error recovery for this device */
  269. goto error_out;
  270. }
  271. mutex_lock(&sdp->open_rel_lock);
  272. if (flags & O_NONBLOCK) {
  273. if (flags & O_EXCL) {
  274. if (sdp->open_cnt > 0) {
  275. retval = -EBUSY;
  276. goto error_mutex_locked;
  277. }
  278. } else {
  279. if (sdp->exclude) {
  280. retval = -EBUSY;
  281. goto error_mutex_locked;
  282. }
  283. }
  284. } else {
  285. retval = open_wait(sdp, flags);
  286. if (retval) /* -ERESTARTSYS or -ENODEV */
  287. goto error_mutex_locked;
  288. }
  289. /* N.B. at this point we are holding the open_rel_lock */
  290. if (flags & O_EXCL)
  291. sdp->exclude = true;
  292. if (sdp->open_cnt < 1) { /* no existing opens */
  293. sdp->sgdebug = 0;
  294. q = sdp->device->request_queue;
  295. sdp->sg_tablesize = queue_max_segments(q);
  296. }
  297. sfp = sg_add_sfp(sdp);
  298. if (IS_ERR(sfp)) {
  299. retval = PTR_ERR(sfp);
  300. goto out_undo;
  301. }
  302. filp->private_data = sfp;
  303. sdp->open_cnt++;
  304. mutex_unlock(&sdp->open_rel_lock);
  305. retval = 0;
  306. sg_put:
  307. kref_put(&sdp->d_ref, sg_device_destroy);
  308. return retval;
  309. out_undo:
  310. if (flags & O_EXCL) {
  311. sdp->exclude = false; /* undo if error */
  312. wake_up_interruptible(&sdp->open_wait);
  313. }
  314. error_mutex_locked:
  315. mutex_unlock(&sdp->open_rel_lock);
  316. error_out:
  317. scsi_autopm_put_device(sdp->device);
  318. sdp_put:
  319. scsi_device_put(sdp->device);
  320. goto sg_put;
  321. }
  322. /* Release resources associated with a successful sg_open()
  323. * Returns 0 on success, else a negated errno value */
  324. static int
  325. sg_release(struct inode *inode, struct file *filp)
  326. {
  327. Sg_device *sdp;
  328. Sg_fd *sfp;
  329. if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
  330. return -ENXIO;
  331. SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp, "sg_release\n"));
  332. mutex_lock(&sdp->open_rel_lock);
  333. scsi_autopm_put_device(sdp->device);
  334. kref_put(&sfp->f_ref, sg_remove_sfp);
  335. sdp->open_cnt--;
  336. /* possibly many open()s waiting on exlude clearing, start many;
  337. * only open(O_EXCL)s wait on 0==open_cnt so only start one */
  338. if (sdp->exclude) {
  339. sdp->exclude = false;
  340. wake_up_interruptible_all(&sdp->open_wait);
  341. } else if (0 == sdp->open_cnt) {
  342. wake_up_interruptible(&sdp->open_wait);
  343. }
  344. mutex_unlock(&sdp->open_rel_lock);
  345. return 0;
  346. }
  347. static ssize_t
  348. sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
  349. {
  350. Sg_device *sdp;
  351. Sg_fd *sfp;
  352. Sg_request *srp;
  353. int req_pack_id = -1;
  354. sg_io_hdr_t *hp;
  355. struct sg_header *old_hdr = NULL;
  356. int retval = 0;
  357. if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
  358. return -ENXIO;
  359. SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
  360. "sg_read: count=%d\n", (int) count));
  361. if (!access_ok(VERIFY_WRITE, buf, count))
  362. return -EFAULT;
  363. if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
  364. old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
  365. if (!old_hdr)
  366. return -ENOMEM;
  367. if (__copy_from_user(old_hdr, buf, SZ_SG_HEADER)) {
  368. retval = -EFAULT;
  369. goto free_old_hdr;
  370. }
  371. if (old_hdr->reply_len < 0) {
  372. if (count >= SZ_SG_IO_HDR) {
  373. sg_io_hdr_t *new_hdr;
  374. new_hdr = kmalloc(SZ_SG_IO_HDR, GFP_KERNEL);
  375. if (!new_hdr) {
  376. retval = -ENOMEM;
  377. goto free_old_hdr;
  378. }
  379. retval =__copy_from_user
  380. (new_hdr, buf, SZ_SG_IO_HDR);
  381. req_pack_id = new_hdr->pack_id;
  382. kfree(new_hdr);
  383. if (retval) {
  384. retval = -EFAULT;
  385. goto free_old_hdr;
  386. }
  387. }
  388. } else
  389. req_pack_id = old_hdr->pack_id;
  390. }
  391. srp = sg_get_rq_mark(sfp, req_pack_id);
  392. if (!srp) { /* now wait on packet to arrive */
  393. if (atomic_read(&sdp->detaching)) {
  394. retval = -ENODEV;
  395. goto free_old_hdr;
  396. }
  397. if (filp->f_flags & O_NONBLOCK) {
  398. retval = -EAGAIN;
  399. goto free_old_hdr;
  400. }
  401. retval = wait_event_interruptible(sfp->read_wait,
  402. (atomic_read(&sdp->detaching) ||
  403. (srp = sg_get_rq_mark(sfp, req_pack_id))));
  404. if (atomic_read(&sdp->detaching)) {
  405. retval = -ENODEV;
  406. goto free_old_hdr;
  407. }
  408. if (retval) {
  409. /* -ERESTARTSYS as signal hit process */
  410. goto free_old_hdr;
  411. }
  412. }
  413. if (srp->header.interface_id != '\0') {
  414. retval = sg_new_read(sfp, buf, count, srp);
  415. goto free_old_hdr;
  416. }
  417. hp = &srp->header;
  418. if (old_hdr == NULL) {
  419. old_hdr = kmalloc(SZ_SG_HEADER, GFP_KERNEL);
  420. if (! old_hdr) {
  421. retval = -ENOMEM;
  422. goto free_old_hdr;
  423. }
  424. }
  425. memset(old_hdr, 0, SZ_SG_HEADER);
  426. old_hdr->reply_len = (int) hp->timeout;
  427. old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
  428. old_hdr->pack_id = hp->pack_id;
  429. old_hdr->twelve_byte =
  430. ((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0;
  431. old_hdr->target_status = hp->masked_status;
  432. old_hdr->host_status = hp->host_status;
  433. old_hdr->driver_status = hp->driver_status;
  434. if ((CHECK_CONDITION & hp->masked_status) ||
  435. (DRIVER_SENSE & hp->driver_status))
  436. memcpy(old_hdr->sense_buffer, srp->sense_b,
  437. sizeof (old_hdr->sense_buffer));
  438. switch (hp->host_status) {
  439. /* This setup of 'result' is for backward compatibility and is best
  440. ignored by the user who should use target, host + driver status */
  441. case DID_OK:
  442. case DID_PASSTHROUGH:
  443. case DID_SOFT_ERROR:
  444. old_hdr->result = 0;
  445. break;
  446. case DID_NO_CONNECT:
  447. case DID_BUS_BUSY:
  448. case DID_TIME_OUT:
  449. old_hdr->result = EBUSY;
  450. break;
  451. case DID_BAD_TARGET:
  452. case DID_ABORT:
  453. case DID_PARITY:
  454. case DID_RESET:
  455. case DID_BAD_INTR:
  456. old_hdr->result = EIO;
  457. break;
  458. case DID_ERROR:
  459. old_hdr->result = (srp->sense_b[0] == 0 &&
  460. hp->masked_status == GOOD) ? 0 : EIO;
  461. break;
  462. default:
  463. old_hdr->result = EIO;
  464. break;
  465. }
  466. /* Now copy the result back to the user buffer. */
  467. if (count >= SZ_SG_HEADER) {
  468. if (__copy_to_user(buf, old_hdr, SZ_SG_HEADER)) {
  469. retval = -EFAULT;
  470. goto free_old_hdr;
  471. }
  472. buf += SZ_SG_HEADER;
  473. if (count > old_hdr->reply_len)
  474. count = old_hdr->reply_len;
  475. if (count > SZ_SG_HEADER) {
  476. if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) {
  477. retval = -EFAULT;
  478. goto free_old_hdr;
  479. }
  480. }
  481. } else
  482. count = (old_hdr->result == 0) ? 0 : -EIO;
  483. sg_finish_rem_req(srp);
  484. retval = count;
  485. free_old_hdr:
  486. kfree(old_hdr);
  487. return retval;
  488. }
  489. static ssize_t
  490. sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
  491. {
  492. sg_io_hdr_t *hp = &srp->header;
  493. int err = 0, err2;
  494. int len;
  495. if (count < SZ_SG_IO_HDR) {
  496. err = -EINVAL;
  497. goto err_out;
  498. }
  499. hp->sb_len_wr = 0;
  500. if ((hp->mx_sb_len > 0) && hp->sbp) {
  501. if ((CHECK_CONDITION & hp->masked_status) ||
  502. (DRIVER_SENSE & hp->driver_status)) {
  503. int sb_len = SCSI_SENSE_BUFFERSIZE;
  504. sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len;
  505. len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */
  506. len = (len > sb_len) ? sb_len : len;
  507. if (copy_to_user(hp->sbp, srp->sense_b, len)) {
  508. err = -EFAULT;
  509. goto err_out;
  510. }
  511. hp->sb_len_wr = len;
  512. }
  513. }
  514. if (hp->masked_status || hp->host_status || hp->driver_status)
  515. hp->info |= SG_INFO_CHECK;
  516. if (copy_to_user(buf, hp, SZ_SG_IO_HDR)) {
  517. err = -EFAULT;
  518. goto err_out;
  519. }
  520. err_out:
  521. err2 = sg_finish_rem_req(srp);
  522. return err ? : err2 ? : count;
  523. }
  524. static ssize_t
  525. sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
  526. {
  527. int mxsize, cmd_size, k;
  528. int input_size, blocking;
  529. unsigned char opcode;
  530. Sg_device *sdp;
  531. Sg_fd *sfp;
  532. Sg_request *srp;
  533. struct sg_header old_hdr;
  534. sg_io_hdr_t *hp;
  535. unsigned char cmnd[SG_MAX_CDB_SIZE];
  536. if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
  537. return -ENXIO;
  538. SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
  539. "sg_write: count=%d\n", (int) count));
  540. if (atomic_read(&sdp->detaching))
  541. return -ENODEV;
  542. if (!((filp->f_flags & O_NONBLOCK) ||
  543. scsi_block_when_processing_errors(sdp->device)))
  544. return -ENXIO;
  545. if (!access_ok(VERIFY_READ, buf, count))
  546. return -EFAULT; /* protects following copy_from_user()s + get_user()s */
  547. if (count < SZ_SG_HEADER)
  548. return -EIO;
  549. if (__copy_from_user(&old_hdr, buf, SZ_SG_HEADER))
  550. return -EFAULT;
  551. blocking = !(filp->f_flags & O_NONBLOCK);
  552. if (old_hdr.reply_len < 0)
  553. return sg_new_write(sfp, filp, buf, count,
  554. blocking, 0, 0, NULL);
  555. if (count < (SZ_SG_HEADER + 6))
  556. return -EIO; /* The minimum scsi command length is 6 bytes. */
  557. if (!(srp = sg_add_request(sfp))) {
  558. SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO, sdp,
  559. "sg_write: queue full\n"));
  560. return -EDOM;
  561. }
  562. buf += SZ_SG_HEADER;
  563. __get_user(opcode, buf);
  564. if (sfp->next_cmd_len > 0) {
  565. cmd_size = sfp->next_cmd_len;
  566. sfp->next_cmd_len = 0; /* reset so only this write() effected */
  567. } else {
  568. cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */
  569. if ((opcode >= 0xc0) && old_hdr.twelve_byte)
  570. cmd_size = 12;
  571. }
  572. SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sdp,
  573. "sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size));
  574. /* Determine buffer size. */
  575. input_size = count - cmd_size;
  576. mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len;
  577. mxsize -= SZ_SG_HEADER;
  578. input_size -= SZ_SG_HEADER;
  579. if (input_size < 0) {
  580. sg_remove_request(sfp, srp);
  581. return -EIO; /* User did not pass enough bytes for this command. */
  582. }
  583. hp = &srp->header;
  584. hp->interface_id = '\0'; /* indicator of old interface tunnelled */
  585. hp->cmd_len = (unsigned char) cmd_size;
  586. hp->iovec_count = 0;
  587. hp->mx_sb_len = 0;
  588. if (input_size > 0)
  589. hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ?
  590. SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV;
  591. else
  592. hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE;
  593. hp->dxfer_len = mxsize;
  594. if (hp->dxfer_direction == SG_DXFER_TO_DEV)
  595. hp->dxferp = (char __user *)buf + cmd_size;
  596. else
  597. hp->dxferp = NULL;
  598. hp->sbp = NULL;
  599. hp->timeout = old_hdr.reply_len; /* structure abuse ... */
  600. hp->flags = input_size; /* structure abuse ... */
  601. hp->pack_id = old_hdr.pack_id;
  602. hp->usr_ptr = NULL;
  603. if (__copy_from_user(cmnd, buf, cmd_size))
  604. return -EFAULT;
  605. /*
  606. * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
  607. * but is is possible that the app intended SG_DXFER_TO_DEV, because there
  608. * is a non-zero input_size, so emit a warning.
  609. */
  610. if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
  611. static char cmd[TASK_COMM_LEN];
  612. if (strcmp(current->comm, cmd)) {
  613. printk_ratelimited(KERN_WARNING
  614. "sg_write: data in/out %d/%d bytes "
  615. "for SCSI command 0x%x-- guessing "
  616. "data in;\n program %s not setting "
  617. "count and/or reply_len properly\n",
  618. old_hdr.reply_len - (int)SZ_SG_HEADER,
  619. input_size, (unsigned int) cmnd[0],
  620. current->comm);
  621. strcpy(cmd, current->comm);
  622. }
  623. }
  624. k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
  625. return (k < 0) ? k : count;
  626. }
  627. static ssize_t
  628. sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
  629. size_t count, int blocking, int read_only, int sg_io_owned,
  630. Sg_request **o_srp)
  631. {
  632. int k;
  633. Sg_request *srp;
  634. sg_io_hdr_t *hp;
  635. unsigned char cmnd[SG_MAX_CDB_SIZE];
  636. int timeout;
  637. unsigned long ul_timeout;
  638. if (count < SZ_SG_IO_HDR)
  639. return -EINVAL;
  640. if (!access_ok(VERIFY_READ, buf, count))
  641. return -EFAULT; /* protects following copy_from_user()s + get_user()s */
  642. sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */
  643. if (!(srp = sg_add_request(sfp))) {
  644. SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO, sfp->parentdp,
  645. "sg_new_write: queue full\n"));
  646. return -EDOM;
  647. }
  648. srp->sg_io_owned = sg_io_owned;
  649. hp = &srp->header;
  650. if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
  651. sg_remove_request(sfp, srp);
  652. return -EFAULT;
  653. }
  654. if (hp->interface_id != 'S') {
  655. sg_remove_request(sfp, srp);
  656. return -ENOSYS;
  657. }
  658. if (hp->flags & SG_FLAG_MMAP_IO) {
  659. if (hp->dxfer_len > sfp->reserve.bufflen) {
  660. sg_remove_request(sfp, srp);
  661. return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */
  662. }
  663. if (hp->flags & SG_FLAG_DIRECT_IO) {
  664. sg_remove_request(sfp, srp);
  665. return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */
  666. }
  667. if (sg_res_in_use(sfp)) {
  668. sg_remove_request(sfp, srp);
  669. return -EBUSY; /* reserve buffer already being used */
  670. }
  671. }
  672. ul_timeout = msecs_to_jiffies(srp->header.timeout);
  673. timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
  674. if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
  675. sg_remove_request(sfp, srp);
  676. return -EMSGSIZE;
  677. }
  678. if (!access_ok(VERIFY_READ, hp->cmdp, hp->cmd_len)) {
  679. sg_remove_request(sfp, srp);
  680. return -EFAULT; /* protects following copy_from_user()s + get_user()s */
  681. }
  682. if (__copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) {
  683. sg_remove_request(sfp, srp);
  684. return -EFAULT;
  685. }
  686. if (read_only && sg_allow_access(file, cmnd)) {
  687. sg_remove_request(sfp, srp);
  688. return -EPERM;
  689. }
  690. k = sg_common_write(sfp, srp, cmnd, timeout, blocking);
  691. if (k < 0)
  692. return k;
  693. if (o_srp)
  694. *o_srp = srp;
  695. return count;
  696. }
  697. static int
  698. sg_common_write(Sg_fd * sfp, Sg_request * srp,
  699. unsigned char *cmnd, int timeout, int blocking)
  700. {
  701. int k, at_head;
  702. Sg_device *sdp = sfp->parentdp;
  703. sg_io_hdr_t *hp = &srp->header;
  704. srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */
  705. hp->status = 0;
  706. hp->masked_status = 0;
  707. hp->msg_status = 0;
  708. hp->info = 0;
  709. hp->host_status = 0;
  710. hp->driver_status = 0;
  711. hp->resid = 0;
  712. SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
  713. "sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n",
  714. (int) cmnd[0], (int) hp->cmd_len));
  715. k = sg_start_req(srp, cmnd);
  716. if (k) {
  717. SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO, sfp->parentdp,
  718. "sg_common_write: start_req err=%d\n", k));
  719. sg_finish_rem_req(srp);
  720. return k; /* probably out of space --> ENOMEM */
  721. }
  722. if (atomic_read(&sdp->detaching)) {
  723. if (srp->bio)
  724. blk_end_request_all(srp->rq, -EIO);
  725. sg_finish_rem_req(srp);
  726. return -ENODEV;
  727. }
  728. hp->duration = jiffies_to_msecs(jiffies);
  729. if (hp->interface_id != '\0' && /* v3 (or later) interface */
  730. (SG_FLAG_Q_AT_TAIL & hp->flags))
  731. at_head = 0;
  732. else
  733. at_head = 1;
  734. srp->rq->timeout = timeout;
  735. kref_get(&sfp->f_ref); /* sg_rq_end_io() does kref_put(). */
  736. blk_execute_rq_nowait(sdp->device->request_queue, sdp->disk,
  737. srp->rq, at_head, sg_rq_end_io);
  738. return 0;
  739. }
  740. static int srp_done(Sg_fd *sfp, Sg_request *srp)
  741. {
  742. unsigned long flags;
  743. int ret;
  744. read_lock_irqsave(&sfp->rq_list_lock, flags);
  745. ret = srp->done;
  746. read_unlock_irqrestore(&sfp->rq_list_lock, flags);
  747. return ret;
  748. }
  749. static int max_sectors_bytes(struct request_queue *q)
  750. {
  751. unsigned int max_sectors = queue_max_sectors(q);
  752. max_sectors = min_t(unsigned int, max_sectors, INT_MAX >> 9);
  753. return max_sectors << 9;
  754. }
  755. static long
  756. sg_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
  757. {
  758. void __user *p = (void __user *)arg;
  759. int __user *ip = p;
  760. int result, val, read_only;
  761. Sg_device *sdp;
  762. Sg_fd *sfp;
  763. Sg_request *srp;
  764. unsigned long iflags;
  765. if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
  766. return -ENXIO;
  767. SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
  768. "sg_ioctl: cmd=0x%x\n", (int) cmd_in));
  769. read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
  770. switch (cmd_in) {
  771. case SG_IO:
  772. if (atomic_read(&sdp->detaching))
  773. return -ENODEV;
  774. if (!scsi_block_when_processing_errors(sdp->device))
  775. return -ENXIO;
  776. if (!access_ok(VERIFY_WRITE, p, SZ_SG_IO_HDR))
  777. return -EFAULT;
  778. result = sg_new_write(sfp, filp, p, SZ_SG_IO_HDR,
  779. 1, read_only, 1, &srp);
  780. if (result < 0)
  781. return result;
  782. result = wait_event_interruptible(sfp->read_wait,
  783. (srp_done(sfp, srp) || atomic_read(&sdp->detaching)));
  784. if (atomic_read(&sdp->detaching))
  785. return -ENODEV;
  786. write_lock_irq(&sfp->rq_list_lock);
  787. if (srp->done) {
  788. srp->done = 2;
  789. write_unlock_irq(&sfp->rq_list_lock);
  790. result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
  791. return (result < 0) ? result : 0;
  792. }
  793. srp->orphan = 1;
  794. write_unlock_irq(&sfp->rq_list_lock);
  795. return result; /* -ERESTARTSYS because signal hit process */
  796. case SG_SET_TIMEOUT:
  797. result = get_user(val, ip);
  798. if (result)
  799. return result;
  800. if (val < 0)
  801. return -EIO;
  802. if (val >= MULDIV (INT_MAX, USER_HZ, HZ))
  803. val = MULDIV (INT_MAX, USER_HZ, HZ);
  804. sfp->timeout_user = val;
  805. sfp->timeout = MULDIV (val, HZ, USER_HZ);
  806. return 0;
  807. case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */
  808. /* strange ..., for backward compatibility */
  809. return sfp->timeout_user;
  810. case SG_SET_FORCE_LOW_DMA:
  811. result = get_user(val, ip);
  812. if (result)
  813. return result;
  814. if (val) {
  815. sfp->low_dma = 1;
  816. if ((0 == sfp->low_dma) && (0 == sg_res_in_use(sfp))) {
  817. val = (int) sfp->reserve.bufflen;
  818. sg_remove_scat(sfp, &sfp->reserve);
  819. sg_build_reserve(sfp, val);
  820. }
  821. } else {
  822. if (atomic_read(&sdp->detaching))
  823. return -ENODEV;
  824. sfp->low_dma = sdp->device->host->unchecked_isa_dma;
  825. }
  826. return 0;
  827. case SG_GET_LOW_DMA:
  828. return put_user((int) sfp->low_dma, ip);
  829. case SG_GET_SCSI_ID:
  830. if (!access_ok(VERIFY_WRITE, p, sizeof (sg_scsi_id_t)))
  831. return -EFAULT;
  832. else {
  833. sg_scsi_id_t __user *sg_idp = p;
  834. if (atomic_read(&sdp->detaching))
  835. return -ENODEV;
  836. __put_user((int) sdp->device->host->host_no,
  837. &sg_idp->host_no);
  838. __put_user((int) sdp->device->channel,
  839. &sg_idp->channel);
  840. __put_user((int) sdp->device->id, &sg_idp->scsi_id);
  841. __put_user((int) sdp->device->lun, &sg_idp->lun);
  842. __put_user((int) sdp->device->type, &sg_idp->scsi_type);
  843. __put_user((short) sdp->device->host->cmd_per_lun,
  844. &sg_idp->h_cmd_per_lun);
  845. __put_user((short) sdp->device->queue_depth,
  846. &sg_idp->d_queue_depth);
  847. __put_user(0, &sg_idp->unused[0]);
  848. __put_user(0, &sg_idp->unused[1]);
  849. return 0;
  850. }
  851. case SG_SET_FORCE_PACK_ID:
  852. result = get_user(val, ip);
  853. if (result)
  854. return result;
  855. sfp->force_packid = val ? 1 : 0;
  856. return 0;
  857. case SG_GET_PACK_ID:
  858. if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
  859. return -EFAULT;
  860. read_lock_irqsave(&sfp->rq_list_lock, iflags);
  861. for (srp = sfp->headrp; srp; srp = srp->nextrp) {
  862. if ((1 == srp->done) && (!srp->sg_io_owned)) {
  863. read_unlock_irqrestore(&sfp->rq_list_lock,
  864. iflags);
  865. __put_user(srp->header.pack_id, ip);
  866. return 0;
  867. }
  868. }
  869. read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
  870. __put_user(-1, ip);
  871. return 0;
  872. case SG_GET_NUM_WAITING:
  873. read_lock_irqsave(&sfp->rq_list_lock, iflags);
  874. for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
  875. if ((1 == srp->done) && (!srp->sg_io_owned))
  876. ++val;
  877. }
  878. read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
  879. return put_user(val, ip);
  880. case SG_GET_SG_TABLESIZE:
  881. return put_user(sdp->sg_tablesize, ip);
  882. case SG_SET_RESERVED_SIZE:
  883. result = get_user(val, ip);
  884. if (result)
  885. return result;
  886. if (val < 0)
  887. return -EINVAL;
  888. val = min_t(int, val,
  889. max_sectors_bytes(sdp->device->request_queue));
  890. if (val != sfp->reserve.bufflen) {
  891. if (sg_res_in_use(sfp) || sfp->mmap_called)
  892. return -EBUSY;
  893. sg_remove_scat(sfp, &sfp->reserve);
  894. sg_build_reserve(sfp, val);
  895. }
  896. return 0;
  897. case SG_GET_RESERVED_SIZE:
  898. val = min_t(int, sfp->reserve.bufflen,
  899. max_sectors_bytes(sdp->device->request_queue));
  900. return put_user(val, ip);
  901. case SG_SET_COMMAND_Q:
  902. result = get_user(val, ip);
  903. if (result)
  904. return result;
  905. sfp->cmd_q = val ? 1 : 0;
  906. return 0;
  907. case SG_GET_COMMAND_Q:
  908. return put_user((int) sfp->cmd_q, ip);
  909. case SG_SET_KEEP_ORPHAN:
  910. result = get_user(val, ip);
  911. if (result)
  912. return result;
  913. sfp->keep_orphan = val;
  914. return 0;
  915. case SG_GET_KEEP_ORPHAN:
  916. return put_user((int) sfp->keep_orphan, ip);
  917. case SG_NEXT_CMD_LEN:
  918. result = get_user(val, ip);
  919. if (result)
  920. return result;
  921. sfp->next_cmd_len = (val > 0) ? val : 0;
  922. return 0;
  923. case SG_GET_VERSION_NUM:
  924. return put_user(sg_version_num, ip);
  925. case SG_GET_ACCESS_COUNT:
  926. /* faked - we don't have a real access count anymore */
  927. val = (sdp->device ? 1 : 0);
  928. return put_user(val, ip);
  929. case SG_GET_REQUEST_TABLE:
  930. if (!access_ok(VERIFY_WRITE, p, SZ_SG_REQ_INFO * SG_MAX_QUEUE))
  931. return -EFAULT;
  932. else {
  933. sg_req_info_t *rinfo;
  934. unsigned int ms;
  935. rinfo = kmalloc(SZ_SG_REQ_INFO * SG_MAX_QUEUE,
  936. GFP_KERNEL);
  937. if (!rinfo)
  938. return -ENOMEM;
  939. read_lock_irqsave(&sfp->rq_list_lock, iflags);
  940. for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
  941. ++val, srp = srp ? srp->nextrp : srp) {
  942. memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
  943. if (srp) {
  944. rinfo[val].req_state = srp->done + 1;
  945. rinfo[val].problem =
  946. srp->header.masked_status &
  947. srp->header.host_status &
  948. srp->header.driver_status;
  949. if (srp->done)
  950. rinfo[val].duration =
  951. srp->header.duration;
  952. else {
  953. ms = jiffies_to_msecs(jiffies);
  954. rinfo[val].duration =
  955. (ms > srp->header.duration) ?
  956. (ms - srp->header.duration) : 0;
  957. }
  958. rinfo[val].orphan = srp->orphan;
  959. rinfo[val].sg_io_owned =
  960. srp->sg_io_owned;
  961. rinfo[val].pack_id =
  962. srp->header.pack_id;
  963. rinfo[val].usr_ptr =
  964. srp->header.usr_ptr;
  965. }
  966. }
  967. read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
  968. result = __copy_to_user(p, rinfo,
  969. SZ_SG_REQ_INFO * SG_MAX_QUEUE);
  970. result = result ? -EFAULT : 0;
  971. kfree(rinfo);
  972. return result;
  973. }
  974. case SG_EMULATED_HOST:
  975. if (atomic_read(&sdp->detaching))
  976. return -ENODEV;
  977. return put_user(sdp->device->host->hostt->emulated, ip);
  978. case SCSI_IOCTL_SEND_COMMAND:
  979. if (atomic_read(&sdp->detaching))
  980. return -ENODEV;
  981. if (read_only) {
  982. unsigned char opcode = WRITE_6;
  983. Scsi_Ioctl_Command __user *siocp = p;
  984. if (copy_from_user(&opcode, siocp->data, 1))
  985. return -EFAULT;
  986. if (sg_allow_access(filp, &opcode))
  987. return -EPERM;
  988. }
  989. return sg_scsi_ioctl(sdp->device->request_queue, NULL, filp->f_mode, p);
  990. case SG_SET_DEBUG:
  991. result = get_user(val, ip);
  992. if (result)
  993. return result;
  994. sdp->sgdebug = (char) val;
  995. return 0;
  996. case BLKSECTGET:
  997. return put_user(max_sectors_bytes(sdp->device->request_queue),
  998. ip);
  999. case BLKTRACESETUP:
  1000. return blk_trace_setup(sdp->device->request_queue,
  1001. sdp->disk->disk_name,
  1002. MKDEV(SCSI_GENERIC_MAJOR, sdp->index),
  1003. NULL,
  1004. (char *)arg);
  1005. case BLKTRACESTART:
  1006. return blk_trace_startstop(sdp->device->request_queue, 1);
  1007. case BLKTRACESTOP:
  1008. return blk_trace_startstop(sdp->device->request_queue, 0);
  1009. case BLKTRACETEARDOWN:
  1010. return blk_trace_remove(sdp->device->request_queue);
  1011. case SCSI_IOCTL_GET_IDLUN:
  1012. case SCSI_IOCTL_GET_BUS_NUMBER:
  1013. case SCSI_IOCTL_PROBE_HOST:
  1014. case SG_GET_TRANSFORM:
  1015. case SG_SCSI_RESET:
  1016. if (atomic_read(&sdp->detaching))
  1017. return -ENODEV;
  1018. break;
  1019. default:
  1020. if (read_only)
  1021. return -EPERM; /* don't know so take safe approach */
  1022. break;
  1023. }
  1024. result = scsi_ioctl_block_when_processing_errors(sdp->device,
  1025. cmd_in, filp->f_flags & O_NDELAY);
  1026. if (result)
  1027. return result;
  1028. return scsi_ioctl(sdp->device, cmd_in, p);
  1029. }
  1030. #ifdef CONFIG_COMPAT
  1031. static long sg_compat_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
  1032. {
  1033. Sg_device *sdp;
  1034. Sg_fd *sfp;
  1035. struct scsi_device *sdev;
  1036. if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
  1037. return -ENXIO;
  1038. sdev = sdp->device;
  1039. if (sdev->host->hostt->compat_ioctl) {
  1040. int ret;
  1041. ret = sdev->host->hostt->compat_ioctl(sdev, cmd_in, (void __user *)arg);
  1042. return ret;
  1043. }
  1044. return -ENOIOCTLCMD;
  1045. }
  1046. #endif
  1047. static unsigned int
  1048. sg_poll(struct file *filp, poll_table * wait)
  1049. {
  1050. unsigned int res = 0;
  1051. Sg_device *sdp;
  1052. Sg_fd *sfp;
  1053. Sg_request *srp;
  1054. int count = 0;
  1055. unsigned long iflags;
  1056. sfp = filp->private_data;
  1057. if (!sfp)
  1058. return POLLERR;
  1059. sdp = sfp->parentdp;
  1060. if (!sdp)
  1061. return POLLERR;
  1062. poll_wait(filp, &sfp->read_wait, wait);
  1063. read_lock_irqsave(&sfp->rq_list_lock, iflags);
  1064. for (srp = sfp->headrp; srp; srp = srp->nextrp) {
  1065. /* if any read waiting, flag it */
  1066. if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
  1067. res = POLLIN | POLLRDNORM;
  1068. ++count;
  1069. }
  1070. read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
  1071. if (atomic_read(&sdp->detaching))
  1072. res |= POLLHUP;
  1073. else if (!sfp->cmd_q) {
  1074. if (0 == count)
  1075. res |= POLLOUT | POLLWRNORM;
  1076. } else if (count < SG_MAX_QUEUE)
  1077. res |= POLLOUT | POLLWRNORM;
  1078. SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
  1079. "sg_poll: res=0x%x\n", (int) res));
  1080. return res;
  1081. }
  1082. static int
  1083. sg_fasync(int fd, struct file *filp, int mode)
  1084. {
  1085. Sg_device *sdp;
  1086. Sg_fd *sfp;
  1087. if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
  1088. return -ENXIO;
  1089. SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
  1090. "sg_fasync: mode=%d\n", mode));
  1091. return fasync_helper(fd, filp, mode, &sfp->async_qp);
  1092. }
  1093. static int
  1094. sg_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1095. {
  1096. Sg_fd *sfp;
  1097. unsigned long offset, len, sa;
  1098. Sg_scatter_hold *rsv_schp;
  1099. int k, length;
  1100. if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
  1101. return VM_FAULT_SIGBUS;
  1102. rsv_schp = &sfp->reserve;
  1103. offset = vmf->pgoff << PAGE_SHIFT;
  1104. if (offset >= rsv_schp->bufflen)
  1105. return VM_FAULT_SIGBUS;
  1106. SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sfp->parentdp,
  1107. "sg_vma_fault: offset=%lu, scatg=%d\n",
  1108. offset, rsv_schp->k_use_sg));
  1109. sa = vma->vm_start;
  1110. length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
  1111. for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
  1112. len = vma->vm_end - sa;
  1113. len = (len < length) ? len : length;
  1114. if (offset < len) {
  1115. struct page *page = nth_page(rsv_schp->pages[k],
  1116. offset >> PAGE_SHIFT);
  1117. get_page(page); /* increment page count */
  1118. vmf->page = page;
  1119. return 0; /* success */
  1120. }
  1121. sa += len;
  1122. offset -= len;
  1123. }
  1124. return VM_FAULT_SIGBUS;
  1125. }
  1126. static const struct vm_operations_struct sg_mmap_vm_ops = {
  1127. .fault = sg_vma_fault,
  1128. };
  1129. static int
  1130. sg_mmap(struct file *filp, struct vm_area_struct *vma)
  1131. {
  1132. Sg_fd *sfp;
  1133. unsigned long req_sz, len, sa;
  1134. Sg_scatter_hold *rsv_schp;
  1135. int k, length;
  1136. if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
  1137. return -ENXIO;
  1138. req_sz = vma->vm_end - vma->vm_start;
  1139. SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sfp->parentdp,
  1140. "sg_mmap starting, vm_start=%p, len=%d\n",
  1141. (void *) vma->vm_start, (int) req_sz));
  1142. if (vma->vm_pgoff)
  1143. return -EINVAL; /* want no offset */
  1144. rsv_schp = &sfp->reserve;
  1145. if (req_sz > rsv_schp->bufflen)
  1146. return -ENOMEM; /* cannot map more than reserved buffer */
  1147. sa = vma->vm_start;
  1148. length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
  1149. for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
  1150. len = vma->vm_end - sa;
  1151. len = (len < length) ? len : length;
  1152. sa += len;
  1153. }
  1154. sfp->mmap_called = 1;
  1155. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  1156. vma->vm_private_data = sfp;
  1157. vma->vm_ops = &sg_mmap_vm_ops;
  1158. return 0;
  1159. }
  1160. static void
  1161. sg_rq_end_io_usercontext(struct work_struct *work)
  1162. {
  1163. struct sg_request *srp = container_of(work, struct sg_request, ew.work);
  1164. struct sg_fd *sfp = srp->parentfp;
  1165. sg_finish_rem_req(srp);
  1166. kref_put(&sfp->f_ref, sg_remove_sfp);
  1167. }
  1168. /*
  1169. * This function is a "bottom half" handler that is called by the mid
  1170. * level when a command is completed (or has failed).
  1171. */
  1172. static void
  1173. sg_rq_end_io(struct request *rq, int uptodate)
  1174. {
  1175. struct sg_request *srp = rq->end_io_data;
  1176. Sg_device *sdp;
  1177. Sg_fd *sfp;
  1178. unsigned long iflags;
  1179. unsigned int ms;
  1180. char *sense;
  1181. int result, resid, done = 1;
  1182. if (WARN_ON(srp->done != 0))
  1183. return;
  1184. sfp = srp->parentfp;
  1185. if (WARN_ON(sfp == NULL))
  1186. return;
  1187. sdp = sfp->parentdp;
  1188. if (unlikely(atomic_read(&sdp->detaching)))
  1189. pr_info("%s: device detaching\n", __func__);
  1190. sense = rq->sense;
  1191. result = rq->errors;
  1192. resid = rq->resid_len;
  1193. SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sdp,
  1194. "sg_cmd_done: pack_id=%d, res=0x%x\n",
  1195. srp->header.pack_id, result));
  1196. srp->header.resid = resid;
  1197. ms = jiffies_to_msecs(jiffies);
  1198. srp->header.duration = (ms > srp->header.duration) ?
  1199. (ms - srp->header.duration) : 0;
  1200. if (0 != result) {
  1201. struct scsi_sense_hdr sshdr;
  1202. srp->header.status = 0xff & result;
  1203. srp->header.masked_status = status_byte(result);
  1204. srp->header.msg_status = msg_byte(result);
  1205. srp->header.host_status = host_byte(result);
  1206. srp->header.driver_status = driver_byte(result);
  1207. if ((sdp->sgdebug > 0) &&
  1208. ((CHECK_CONDITION == srp->header.masked_status) ||
  1209. (COMMAND_TERMINATED == srp->header.masked_status)))
  1210. __scsi_print_sense(sdp->device, __func__, sense,
  1211. SCSI_SENSE_BUFFERSIZE);
  1212. /* Following if statement is a patch supplied by Eric Youngdale */
  1213. if (driver_byte(result) != 0
  1214. && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
  1215. && !scsi_sense_is_deferred(&sshdr)
  1216. && sshdr.sense_key == UNIT_ATTENTION
  1217. && sdp->device->removable) {
  1218. /* Detected possible disc change. Set the bit - this */
  1219. /* may be used if there are filesystems using this device */
  1220. sdp->device->changed = 1;
  1221. }
  1222. }
  1223. /* Rely on write phase to clean out srp status values, so no "else" */
  1224. /*
  1225. * Free the request as soon as it is complete so that its resources
  1226. * can be reused without waiting for userspace to read() the
  1227. * result. But keep the associated bio (if any) around until
  1228. * blk_rq_unmap_user() can be called from user context.
  1229. */
  1230. srp->rq = NULL;
  1231. if (rq->cmd != rq->__cmd)
  1232. kfree(rq->cmd);
  1233. __blk_put_request(rq->q, rq);
  1234. write_lock_irqsave(&sfp->rq_list_lock, iflags);
  1235. if (unlikely(srp->orphan)) {
  1236. if (sfp->keep_orphan)
  1237. srp->sg_io_owned = 0;
  1238. else
  1239. done = 0;
  1240. }
  1241. srp->done = done;
  1242. write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
  1243. if (likely(done)) {
  1244. /* Now wake up any sg_read() that is waiting for this
  1245. * packet.
  1246. */
  1247. wake_up_interruptible(&sfp->read_wait);
  1248. kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
  1249. kref_put(&sfp->f_ref, sg_remove_sfp);
  1250. } else {
  1251. INIT_WORK(&srp->ew.work, sg_rq_end_io_usercontext);
  1252. schedule_work(&srp->ew.work);
  1253. }
  1254. }
  1255. static const struct file_operations sg_fops = {
  1256. .owner = THIS_MODULE,
  1257. .read = sg_read,
  1258. .write = sg_write,
  1259. .poll = sg_poll,
  1260. .unlocked_ioctl = sg_ioctl,
  1261. #ifdef CONFIG_COMPAT
  1262. .compat_ioctl = sg_compat_ioctl,
  1263. #endif
  1264. .open = sg_open,
  1265. .mmap = sg_mmap,
  1266. .release = sg_release,
  1267. .fasync = sg_fasync,
  1268. .llseek = no_llseek,
  1269. };
  1270. static struct class *sg_sysfs_class;
  1271. static int sg_sysfs_valid = 0;
  1272. static Sg_device *
  1273. sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
  1274. {
  1275. struct request_queue *q = scsidp->request_queue;
  1276. Sg_device *sdp;
  1277. unsigned long iflags;
  1278. int error;
  1279. u32 k;
  1280. sdp = kzalloc(sizeof(Sg_device), GFP_KERNEL);
  1281. if (!sdp) {
  1282. sdev_printk(KERN_WARNING, scsidp, "%s: kmalloc Sg_device "
  1283. "failure\n", __func__);
  1284. return ERR_PTR(-ENOMEM);
  1285. }
  1286. idr_preload(GFP_KERNEL);
  1287. write_lock_irqsave(&sg_index_lock, iflags);
  1288. error = idr_alloc(&sg_index_idr, sdp, 0, SG_MAX_DEVS, GFP_NOWAIT);
  1289. if (error < 0) {
  1290. if (error == -ENOSPC) {
  1291. sdev_printk(KERN_WARNING, scsidp,
  1292. "Unable to attach sg device type=%d, minor number exceeds %d\n",
  1293. scsidp->type, SG_MAX_DEVS - 1);
  1294. error = -ENODEV;
  1295. } else {
  1296. sdev_printk(KERN_WARNING, scsidp, "%s: idr "
  1297. "allocation Sg_device failure: %d\n",
  1298. __func__, error);
  1299. }
  1300. goto out_unlock;
  1301. }
  1302. k = error;
  1303. SCSI_LOG_TIMEOUT(3, sdev_printk(KERN_INFO, scsidp,
  1304. "sg_alloc: dev=%d \n", k));
  1305. sprintf(disk->disk_name, "sg%d", k);
  1306. disk->first_minor = k;
  1307. sdp->disk = disk;
  1308. sdp->device = scsidp;
  1309. mutex_init(&sdp->open_rel_lock);
  1310. INIT_LIST_HEAD(&sdp->sfds);
  1311. init_waitqueue_head(&sdp->open_wait);
  1312. atomic_set(&sdp->detaching, 0);
  1313. rwlock_init(&sdp->sfd_lock);
  1314. sdp->sg_tablesize = queue_max_segments(q);
  1315. sdp->index = k;
  1316. kref_init(&sdp->d_ref);
  1317. error = 0;
  1318. out_unlock:
  1319. write_unlock_irqrestore(&sg_index_lock, iflags);
  1320. idr_preload_end();
  1321. if (error) {
  1322. kfree(sdp);
  1323. return ERR_PTR(error);
  1324. }
  1325. return sdp;
  1326. }
  1327. static int
  1328. sg_add_device(struct device *cl_dev, struct class_interface *cl_intf)
  1329. {
  1330. struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
  1331. struct gendisk *disk;
  1332. Sg_device *sdp = NULL;
  1333. struct cdev * cdev = NULL;
  1334. int error;
  1335. unsigned long iflags;
  1336. disk = alloc_disk(1);
  1337. if (!disk) {
  1338. pr_warn("%s: alloc_disk failed\n", __func__);
  1339. return -ENOMEM;
  1340. }
  1341. disk->major = SCSI_GENERIC_MAJOR;
  1342. error = -ENOMEM;
  1343. cdev = cdev_alloc();
  1344. if (!cdev) {
  1345. pr_warn("%s: cdev_alloc failed\n", __func__);
  1346. goto out;
  1347. }
  1348. cdev->owner = THIS_MODULE;
  1349. cdev->ops = &sg_fops;
  1350. sdp = sg_alloc(disk, scsidp);
  1351. if (IS_ERR(sdp)) {
  1352. pr_warn("%s: sg_alloc failed\n", __func__);
  1353. error = PTR_ERR(sdp);
  1354. goto out;
  1355. }
  1356. error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
  1357. if (error)
  1358. goto cdev_add_err;
  1359. sdp->cdev = cdev;
  1360. if (sg_sysfs_valid) {
  1361. struct device *sg_class_member;
  1362. sg_class_member = device_create(sg_sysfs_class, cl_dev->parent,
  1363. MKDEV(SCSI_GENERIC_MAJOR,
  1364. sdp->index),
  1365. sdp, "%s", disk->disk_name);
  1366. if (IS_ERR(sg_class_member)) {
  1367. pr_err("%s: device_create failed\n", __func__);
  1368. error = PTR_ERR(sg_class_member);
  1369. goto cdev_add_err;
  1370. }
  1371. error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
  1372. &sg_class_member->kobj, "generic");
  1373. if (error)
  1374. pr_err("%s: unable to make symlink 'generic' back "
  1375. "to sg%d\n", __func__, sdp->index);
  1376. } else
  1377. pr_warn("%s: sg_sys Invalid\n", __func__);
  1378. sdev_printk(KERN_NOTICE, scsidp, "Attached scsi generic sg%d "
  1379. "type %d\n", sdp->index, scsidp->type);
  1380. dev_set_drvdata(cl_dev, sdp);
  1381. return 0;
  1382. cdev_add_err:
  1383. write_lock_irqsave(&sg_index_lock, iflags);
  1384. idr_remove(&sg_index_idr, sdp->index);
  1385. write_unlock_irqrestore(&sg_index_lock, iflags);
  1386. kfree(sdp);
  1387. out:
  1388. put_disk(disk);
  1389. if (cdev)
  1390. cdev_del(cdev);
  1391. return error;
  1392. }
  1393. static void
  1394. sg_device_destroy(struct kref *kref)
  1395. {
  1396. struct sg_device *sdp = container_of(kref, struct sg_device, d_ref);
  1397. unsigned long flags;
  1398. /* CAUTION! Note that the device can still be found via idr_find()
  1399. * even though the refcount is 0. Therefore, do idr_remove() BEFORE
  1400. * any other cleanup.
  1401. */
  1402. write_lock_irqsave(&sg_index_lock, flags);
  1403. idr_remove(&sg_index_idr, sdp->index);
  1404. write_unlock_irqrestore(&sg_index_lock, flags);
  1405. SCSI_LOG_TIMEOUT(3,
  1406. sg_printk(KERN_INFO, sdp, "sg_device_destroy\n"));
  1407. put_disk(sdp->disk);
  1408. kfree(sdp);
  1409. }
  1410. static void
  1411. sg_remove_device(struct device *cl_dev, struct class_interface *cl_intf)
  1412. {
  1413. struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
  1414. Sg_device *sdp = dev_get_drvdata(cl_dev);
  1415. unsigned long iflags;
  1416. Sg_fd *sfp;
  1417. int val;
  1418. if (!sdp)
  1419. return;
  1420. /* want sdp->detaching non-zero as soon as possible */
  1421. val = atomic_inc_return(&sdp->detaching);
  1422. if (val > 1)
  1423. return; /* only want to do following once per device */
  1424. SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
  1425. "%s\n", __func__));
  1426. read_lock_irqsave(&sdp->sfd_lock, iflags);
  1427. list_for_each_entry(sfp, &sdp->sfds, sfd_siblings) {
  1428. wake_up_interruptible_all(&sfp->read_wait);
  1429. kill_fasync(&sfp->async_qp, SIGPOLL, POLL_HUP);
  1430. }
  1431. wake_up_interruptible_all(&sdp->open_wait);
  1432. read_unlock_irqrestore(&sdp->sfd_lock, iflags);
  1433. sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
  1434. device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
  1435. cdev_del(sdp->cdev);
  1436. sdp->cdev = NULL;
  1437. kref_put(&sdp->d_ref, sg_device_destroy);
  1438. }
  1439. module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
  1440. module_param_named(def_reserved_size, def_reserved_size, int,
  1441. S_IRUGO | S_IWUSR);
  1442. module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
  1443. MODULE_AUTHOR("Douglas Gilbert");
  1444. MODULE_DESCRIPTION("SCSI generic (sg) driver");
  1445. MODULE_LICENSE("GPL");
  1446. MODULE_VERSION(SG_VERSION_STR);
  1447. MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
  1448. MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
  1449. "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
  1450. MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
  1451. MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
  1452. static int __init
  1453. init_sg(void)
  1454. {
  1455. int rc;
  1456. if (scatter_elem_sz < PAGE_SIZE) {
  1457. scatter_elem_sz = PAGE_SIZE;
  1458. scatter_elem_sz_prev = scatter_elem_sz;
  1459. }
  1460. if (def_reserved_size >= 0)
  1461. sg_big_buff = def_reserved_size;
  1462. else
  1463. def_reserved_size = sg_big_buff;
  1464. rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
  1465. SG_MAX_DEVS, "sg");
  1466. if (rc)
  1467. return rc;
  1468. sg_sysfs_class = class_create(THIS_MODULE, "scsi_generic");
  1469. if ( IS_ERR(sg_sysfs_class) ) {
  1470. rc = PTR_ERR(sg_sysfs_class);
  1471. goto err_out;
  1472. }
  1473. sg_sysfs_valid = 1;
  1474. rc = scsi_register_interface(&sg_interface);
  1475. if (0 == rc) {
  1476. #ifdef CONFIG_SCSI_PROC_FS
  1477. sg_proc_init();
  1478. #endif /* CONFIG_SCSI_PROC_FS */
  1479. return 0;
  1480. }
  1481. class_destroy(sg_sysfs_class);
  1482. err_out:
  1483. unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
  1484. return rc;
  1485. }
  1486. static void __exit
  1487. exit_sg(void)
  1488. {
  1489. #ifdef CONFIG_SCSI_PROC_FS
  1490. sg_proc_cleanup();
  1491. #endif /* CONFIG_SCSI_PROC_FS */
  1492. scsi_unregister_interface(&sg_interface);
  1493. class_destroy(sg_sysfs_class);
  1494. sg_sysfs_valid = 0;
  1495. unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
  1496. SG_MAX_DEVS);
  1497. idr_destroy(&sg_index_idr);
  1498. }
  1499. static int
  1500. sg_start_req(Sg_request *srp, unsigned char *cmd)
  1501. {
  1502. int res;
  1503. struct request *rq;
  1504. Sg_fd *sfp = srp->parentfp;
  1505. sg_io_hdr_t *hp = &srp->header;
  1506. int dxfer_len = (int) hp->dxfer_len;
  1507. int dxfer_dir = hp->dxfer_direction;
  1508. unsigned int iov_count = hp->iovec_count;
  1509. Sg_scatter_hold *req_schp = &srp->data;
  1510. Sg_scatter_hold *rsv_schp = &sfp->reserve;
  1511. struct request_queue *q = sfp->parentdp->device->request_queue;
  1512. struct rq_map_data *md, map_data;
  1513. int rw = hp->dxfer_direction == SG_DXFER_TO_DEV ? WRITE : READ;
  1514. unsigned char *long_cmdp = NULL;
  1515. SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
  1516. "sg_start_req: dxfer_len=%d\n",
  1517. dxfer_len));
  1518. if (hp->cmd_len > BLK_MAX_CDB) {
  1519. long_cmdp = kzalloc(hp->cmd_len, GFP_KERNEL);
  1520. if (!long_cmdp)
  1521. return -ENOMEM;
  1522. }
  1523. /*
  1524. * NOTE
  1525. *
  1526. * With scsi-mq enabled, there are a fixed number of preallocated
  1527. * requests equal in number to shost->can_queue. If all of the
  1528. * preallocated requests are already in use, then using GFP_ATOMIC with
  1529. * blk_get_request() will return -EWOULDBLOCK, whereas using GFP_KERNEL
  1530. * will cause blk_get_request() to sleep until an active command
  1531. * completes, freeing up a request. Neither option is ideal, but
  1532. * GFP_KERNEL is the better choice to prevent userspace from getting an
  1533. * unexpected EWOULDBLOCK.
  1534. *
  1535. * With scsi-mq disabled, blk_get_request() with GFP_KERNEL usually
  1536. * does not sleep except under memory pressure.
  1537. */
  1538. rq = blk_get_request(q, rw, GFP_KERNEL);
  1539. if (IS_ERR(rq)) {
  1540. kfree(long_cmdp);
  1541. return PTR_ERR(rq);
  1542. }
  1543. blk_rq_set_block_pc(rq);
  1544. if (hp->cmd_len > BLK_MAX_CDB)
  1545. rq->cmd = long_cmdp;
  1546. memcpy(rq->cmd, cmd, hp->cmd_len);
  1547. rq->cmd_len = hp->cmd_len;
  1548. srp->rq = rq;
  1549. rq->end_io_data = srp;
  1550. rq->sense = srp->sense_b;
  1551. rq->retries = SG_DEFAULT_RETRIES;
  1552. if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
  1553. return 0;
  1554. if (sg_allow_dio && hp->flags & SG_FLAG_DIRECT_IO &&
  1555. dxfer_dir != SG_DXFER_UNKNOWN && !iov_count &&
  1556. !sfp->parentdp->device->host->unchecked_isa_dma &&
  1557. blk_rq_aligned(q, (unsigned long)hp->dxferp, dxfer_len))
  1558. md = NULL;
  1559. else
  1560. md = &map_data;
  1561. if (md) {
  1562. if (!sg_res_in_use(sfp) && dxfer_len <= rsv_schp->bufflen)
  1563. sg_link_reserve(sfp, srp, dxfer_len);
  1564. else {
  1565. res = sg_build_indirect(req_schp, sfp, dxfer_len);
  1566. if (res)
  1567. return res;
  1568. }
  1569. md->pages = req_schp->pages;
  1570. md->page_order = req_schp->page_order;
  1571. md->nr_entries = req_schp->k_use_sg;
  1572. md->offset = 0;
  1573. md->null_mapped = hp->dxferp ? 0 : 1;
  1574. if (dxfer_dir == SG_DXFER_TO_FROM_DEV)
  1575. md->from_user = 1;
  1576. else
  1577. md->from_user = 0;
  1578. }
  1579. if (iov_count) {
  1580. struct iovec *iov = NULL;
  1581. struct iov_iter i;
  1582. res = import_iovec(rw, hp->dxferp, iov_count, 0, &iov, &i);
  1583. if (res < 0)
  1584. return res;
  1585. iov_iter_truncate(&i, hp->dxfer_len);
  1586. res = blk_rq_map_user_iov(q, rq, md, &i, GFP_ATOMIC);
  1587. kfree(iov);
  1588. } else
  1589. res = blk_rq_map_user(q, rq, md, hp->dxferp,
  1590. hp->dxfer_len, GFP_ATOMIC);
  1591. if (!res) {
  1592. srp->bio = rq->bio;
  1593. if (!md) {
  1594. req_schp->dio_in_use = 1;
  1595. hp->info |= SG_INFO_DIRECT_IO;
  1596. }
  1597. }
  1598. return res;
  1599. }
  1600. static int
  1601. sg_finish_rem_req(Sg_request *srp)
  1602. {
  1603. int ret = 0;
  1604. Sg_fd *sfp = srp->parentfp;
  1605. Sg_scatter_hold *req_schp = &srp->data;
  1606. SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
  1607. "sg_finish_rem_req: res_used=%d\n",
  1608. (int) srp->res_used));
  1609. if (srp->bio)
  1610. ret = blk_rq_unmap_user(srp->bio);
  1611. if (srp->rq) {
  1612. if (srp->rq->cmd != srp->rq->__cmd)
  1613. kfree(srp->rq->cmd);
  1614. blk_put_request(srp->rq);
  1615. }
  1616. if (srp->res_used)
  1617. sg_unlink_reserve(sfp, srp);
  1618. else
  1619. sg_remove_scat(sfp, req_schp);
  1620. sg_remove_request(sfp, srp);
  1621. return ret;
  1622. }
  1623. static int
  1624. sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
  1625. {
  1626. int sg_bufflen = tablesize * sizeof(struct page *);
  1627. gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
  1628. schp->pages = kzalloc(sg_bufflen, gfp_flags);
  1629. if (!schp->pages)
  1630. return -ENOMEM;
  1631. schp->sglist_len = sg_bufflen;
  1632. return tablesize; /* number of scat_gath elements allocated */
  1633. }
  1634. static int
  1635. sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
  1636. {
  1637. int ret_sz = 0, i, k, rem_sz, num, mx_sc_elems;
  1638. int sg_tablesize = sfp->parentdp->sg_tablesize;
  1639. int blk_size = buff_size, order;
  1640. gfp_t gfp_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN;
  1641. if (blk_size < 0)
  1642. return -EFAULT;
  1643. if (0 == blk_size)
  1644. ++blk_size; /* don't know why */
  1645. /* round request up to next highest SG_SECTOR_SZ byte boundary */
  1646. blk_size = ALIGN(blk_size, SG_SECTOR_SZ);
  1647. SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
  1648. "sg_build_indirect: buff_size=%d, blk_size=%d\n",
  1649. buff_size, blk_size));
  1650. /* N.B. ret_sz carried into this block ... */
  1651. mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
  1652. if (mx_sc_elems < 0)
  1653. return mx_sc_elems; /* most likely -ENOMEM */
  1654. num = scatter_elem_sz;
  1655. if (unlikely(num != scatter_elem_sz_prev)) {
  1656. if (num < PAGE_SIZE) {
  1657. scatter_elem_sz = PAGE_SIZE;
  1658. scatter_elem_sz_prev = PAGE_SIZE;
  1659. } else
  1660. scatter_elem_sz_prev = num;
  1661. }
  1662. if (sfp->low_dma)
  1663. gfp_mask |= GFP_DMA;
  1664. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  1665. gfp_mask |= __GFP_ZERO;
  1666. order = get_order(num);
  1667. retry:
  1668. ret_sz = 1 << (PAGE_SHIFT + order);
  1669. for (k = 0, rem_sz = blk_size; rem_sz > 0 && k < mx_sc_elems;
  1670. k++, rem_sz -= ret_sz) {
  1671. num = (rem_sz > scatter_elem_sz_prev) ?
  1672. scatter_elem_sz_prev : rem_sz;
  1673. schp->pages[k] = alloc_pages(gfp_mask, order);
  1674. if (!schp->pages[k])
  1675. goto out;
  1676. if (num == scatter_elem_sz_prev) {
  1677. if (unlikely(ret_sz > scatter_elem_sz_prev)) {
  1678. scatter_elem_sz = ret_sz;
  1679. scatter_elem_sz_prev = ret_sz;
  1680. }
  1681. }
  1682. SCSI_LOG_TIMEOUT(5, sg_printk(KERN_INFO, sfp->parentdp,
  1683. "sg_build_indirect: k=%d, num=%d, ret_sz=%d\n",
  1684. k, num, ret_sz));
  1685. } /* end of for loop */
  1686. schp->page_order = order;
  1687. schp->k_use_sg = k;
  1688. SCSI_LOG_TIMEOUT(5, sg_printk(KERN_INFO, sfp->parentdp,
  1689. "sg_build_indirect: k_use_sg=%d, rem_sz=%d\n",
  1690. k, rem_sz));
  1691. schp->bufflen = blk_size;
  1692. if (rem_sz > 0) /* must have failed */
  1693. return -ENOMEM;
  1694. return 0;
  1695. out:
  1696. for (i = 0; i < k; i++)
  1697. __free_pages(schp->pages[i], order);
  1698. if (--order >= 0)
  1699. goto retry;
  1700. return -ENOMEM;
  1701. }
  1702. static void
  1703. sg_remove_scat(Sg_fd * sfp, Sg_scatter_hold * schp)
  1704. {
  1705. SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
  1706. "sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
  1707. if (schp->pages && schp->sglist_len > 0) {
  1708. if (!schp->dio_in_use) {
  1709. int k;
  1710. for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
  1711. SCSI_LOG_TIMEOUT(5,
  1712. sg_printk(KERN_INFO, sfp->parentdp,
  1713. "sg_remove_scat: k=%d, pg=0x%p\n",
  1714. k, schp->pages[k]));
  1715. __free_pages(schp->pages[k], schp->page_order);
  1716. }
  1717. kfree(schp->pages);
  1718. }
  1719. }
  1720. memset(schp, 0, sizeof (*schp));
  1721. }
  1722. static int
  1723. sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
  1724. {
  1725. Sg_scatter_hold *schp = &srp->data;
  1726. int k, num;
  1727. SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, srp->parentfp->parentdp,
  1728. "sg_read_oxfer: num_read_xfer=%d\n",
  1729. num_read_xfer));
  1730. if ((!outp) || (num_read_xfer <= 0))
  1731. return 0;
  1732. num = 1 << (PAGE_SHIFT + schp->page_order);
  1733. for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
  1734. if (num > num_read_xfer) {
  1735. if (__copy_to_user(outp, page_address(schp->pages[k]),
  1736. num_read_xfer))
  1737. return -EFAULT;
  1738. break;
  1739. } else {
  1740. if (__copy_to_user(outp, page_address(schp->pages[k]),
  1741. num))
  1742. return -EFAULT;
  1743. num_read_xfer -= num;
  1744. if (num_read_xfer <= 0)
  1745. break;
  1746. outp += num;
  1747. }
  1748. }
  1749. return 0;
  1750. }
  1751. static void
  1752. sg_build_reserve(Sg_fd * sfp, int req_size)
  1753. {
  1754. Sg_scatter_hold *schp = &sfp->reserve;
  1755. SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
  1756. "sg_build_reserve: req_size=%d\n", req_size));
  1757. do {
  1758. if (req_size < PAGE_SIZE)
  1759. req_size = PAGE_SIZE;
  1760. if (0 == sg_build_indirect(schp, sfp, req_size))
  1761. return;
  1762. else
  1763. sg_remove_scat(sfp, schp);
  1764. req_size >>= 1; /* divide by 2 */
  1765. } while (req_size > (PAGE_SIZE / 2));
  1766. }
  1767. static void
  1768. sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
  1769. {
  1770. Sg_scatter_hold *req_schp = &srp->data;
  1771. Sg_scatter_hold *rsv_schp = &sfp->reserve;
  1772. int k, num, rem;
  1773. srp->res_used = 1;
  1774. SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
  1775. "sg_link_reserve: size=%d\n", size));
  1776. rem = size;
  1777. num = 1 << (PAGE_SHIFT + rsv_schp->page_order);
  1778. for (k = 0; k < rsv_schp->k_use_sg; k++) {
  1779. if (rem <= num) {
  1780. req_schp->k_use_sg = k + 1;
  1781. req_schp->sglist_len = rsv_schp->sglist_len;
  1782. req_schp->pages = rsv_schp->pages;
  1783. req_schp->bufflen = size;
  1784. req_schp->page_order = rsv_schp->page_order;
  1785. break;
  1786. } else
  1787. rem -= num;
  1788. }
  1789. if (k >= rsv_schp->k_use_sg)
  1790. SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO, sfp->parentdp,
  1791. "sg_link_reserve: BAD size\n"));
  1792. }
  1793. static void
  1794. sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
  1795. {
  1796. Sg_scatter_hold *req_schp = &srp->data;
  1797. SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, srp->parentfp->parentdp,
  1798. "sg_unlink_reserve: req->k_use_sg=%d\n",
  1799. (int) req_schp->k_use_sg));
  1800. req_schp->k_use_sg = 0;
  1801. req_schp->bufflen = 0;
  1802. req_schp->pages = NULL;
  1803. req_schp->page_order = 0;
  1804. req_schp->sglist_len = 0;
  1805. sfp->save_scat_len = 0;
  1806. srp->res_used = 0;
  1807. }
  1808. static Sg_request *
  1809. sg_get_rq_mark(Sg_fd * sfp, int pack_id)
  1810. {
  1811. Sg_request *resp;
  1812. unsigned long iflags;
  1813. write_lock_irqsave(&sfp->rq_list_lock, iflags);
  1814. for (resp = sfp->headrp; resp; resp = resp->nextrp) {
  1815. /* look for requests that are ready + not SG_IO owned */
  1816. if ((1 == resp->done) && (!resp->sg_io_owned) &&
  1817. ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
  1818. resp->done = 2; /* guard against other readers */
  1819. break;
  1820. }
  1821. }
  1822. write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
  1823. return resp;
  1824. }
  1825. /* always adds to end of list */
  1826. static Sg_request *
  1827. sg_add_request(Sg_fd * sfp)
  1828. {
  1829. int k;
  1830. unsigned long iflags;
  1831. Sg_request *resp;
  1832. Sg_request *rp = sfp->req_arr;
  1833. write_lock_irqsave(&sfp->rq_list_lock, iflags);
  1834. resp = sfp->headrp;
  1835. if (!resp) {
  1836. memset(rp, 0, sizeof (Sg_request));
  1837. rp->parentfp = sfp;
  1838. resp = rp;
  1839. sfp->headrp = resp;
  1840. } else {
  1841. if (0 == sfp->cmd_q)
  1842. resp = NULL; /* command queuing disallowed */
  1843. else {
  1844. for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
  1845. if (!rp->parentfp)
  1846. break;
  1847. }
  1848. if (k < SG_MAX_QUEUE) {
  1849. memset(rp, 0, sizeof (Sg_request));
  1850. rp->parentfp = sfp;
  1851. while (resp->nextrp)
  1852. resp = resp->nextrp;
  1853. resp->nextrp = rp;
  1854. resp = rp;
  1855. } else
  1856. resp = NULL;
  1857. }
  1858. }
  1859. if (resp) {
  1860. resp->nextrp = NULL;
  1861. resp->header.duration = jiffies_to_msecs(jiffies);
  1862. }
  1863. write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
  1864. return resp;
  1865. }
  1866. /* Return of 1 for found; 0 for not found */
  1867. static int
  1868. sg_remove_request(Sg_fd * sfp, Sg_request * srp)
  1869. {
  1870. Sg_request *prev_rp;
  1871. Sg_request *rp;
  1872. unsigned long iflags;
  1873. int res = 0;
  1874. if ((!sfp) || (!srp) || (!sfp->headrp))
  1875. return res;
  1876. write_lock_irqsave(&sfp->rq_list_lock, iflags);
  1877. prev_rp = sfp->headrp;
  1878. if (srp == prev_rp) {
  1879. sfp->headrp = prev_rp->nextrp;
  1880. prev_rp->parentfp = NULL;
  1881. res = 1;
  1882. } else {
  1883. while ((rp = prev_rp->nextrp)) {
  1884. if (srp == rp) {
  1885. prev_rp->nextrp = rp->nextrp;
  1886. rp->parentfp = NULL;
  1887. res = 1;
  1888. break;
  1889. }
  1890. prev_rp = rp;
  1891. }
  1892. }
  1893. write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
  1894. return res;
  1895. }
  1896. static Sg_fd *
  1897. sg_add_sfp(Sg_device * sdp)
  1898. {
  1899. Sg_fd *sfp;
  1900. unsigned long iflags;
  1901. int bufflen;
  1902. sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
  1903. if (!sfp)
  1904. return ERR_PTR(-ENOMEM);
  1905. init_waitqueue_head(&sfp->read_wait);
  1906. rwlock_init(&sfp->rq_list_lock);
  1907. kref_init(&sfp->f_ref);
  1908. sfp->timeout = SG_DEFAULT_TIMEOUT;
  1909. sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
  1910. sfp->force_packid = SG_DEF_FORCE_PACK_ID;
  1911. sfp->low_dma = (SG_DEF_FORCE_LOW_DMA == 0) ?
  1912. sdp->device->host->unchecked_isa_dma : 1;
  1913. sfp->cmd_q = SG_DEF_COMMAND_Q;
  1914. sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
  1915. sfp->parentdp = sdp;
  1916. write_lock_irqsave(&sdp->sfd_lock, iflags);
  1917. if (atomic_read(&sdp->detaching)) {
  1918. write_unlock_irqrestore(&sdp->sfd_lock, iflags);
  1919. return ERR_PTR(-ENODEV);
  1920. }
  1921. list_add_tail(&sfp->sfd_siblings, &sdp->sfds);
  1922. write_unlock_irqrestore(&sdp->sfd_lock, iflags);
  1923. SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
  1924. "sg_add_sfp: sfp=0x%p\n", sfp));
  1925. if (unlikely(sg_big_buff != def_reserved_size))
  1926. sg_big_buff = def_reserved_size;
  1927. bufflen = min_t(int, sg_big_buff,
  1928. max_sectors_bytes(sdp->device->request_queue));
  1929. sg_build_reserve(sfp, bufflen);
  1930. SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
  1931. "sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
  1932. sfp->reserve.bufflen,
  1933. sfp->reserve.k_use_sg));
  1934. kref_get(&sdp->d_ref);
  1935. __module_get(THIS_MODULE);
  1936. return sfp;
  1937. }
  1938. static void
  1939. sg_remove_sfp_usercontext(struct work_struct *work)
  1940. {
  1941. struct sg_fd *sfp = container_of(work, struct sg_fd, ew.work);
  1942. struct sg_device *sdp = sfp->parentdp;
  1943. /* Cleanup any responses which were never read(). */
  1944. while (sfp->headrp)
  1945. sg_finish_rem_req(sfp->headrp);
  1946. if (sfp->reserve.bufflen > 0) {
  1947. SCSI_LOG_TIMEOUT(6, sg_printk(KERN_INFO, sdp,
  1948. "sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
  1949. (int) sfp->reserve.bufflen,
  1950. (int) sfp->reserve.k_use_sg));
  1951. sg_remove_scat(sfp, &sfp->reserve);
  1952. }
  1953. SCSI_LOG_TIMEOUT(6, sg_printk(KERN_INFO, sdp,
  1954. "sg_remove_sfp: sfp=0x%p\n", sfp));
  1955. kfree(sfp);
  1956. scsi_device_put(sdp->device);
  1957. kref_put(&sdp->d_ref, sg_device_destroy);
  1958. module_put(THIS_MODULE);
  1959. }
  1960. static void
  1961. sg_remove_sfp(struct kref *kref)
  1962. {
  1963. struct sg_fd *sfp = container_of(kref, struct sg_fd, f_ref);
  1964. struct sg_device *sdp = sfp->parentdp;
  1965. unsigned long iflags;
  1966. write_lock_irqsave(&sdp->sfd_lock, iflags);
  1967. list_del(&sfp->sfd_siblings);
  1968. write_unlock_irqrestore(&sdp->sfd_lock, iflags);
  1969. INIT_WORK(&sfp->ew.work, sg_remove_sfp_usercontext);
  1970. schedule_work(&sfp->ew.work);
  1971. }
  1972. static int
  1973. sg_res_in_use(Sg_fd * sfp)
  1974. {
  1975. const Sg_request *srp;
  1976. unsigned long iflags;
  1977. read_lock_irqsave(&sfp->rq_list_lock, iflags);
  1978. for (srp = sfp->headrp; srp; srp = srp->nextrp)
  1979. if (srp->res_used)
  1980. break;
  1981. read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
  1982. return srp ? 1 : 0;
  1983. }
  1984. #ifdef CONFIG_SCSI_PROC_FS
  1985. static int
  1986. sg_idr_max_id(int id, void *p, void *data)
  1987. {
  1988. int *k = data;
  1989. if (*k < id)
  1990. *k = id;
  1991. return 0;
  1992. }
  1993. static int
  1994. sg_last_dev(void)
  1995. {
  1996. int k = -1;
  1997. unsigned long iflags;
  1998. read_lock_irqsave(&sg_index_lock, iflags);
  1999. idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
  2000. read_unlock_irqrestore(&sg_index_lock, iflags);
  2001. return k + 1; /* origin 1 */
  2002. }
  2003. #endif
  2004. /* must be called with sg_index_lock held */
  2005. static Sg_device *sg_lookup_dev(int dev)
  2006. {
  2007. return idr_find(&sg_index_idr, dev);
  2008. }
  2009. static Sg_device *
  2010. sg_get_dev(int dev)
  2011. {
  2012. struct sg_device *sdp;
  2013. unsigned long flags;
  2014. read_lock_irqsave(&sg_index_lock, flags);
  2015. sdp = sg_lookup_dev(dev);
  2016. if (!sdp)
  2017. sdp = ERR_PTR(-ENXIO);
  2018. else if (atomic_read(&sdp->detaching)) {
  2019. /* If sdp->detaching, then the refcount may already be 0, in
  2020. * which case it would be a bug to do kref_get().
  2021. */
  2022. sdp = ERR_PTR(-ENODEV);
  2023. } else
  2024. kref_get(&sdp->d_ref);
  2025. read_unlock_irqrestore(&sg_index_lock, flags);
  2026. return sdp;
  2027. }
  2028. #ifdef CONFIG_SCSI_PROC_FS
  2029. static struct proc_dir_entry *sg_proc_sgp = NULL;
  2030. static char sg_proc_sg_dirname[] = "scsi/sg";
  2031. static int sg_proc_seq_show_int(struct seq_file *s, void *v);
  2032. static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
  2033. static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
  2034. size_t count, loff_t *off);
  2035. static const struct file_operations adio_fops = {
  2036. .owner = THIS_MODULE,
  2037. .open = sg_proc_single_open_adio,
  2038. .read = seq_read,
  2039. .llseek = seq_lseek,
  2040. .write = sg_proc_write_adio,
  2041. .release = single_release,
  2042. };
  2043. static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
  2044. static ssize_t sg_proc_write_dressz(struct file *filp,
  2045. const char __user *buffer, size_t count, loff_t *off);
  2046. static const struct file_operations dressz_fops = {
  2047. .owner = THIS_MODULE,
  2048. .open = sg_proc_single_open_dressz,
  2049. .read = seq_read,
  2050. .llseek = seq_lseek,
  2051. .write = sg_proc_write_dressz,
  2052. .release = single_release,
  2053. };
  2054. static int sg_proc_seq_show_version(struct seq_file *s, void *v);
  2055. static int sg_proc_single_open_version(struct inode *inode, struct file *file);
  2056. static const struct file_operations version_fops = {
  2057. .owner = THIS_MODULE,
  2058. .open = sg_proc_single_open_version,
  2059. .read = seq_read,
  2060. .llseek = seq_lseek,
  2061. .release = single_release,
  2062. };
  2063. static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
  2064. static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file);
  2065. static const struct file_operations devhdr_fops = {
  2066. .owner = THIS_MODULE,
  2067. .open = sg_proc_single_open_devhdr,
  2068. .read = seq_read,
  2069. .llseek = seq_lseek,
  2070. .release = single_release,
  2071. };
  2072. static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
  2073. static int sg_proc_open_dev(struct inode *inode, struct file *file);
  2074. static void * dev_seq_start(struct seq_file *s, loff_t *pos);
  2075. static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
  2076. static void dev_seq_stop(struct seq_file *s, void *v);
  2077. static const struct file_operations dev_fops = {
  2078. .owner = THIS_MODULE,
  2079. .open = sg_proc_open_dev,
  2080. .read = seq_read,
  2081. .llseek = seq_lseek,
  2082. .release = seq_release,
  2083. };
  2084. static const struct seq_operations dev_seq_ops = {
  2085. .start = dev_seq_start,
  2086. .next = dev_seq_next,
  2087. .stop = dev_seq_stop,
  2088. .show = sg_proc_seq_show_dev,
  2089. };
  2090. static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
  2091. static int sg_proc_open_devstrs(struct inode *inode, struct file *file);
  2092. static const struct file_operations devstrs_fops = {
  2093. .owner = THIS_MODULE,
  2094. .open = sg_proc_open_devstrs,
  2095. .read = seq_read,
  2096. .llseek = seq_lseek,
  2097. .release = seq_release,
  2098. };
  2099. static const struct seq_operations devstrs_seq_ops = {
  2100. .start = dev_seq_start,
  2101. .next = dev_seq_next,
  2102. .stop = dev_seq_stop,
  2103. .show = sg_proc_seq_show_devstrs,
  2104. };
  2105. static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
  2106. static int sg_proc_open_debug(struct inode *inode, struct file *file);
  2107. static const struct file_operations debug_fops = {
  2108. .owner = THIS_MODULE,
  2109. .open = sg_proc_open_debug,
  2110. .read = seq_read,
  2111. .llseek = seq_lseek,
  2112. .release = seq_release,
  2113. };
  2114. static const struct seq_operations debug_seq_ops = {
  2115. .start = dev_seq_start,
  2116. .next = dev_seq_next,
  2117. .stop = dev_seq_stop,
  2118. .show = sg_proc_seq_show_debug,
  2119. };
  2120. struct sg_proc_leaf {
  2121. const char * name;
  2122. const struct file_operations * fops;
  2123. };
  2124. static const struct sg_proc_leaf sg_proc_leaf_arr[] = {
  2125. {"allow_dio", &adio_fops},
  2126. {"debug", &debug_fops},
  2127. {"def_reserved_size", &dressz_fops},
  2128. {"device_hdr", &devhdr_fops},
  2129. {"devices", &dev_fops},
  2130. {"device_strs", &devstrs_fops},
  2131. {"version", &version_fops}
  2132. };
  2133. static int
  2134. sg_proc_init(void)
  2135. {
  2136. int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
  2137. int k;
  2138. sg_proc_sgp = proc_mkdir(sg_proc_sg_dirname, NULL);
  2139. if (!sg_proc_sgp)
  2140. return 1;
  2141. for (k = 0; k < num_leaves; ++k) {
  2142. const struct sg_proc_leaf *leaf = &sg_proc_leaf_arr[k];
  2143. umode_t mask = leaf->fops->write ? S_IRUGO | S_IWUSR : S_IRUGO;
  2144. proc_create(leaf->name, mask, sg_proc_sgp, leaf->fops);
  2145. }
  2146. return 0;
  2147. }
  2148. static void
  2149. sg_proc_cleanup(void)
  2150. {
  2151. int k;
  2152. int num_leaves = ARRAY_SIZE(sg_proc_leaf_arr);
  2153. if (!sg_proc_sgp)
  2154. return;
  2155. for (k = 0; k < num_leaves; ++k)
  2156. remove_proc_entry(sg_proc_leaf_arr[k].name, sg_proc_sgp);
  2157. remove_proc_entry(sg_proc_sg_dirname, NULL);
  2158. }
  2159. static int sg_proc_seq_show_int(struct seq_file *s, void *v)
  2160. {
  2161. seq_printf(s, "%d\n", *((int *)s->private));
  2162. return 0;
  2163. }
  2164. static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
  2165. {
  2166. return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
  2167. }
  2168. static ssize_t
  2169. sg_proc_write_adio(struct file *filp, const char __user *buffer,
  2170. size_t count, loff_t *off)
  2171. {
  2172. int err;
  2173. unsigned long num;
  2174. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  2175. return -EACCES;
  2176. err = kstrtoul_from_user(buffer, count, 0, &num);
  2177. if (err)
  2178. return err;
  2179. sg_allow_dio = num ? 1 : 0;
  2180. return count;
  2181. }
  2182. static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
  2183. {
  2184. return single_open(file, sg_proc_seq_show_int, &sg_big_buff);
  2185. }
  2186. static ssize_t
  2187. sg_proc_write_dressz(struct file *filp, const char __user *buffer,
  2188. size_t count, loff_t *off)
  2189. {
  2190. int err;
  2191. unsigned long k = ULONG_MAX;
  2192. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  2193. return -EACCES;
  2194. err = kstrtoul_from_user(buffer, count, 0, &k);
  2195. if (err)
  2196. return err;
  2197. if (k <= 1048576) { /* limit "big buff" to 1 MB */
  2198. sg_big_buff = k;
  2199. return count;
  2200. }
  2201. return -ERANGE;
  2202. }
  2203. static int sg_proc_seq_show_version(struct seq_file *s, void *v)
  2204. {
  2205. seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
  2206. sg_version_date);
  2207. return 0;
  2208. }
  2209. static int sg_proc_single_open_version(struct inode *inode, struct file *file)
  2210. {
  2211. return single_open(file, sg_proc_seq_show_version, NULL);
  2212. }
  2213. static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
  2214. {
  2215. seq_puts(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\tonline\n");
  2216. return 0;
  2217. }
  2218. static int sg_proc_single_open_devhdr(struct inode *inode, struct file *file)
  2219. {
  2220. return single_open(file, sg_proc_seq_show_devhdr, NULL);
  2221. }
  2222. struct sg_proc_deviter {
  2223. loff_t index;
  2224. size_t max;
  2225. };
  2226. static void * dev_seq_start(struct seq_file *s, loff_t *pos)
  2227. {
  2228. struct sg_proc_deviter * it = kmalloc(sizeof(*it), GFP_KERNEL);
  2229. s->private = it;
  2230. if (! it)
  2231. return NULL;
  2232. it->index = *pos;
  2233. it->max = sg_last_dev();
  2234. if (it->index >= it->max)
  2235. return NULL;
  2236. return it;
  2237. }
  2238. static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
  2239. {
  2240. struct sg_proc_deviter * it = s->private;
  2241. *pos = ++it->index;
  2242. return (it->index < it->max) ? it : NULL;
  2243. }
  2244. static void dev_seq_stop(struct seq_file *s, void *v)
  2245. {
  2246. kfree(s->private);
  2247. }
  2248. static int sg_proc_open_dev(struct inode *inode, struct file *file)
  2249. {
  2250. return seq_open(file, &dev_seq_ops);
  2251. }
  2252. static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
  2253. {
  2254. struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
  2255. Sg_device *sdp;
  2256. struct scsi_device *scsidp;
  2257. unsigned long iflags;
  2258. read_lock_irqsave(&sg_index_lock, iflags);
  2259. sdp = it ? sg_lookup_dev(it->index) : NULL;
  2260. if ((NULL == sdp) || (NULL == sdp->device) ||
  2261. (atomic_read(&sdp->detaching)))
  2262. seq_puts(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
  2263. else {
  2264. scsidp = sdp->device;
  2265. seq_printf(s, "%d\t%d\t%d\t%llu\t%d\t%d\t%d\t%d\t%d\n",
  2266. scsidp->host->host_no, scsidp->channel,
  2267. scsidp->id, scsidp->lun, (int) scsidp->type,
  2268. 1,
  2269. (int) scsidp->queue_depth,
  2270. (int) atomic_read(&scsidp->device_busy),
  2271. (int) scsi_device_online(scsidp));
  2272. }
  2273. read_unlock_irqrestore(&sg_index_lock, iflags);
  2274. return 0;
  2275. }
  2276. static int sg_proc_open_devstrs(struct inode *inode, struct file *file)
  2277. {
  2278. return seq_open(file, &devstrs_seq_ops);
  2279. }
  2280. static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
  2281. {
  2282. struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
  2283. Sg_device *sdp;
  2284. struct scsi_device *scsidp;
  2285. unsigned long iflags;
  2286. read_lock_irqsave(&sg_index_lock, iflags);
  2287. sdp = it ? sg_lookup_dev(it->index) : NULL;
  2288. scsidp = sdp ? sdp->device : NULL;
  2289. if (sdp && scsidp && (!atomic_read(&sdp->detaching)))
  2290. seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
  2291. scsidp->vendor, scsidp->model, scsidp->rev);
  2292. else
  2293. seq_puts(s, "<no active device>\n");
  2294. read_unlock_irqrestore(&sg_index_lock, iflags);
  2295. return 0;
  2296. }
  2297. /* must be called while holding sg_index_lock */
  2298. static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
  2299. {
  2300. int k, m, new_interface, blen, usg;
  2301. Sg_request *srp;
  2302. Sg_fd *fp;
  2303. const sg_io_hdr_t *hp;
  2304. const char * cp;
  2305. unsigned int ms;
  2306. k = 0;
  2307. list_for_each_entry(fp, &sdp->sfds, sfd_siblings) {
  2308. k++;
  2309. read_lock(&fp->rq_list_lock); /* irqs already disabled */
  2310. seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
  2311. "(res)sgat=%d low_dma=%d\n", k,
  2312. jiffies_to_msecs(fp->timeout),
  2313. fp->reserve.bufflen,
  2314. (int) fp->reserve.k_use_sg,
  2315. (int) fp->low_dma);
  2316. seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=0\n",
  2317. (int) fp->cmd_q, (int) fp->force_packid,
  2318. (int) fp->keep_orphan);
  2319. for (m = 0, srp = fp->headrp;
  2320. srp != NULL;
  2321. ++m, srp = srp->nextrp) {
  2322. hp = &srp->header;
  2323. new_interface = (hp->interface_id == '\0') ? 0 : 1;
  2324. if (srp->res_used) {
  2325. if (new_interface &&
  2326. (SG_FLAG_MMAP_IO & hp->flags))
  2327. cp = " mmap>> ";
  2328. else
  2329. cp = " rb>> ";
  2330. } else {
  2331. if (SG_INFO_DIRECT_IO_MASK & hp->info)
  2332. cp = " dio>> ";
  2333. else
  2334. cp = " ";
  2335. }
  2336. seq_puts(s, cp);
  2337. blen = srp->data.bufflen;
  2338. usg = srp->data.k_use_sg;
  2339. seq_puts(s, srp->done ?
  2340. ((1 == srp->done) ? "rcv:" : "fin:")
  2341. : "act:");
  2342. seq_printf(s, " id=%d blen=%d",
  2343. srp->header.pack_id, blen);
  2344. if (srp->done)
  2345. seq_printf(s, " dur=%d", hp->duration);
  2346. else {
  2347. ms = jiffies_to_msecs(jiffies);
  2348. seq_printf(s, " t_o/elap=%d/%d",
  2349. (new_interface ? hp->timeout :
  2350. jiffies_to_msecs(fp->timeout)),
  2351. (ms > hp->duration ? ms - hp->duration : 0));
  2352. }
  2353. seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
  2354. (int) srp->data.cmd_opcode);
  2355. }
  2356. if (0 == m)
  2357. seq_puts(s, " No requests active\n");
  2358. read_unlock(&fp->rq_list_lock);
  2359. }
  2360. }
  2361. static int sg_proc_open_debug(struct inode *inode, struct file *file)
  2362. {
  2363. return seq_open(file, &debug_seq_ops);
  2364. }
  2365. static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
  2366. {
  2367. struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
  2368. Sg_device *sdp;
  2369. unsigned long iflags;
  2370. if (it && (0 == it->index))
  2371. seq_printf(s, "max_active_device=%d def_reserved_size=%d\n",
  2372. (int)it->max, sg_big_buff);
  2373. read_lock_irqsave(&sg_index_lock, iflags);
  2374. sdp = it ? sg_lookup_dev(it->index) : NULL;
  2375. if (NULL == sdp)
  2376. goto skip;
  2377. read_lock(&sdp->sfd_lock);
  2378. if (!list_empty(&sdp->sfds)) {
  2379. seq_printf(s, " >>> device=%s ", sdp->disk->disk_name);
  2380. if (atomic_read(&sdp->detaching))
  2381. seq_puts(s, "detaching pending close ");
  2382. else if (sdp->device) {
  2383. struct scsi_device *scsidp = sdp->device;
  2384. seq_printf(s, "%d:%d:%d:%llu em=%d",
  2385. scsidp->host->host_no,
  2386. scsidp->channel, scsidp->id,
  2387. scsidp->lun,
  2388. scsidp->host->hostt->emulated);
  2389. }
  2390. seq_printf(s, " sg_tablesize=%d excl=%d open_cnt=%d\n",
  2391. sdp->sg_tablesize, sdp->exclude, sdp->open_cnt);
  2392. sg_proc_debug_helper(s, sdp);
  2393. }
  2394. read_unlock(&sdp->sfd_lock);
  2395. skip:
  2396. read_unlock_irqrestore(&sg_index_lock, iflags);
  2397. return 0;
  2398. }
  2399. #endif /* CONFIG_SCSI_PROC_FS */
  2400. module_init(init_sg);
  2401. module_exit(exit_sg);