sg.c 74 KB

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