pktcdvd.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994
  1. /*
  2. * Copyright (C) 2000 Jens Axboe <axboe@suse.de>
  3. * Copyright (C) 2001-2004 Peter Osterlund <petero2@telia.com>
  4. * Copyright (C) 2006 Thomas Maier <balagi@justmail.de>
  5. *
  6. * May be copied or modified under the terms of the GNU General Public
  7. * License. See linux/COPYING for more information.
  8. *
  9. * Packet writing layer for ATAPI and SCSI CD-RW, DVD+RW, DVD-RW and
  10. * DVD-RAM devices.
  11. *
  12. * Theory of operation:
  13. *
  14. * At the lowest level, there is the standard driver for the CD/DVD device,
  15. * typically ide-cd.c or sr.c. This driver can handle read and write requests,
  16. * but it doesn't know anything about the special restrictions that apply to
  17. * packet writing. One restriction is that write requests must be aligned to
  18. * packet boundaries on the physical media, and the size of a write request
  19. * must be equal to the packet size. Another restriction is that a
  20. * GPCMD_FLUSH_CACHE command has to be issued to the drive before a read
  21. * command, if the previous command was a write.
  22. *
  23. * The purpose of the packet writing driver is to hide these restrictions from
  24. * higher layers, such as file systems, and present a block device that can be
  25. * randomly read and written using 2kB-sized blocks.
  26. *
  27. * The lowest layer in the packet writing driver is the packet I/O scheduler.
  28. * Its data is defined by the struct packet_iosched and includes two bio
  29. * queues with pending read and write requests. These queues are processed
  30. * by the pkt_iosched_process_queue() function. The write requests in this
  31. * queue are already properly aligned and sized. This layer is responsible for
  32. * issuing the flush cache commands and scheduling the I/O in a good order.
  33. *
  34. * The next layer transforms unaligned write requests to aligned writes. This
  35. * transformation requires reading missing pieces of data from the underlying
  36. * block device, assembling the pieces to full packets and queuing them to the
  37. * packet I/O scheduler.
  38. *
  39. * At the top layer there is a custom make_request_fn function that forwards
  40. * read requests directly to the iosched queue and puts write requests in the
  41. * unaligned write queue. A kernel thread performs the necessary read
  42. * gathering to convert the unaligned writes to aligned writes and then feeds
  43. * them to the packet I/O scheduler.
  44. *
  45. *************************************************************************/
  46. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  47. #include <linux/pktcdvd.h>
  48. #include <linux/module.h>
  49. #include <linux/types.h>
  50. #include <linux/kernel.h>
  51. #include <linux/compat.h>
  52. #include <linux/kthread.h>
  53. #include <linux/errno.h>
  54. #include <linux/spinlock.h>
  55. #include <linux/file.h>
  56. #include <linux/proc_fs.h>
  57. #include <linux/seq_file.h>
  58. #include <linux/miscdevice.h>
  59. #include <linux/freezer.h>
  60. #include <linux/mutex.h>
  61. #include <linux/slab.h>
  62. #include <linux/backing-dev.h>
  63. #include <scsi/scsi_cmnd.h>
  64. #include <scsi/scsi_ioctl.h>
  65. #include <scsi/scsi.h>
  66. #include <linux/debugfs.h>
  67. #include <linux/device.h>
  68. #include <linux/nospec.h>
  69. #include <linux/uaccess.h>
  70. #define DRIVER_NAME "pktcdvd"
  71. #define pkt_err(pd, fmt, ...) \
  72. pr_err("%s: " fmt, pd->name, ##__VA_ARGS__)
  73. #define pkt_notice(pd, fmt, ...) \
  74. pr_notice("%s: " fmt, pd->name, ##__VA_ARGS__)
  75. #define pkt_info(pd, fmt, ...) \
  76. pr_info("%s: " fmt, pd->name, ##__VA_ARGS__)
  77. #define pkt_dbg(level, pd, fmt, ...) \
  78. do { \
  79. if (level == 2 && PACKET_DEBUG >= 2) \
  80. pr_notice("%s: %s():" fmt, \
  81. pd->name, __func__, ##__VA_ARGS__); \
  82. else if (level == 1 && PACKET_DEBUG >= 1) \
  83. pr_notice("%s: " fmt, pd->name, ##__VA_ARGS__); \
  84. } while (0)
  85. #define MAX_SPEED 0xffff
  86. static DEFINE_MUTEX(pktcdvd_mutex);
  87. static struct pktcdvd_device *pkt_devs[MAX_WRITERS];
  88. static struct proc_dir_entry *pkt_proc;
  89. static int pktdev_major;
  90. static int write_congestion_on = PKT_WRITE_CONGESTION_ON;
  91. static int write_congestion_off = PKT_WRITE_CONGESTION_OFF;
  92. static struct mutex ctl_mutex; /* Serialize open/close/setup/teardown */
  93. static mempool_t psd_pool;
  94. static struct bio_set pkt_bio_set;
  95. static struct class *class_pktcdvd = NULL; /* /sys/class/pktcdvd */
  96. static struct dentry *pkt_debugfs_root = NULL; /* /sys/kernel/debug/pktcdvd */
  97. /* forward declaration */
  98. static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev);
  99. static int pkt_remove_dev(dev_t pkt_dev);
  100. static int pkt_seq_show(struct seq_file *m, void *p);
  101. static sector_t get_zone(sector_t sector, struct pktcdvd_device *pd)
  102. {
  103. return (sector + pd->offset) & ~(sector_t)(pd->settings.size - 1);
  104. }
  105. /*
  106. * create and register a pktcdvd kernel object.
  107. */
  108. static struct pktcdvd_kobj* pkt_kobj_create(struct pktcdvd_device *pd,
  109. const char* name,
  110. struct kobject* parent,
  111. struct kobj_type* ktype)
  112. {
  113. struct pktcdvd_kobj *p;
  114. int error;
  115. p = kzalloc(sizeof(*p), GFP_KERNEL);
  116. if (!p)
  117. return NULL;
  118. p->pd = pd;
  119. error = kobject_init_and_add(&p->kobj, ktype, parent, "%s", name);
  120. if (error) {
  121. kobject_put(&p->kobj);
  122. return NULL;
  123. }
  124. kobject_uevent(&p->kobj, KOBJ_ADD);
  125. return p;
  126. }
  127. /*
  128. * remove a pktcdvd kernel object.
  129. */
  130. static void pkt_kobj_remove(struct pktcdvd_kobj *p)
  131. {
  132. if (p)
  133. kobject_put(&p->kobj);
  134. }
  135. /*
  136. * default release function for pktcdvd kernel objects.
  137. */
  138. static void pkt_kobj_release(struct kobject *kobj)
  139. {
  140. kfree(to_pktcdvdkobj(kobj));
  141. }
  142. /**********************************************************
  143. *
  144. * sysfs interface for pktcdvd
  145. * by (C) 2006 Thomas Maier <balagi@justmail.de>
  146. *
  147. **********************************************************/
  148. #define DEF_ATTR(_obj,_name,_mode) \
  149. static struct attribute _obj = { .name = _name, .mode = _mode }
  150. /**********************************************************
  151. /sys/class/pktcdvd/pktcdvd[0-7]/
  152. stat/reset
  153. stat/packets_started
  154. stat/packets_finished
  155. stat/kb_written
  156. stat/kb_read
  157. stat/kb_read_gather
  158. write_queue/size
  159. write_queue/congestion_off
  160. write_queue/congestion_on
  161. **********************************************************/
  162. DEF_ATTR(kobj_pkt_attr_st1, "reset", 0200);
  163. DEF_ATTR(kobj_pkt_attr_st2, "packets_started", 0444);
  164. DEF_ATTR(kobj_pkt_attr_st3, "packets_finished", 0444);
  165. DEF_ATTR(kobj_pkt_attr_st4, "kb_written", 0444);
  166. DEF_ATTR(kobj_pkt_attr_st5, "kb_read", 0444);
  167. DEF_ATTR(kobj_pkt_attr_st6, "kb_read_gather", 0444);
  168. static struct attribute *kobj_pkt_attrs_stat[] = {
  169. &kobj_pkt_attr_st1,
  170. &kobj_pkt_attr_st2,
  171. &kobj_pkt_attr_st3,
  172. &kobj_pkt_attr_st4,
  173. &kobj_pkt_attr_st5,
  174. &kobj_pkt_attr_st6,
  175. NULL
  176. };
  177. DEF_ATTR(kobj_pkt_attr_wq1, "size", 0444);
  178. DEF_ATTR(kobj_pkt_attr_wq2, "congestion_off", 0644);
  179. DEF_ATTR(kobj_pkt_attr_wq3, "congestion_on", 0644);
  180. static struct attribute *kobj_pkt_attrs_wqueue[] = {
  181. &kobj_pkt_attr_wq1,
  182. &kobj_pkt_attr_wq2,
  183. &kobj_pkt_attr_wq3,
  184. NULL
  185. };
  186. static ssize_t kobj_pkt_show(struct kobject *kobj,
  187. struct attribute *attr, char *data)
  188. {
  189. struct pktcdvd_device *pd = to_pktcdvdkobj(kobj)->pd;
  190. int n = 0;
  191. int v;
  192. if (strcmp(attr->name, "packets_started") == 0) {
  193. n = sprintf(data, "%lu\n", pd->stats.pkt_started);
  194. } else if (strcmp(attr->name, "packets_finished") == 0) {
  195. n = sprintf(data, "%lu\n", pd->stats.pkt_ended);
  196. } else if (strcmp(attr->name, "kb_written") == 0) {
  197. n = sprintf(data, "%lu\n", pd->stats.secs_w >> 1);
  198. } else if (strcmp(attr->name, "kb_read") == 0) {
  199. n = sprintf(data, "%lu\n", pd->stats.secs_r >> 1);
  200. } else if (strcmp(attr->name, "kb_read_gather") == 0) {
  201. n = sprintf(data, "%lu\n", pd->stats.secs_rg >> 1);
  202. } else if (strcmp(attr->name, "size") == 0) {
  203. spin_lock(&pd->lock);
  204. v = pd->bio_queue_size;
  205. spin_unlock(&pd->lock);
  206. n = sprintf(data, "%d\n", v);
  207. } else if (strcmp(attr->name, "congestion_off") == 0) {
  208. spin_lock(&pd->lock);
  209. v = pd->write_congestion_off;
  210. spin_unlock(&pd->lock);
  211. n = sprintf(data, "%d\n", v);
  212. } else if (strcmp(attr->name, "congestion_on") == 0) {
  213. spin_lock(&pd->lock);
  214. v = pd->write_congestion_on;
  215. spin_unlock(&pd->lock);
  216. n = sprintf(data, "%d\n", v);
  217. }
  218. return n;
  219. }
  220. static void init_write_congestion_marks(int* lo, int* hi)
  221. {
  222. if (*hi > 0) {
  223. *hi = max(*hi, 500);
  224. *hi = min(*hi, 1000000);
  225. if (*lo <= 0)
  226. *lo = *hi - 100;
  227. else {
  228. *lo = min(*lo, *hi - 100);
  229. *lo = max(*lo, 100);
  230. }
  231. } else {
  232. *hi = -1;
  233. *lo = -1;
  234. }
  235. }
  236. static ssize_t kobj_pkt_store(struct kobject *kobj,
  237. struct attribute *attr,
  238. const char *data, size_t len)
  239. {
  240. struct pktcdvd_device *pd = to_pktcdvdkobj(kobj)->pd;
  241. int val;
  242. if (strcmp(attr->name, "reset") == 0 && len > 0) {
  243. pd->stats.pkt_started = 0;
  244. pd->stats.pkt_ended = 0;
  245. pd->stats.secs_w = 0;
  246. pd->stats.secs_rg = 0;
  247. pd->stats.secs_r = 0;
  248. } else if (strcmp(attr->name, "congestion_off") == 0
  249. && sscanf(data, "%d", &val) == 1) {
  250. spin_lock(&pd->lock);
  251. pd->write_congestion_off = val;
  252. init_write_congestion_marks(&pd->write_congestion_off,
  253. &pd->write_congestion_on);
  254. spin_unlock(&pd->lock);
  255. } else if (strcmp(attr->name, "congestion_on") == 0
  256. && sscanf(data, "%d", &val) == 1) {
  257. spin_lock(&pd->lock);
  258. pd->write_congestion_on = val;
  259. init_write_congestion_marks(&pd->write_congestion_off,
  260. &pd->write_congestion_on);
  261. spin_unlock(&pd->lock);
  262. }
  263. return len;
  264. }
  265. static const struct sysfs_ops kobj_pkt_ops = {
  266. .show = kobj_pkt_show,
  267. .store = kobj_pkt_store
  268. };
  269. static struct kobj_type kobj_pkt_type_stat = {
  270. .release = pkt_kobj_release,
  271. .sysfs_ops = &kobj_pkt_ops,
  272. .default_attrs = kobj_pkt_attrs_stat
  273. };
  274. static struct kobj_type kobj_pkt_type_wqueue = {
  275. .release = pkt_kobj_release,
  276. .sysfs_ops = &kobj_pkt_ops,
  277. .default_attrs = kobj_pkt_attrs_wqueue
  278. };
  279. static void pkt_sysfs_dev_new(struct pktcdvd_device *pd)
  280. {
  281. if (class_pktcdvd) {
  282. pd->dev = device_create(class_pktcdvd, NULL, MKDEV(0, 0), NULL,
  283. "%s", pd->name);
  284. if (IS_ERR(pd->dev))
  285. pd->dev = NULL;
  286. }
  287. if (pd->dev) {
  288. pd->kobj_stat = pkt_kobj_create(pd, "stat",
  289. &pd->dev->kobj,
  290. &kobj_pkt_type_stat);
  291. pd->kobj_wqueue = pkt_kobj_create(pd, "write_queue",
  292. &pd->dev->kobj,
  293. &kobj_pkt_type_wqueue);
  294. }
  295. }
  296. static void pkt_sysfs_dev_remove(struct pktcdvd_device *pd)
  297. {
  298. pkt_kobj_remove(pd->kobj_stat);
  299. pkt_kobj_remove(pd->kobj_wqueue);
  300. if (class_pktcdvd)
  301. device_unregister(pd->dev);
  302. }
  303. /********************************************************************
  304. /sys/class/pktcdvd/
  305. add map block device
  306. remove unmap packet dev
  307. device_map show mappings
  308. *******************************************************************/
  309. static void class_pktcdvd_release(struct class *cls)
  310. {
  311. kfree(cls);
  312. }
  313. static ssize_t device_map_show(struct class *c, struct class_attribute *attr,
  314. char *data)
  315. {
  316. int n = 0;
  317. int idx;
  318. mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
  319. for (idx = 0; idx < MAX_WRITERS; idx++) {
  320. struct pktcdvd_device *pd = pkt_devs[idx];
  321. if (!pd)
  322. continue;
  323. n += sprintf(data+n, "%s %u:%u %u:%u\n",
  324. pd->name,
  325. MAJOR(pd->pkt_dev), MINOR(pd->pkt_dev),
  326. MAJOR(pd->bdev->bd_dev),
  327. MINOR(pd->bdev->bd_dev));
  328. }
  329. mutex_unlock(&ctl_mutex);
  330. return n;
  331. }
  332. static CLASS_ATTR_RO(device_map);
  333. static ssize_t add_store(struct class *c, struct class_attribute *attr,
  334. const char *buf, size_t count)
  335. {
  336. unsigned int major, minor;
  337. if (sscanf(buf, "%u:%u", &major, &minor) == 2) {
  338. /* pkt_setup_dev() expects caller to hold reference to self */
  339. if (!try_module_get(THIS_MODULE))
  340. return -ENODEV;
  341. pkt_setup_dev(MKDEV(major, minor), NULL);
  342. module_put(THIS_MODULE);
  343. return count;
  344. }
  345. return -EINVAL;
  346. }
  347. static CLASS_ATTR_WO(add);
  348. static ssize_t remove_store(struct class *c, struct class_attribute *attr,
  349. const char *buf, size_t count)
  350. {
  351. unsigned int major, minor;
  352. if (sscanf(buf, "%u:%u", &major, &minor) == 2) {
  353. pkt_remove_dev(MKDEV(major, minor));
  354. return count;
  355. }
  356. return -EINVAL;
  357. }
  358. static CLASS_ATTR_WO(remove);
  359. static struct attribute *class_pktcdvd_attrs[] = {
  360. &class_attr_add.attr,
  361. &class_attr_remove.attr,
  362. &class_attr_device_map.attr,
  363. NULL,
  364. };
  365. ATTRIBUTE_GROUPS(class_pktcdvd);
  366. static int pkt_sysfs_init(void)
  367. {
  368. int ret = 0;
  369. /*
  370. * create control files in sysfs
  371. * /sys/class/pktcdvd/...
  372. */
  373. class_pktcdvd = kzalloc(sizeof(*class_pktcdvd), GFP_KERNEL);
  374. if (!class_pktcdvd)
  375. return -ENOMEM;
  376. class_pktcdvd->name = DRIVER_NAME;
  377. class_pktcdvd->owner = THIS_MODULE;
  378. class_pktcdvd->class_release = class_pktcdvd_release;
  379. class_pktcdvd->class_groups = class_pktcdvd_groups;
  380. ret = class_register(class_pktcdvd);
  381. if (ret) {
  382. kfree(class_pktcdvd);
  383. class_pktcdvd = NULL;
  384. pr_err("failed to create class pktcdvd\n");
  385. return ret;
  386. }
  387. return 0;
  388. }
  389. static void pkt_sysfs_cleanup(void)
  390. {
  391. if (class_pktcdvd)
  392. class_destroy(class_pktcdvd);
  393. class_pktcdvd = NULL;
  394. }
  395. /********************************************************************
  396. entries in debugfs
  397. /sys/kernel/debug/pktcdvd[0-7]/
  398. info
  399. *******************************************************************/
  400. static int pkt_debugfs_seq_show(struct seq_file *m, void *p)
  401. {
  402. return pkt_seq_show(m, p);
  403. }
  404. static int pkt_debugfs_fops_open(struct inode *inode, struct file *file)
  405. {
  406. return single_open(file, pkt_debugfs_seq_show, inode->i_private);
  407. }
  408. static const struct file_operations debug_fops = {
  409. .open = pkt_debugfs_fops_open,
  410. .read = seq_read,
  411. .llseek = seq_lseek,
  412. .release = single_release,
  413. .owner = THIS_MODULE,
  414. };
  415. static void pkt_debugfs_dev_new(struct pktcdvd_device *pd)
  416. {
  417. if (!pkt_debugfs_root)
  418. return;
  419. pd->dfs_d_root = debugfs_create_dir(pd->name, pkt_debugfs_root);
  420. if (!pd->dfs_d_root)
  421. return;
  422. pd->dfs_f_info = debugfs_create_file("info", 0444,
  423. pd->dfs_d_root, pd, &debug_fops);
  424. }
  425. static void pkt_debugfs_dev_remove(struct pktcdvd_device *pd)
  426. {
  427. if (!pkt_debugfs_root)
  428. return;
  429. debugfs_remove(pd->dfs_f_info);
  430. debugfs_remove(pd->dfs_d_root);
  431. pd->dfs_f_info = NULL;
  432. pd->dfs_d_root = NULL;
  433. }
  434. static void pkt_debugfs_init(void)
  435. {
  436. pkt_debugfs_root = debugfs_create_dir(DRIVER_NAME, NULL);
  437. }
  438. static void pkt_debugfs_cleanup(void)
  439. {
  440. debugfs_remove(pkt_debugfs_root);
  441. pkt_debugfs_root = NULL;
  442. }
  443. /* ----------------------------------------------------------*/
  444. static void pkt_bio_finished(struct pktcdvd_device *pd)
  445. {
  446. BUG_ON(atomic_read(&pd->cdrw.pending_bios) <= 0);
  447. if (atomic_dec_and_test(&pd->cdrw.pending_bios)) {
  448. pkt_dbg(2, pd, "queue empty\n");
  449. atomic_set(&pd->iosched.attention, 1);
  450. wake_up(&pd->wqueue);
  451. }
  452. }
  453. /*
  454. * Allocate a packet_data struct
  455. */
  456. static struct packet_data *pkt_alloc_packet_data(int frames)
  457. {
  458. int i;
  459. struct packet_data *pkt;
  460. pkt = kzalloc(sizeof(struct packet_data), GFP_KERNEL);
  461. if (!pkt)
  462. goto no_pkt;
  463. pkt->frames = frames;
  464. pkt->w_bio = bio_kmalloc(GFP_KERNEL, frames);
  465. if (!pkt->w_bio)
  466. goto no_bio;
  467. for (i = 0; i < frames / FRAMES_PER_PAGE; i++) {
  468. pkt->pages[i] = alloc_page(GFP_KERNEL|__GFP_ZERO);
  469. if (!pkt->pages[i])
  470. goto no_page;
  471. }
  472. spin_lock_init(&pkt->lock);
  473. bio_list_init(&pkt->orig_bios);
  474. for (i = 0; i < frames; i++) {
  475. struct bio *bio = bio_kmalloc(GFP_KERNEL, 1);
  476. if (!bio)
  477. goto no_rd_bio;
  478. pkt->r_bios[i] = bio;
  479. }
  480. return pkt;
  481. no_rd_bio:
  482. for (i = 0; i < frames; i++) {
  483. struct bio *bio = pkt->r_bios[i];
  484. if (bio)
  485. bio_put(bio);
  486. }
  487. no_page:
  488. for (i = 0; i < frames / FRAMES_PER_PAGE; i++)
  489. if (pkt->pages[i])
  490. __free_page(pkt->pages[i]);
  491. bio_put(pkt->w_bio);
  492. no_bio:
  493. kfree(pkt);
  494. no_pkt:
  495. return NULL;
  496. }
  497. /*
  498. * Free a packet_data struct
  499. */
  500. static void pkt_free_packet_data(struct packet_data *pkt)
  501. {
  502. int i;
  503. for (i = 0; i < pkt->frames; i++) {
  504. struct bio *bio = pkt->r_bios[i];
  505. if (bio)
  506. bio_put(bio);
  507. }
  508. for (i = 0; i < pkt->frames / FRAMES_PER_PAGE; i++)
  509. __free_page(pkt->pages[i]);
  510. bio_put(pkt->w_bio);
  511. kfree(pkt);
  512. }
  513. static void pkt_shrink_pktlist(struct pktcdvd_device *pd)
  514. {
  515. struct packet_data *pkt, *next;
  516. BUG_ON(!list_empty(&pd->cdrw.pkt_active_list));
  517. list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_free_list, list) {
  518. pkt_free_packet_data(pkt);
  519. }
  520. INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
  521. }
  522. static int pkt_grow_pktlist(struct pktcdvd_device *pd, int nr_packets)
  523. {
  524. struct packet_data *pkt;
  525. BUG_ON(!list_empty(&pd->cdrw.pkt_free_list));
  526. while (nr_packets > 0) {
  527. pkt = pkt_alloc_packet_data(pd->settings.size >> 2);
  528. if (!pkt) {
  529. pkt_shrink_pktlist(pd);
  530. return 0;
  531. }
  532. pkt->id = nr_packets;
  533. pkt->pd = pd;
  534. list_add(&pkt->list, &pd->cdrw.pkt_free_list);
  535. nr_packets--;
  536. }
  537. return 1;
  538. }
  539. static inline struct pkt_rb_node *pkt_rbtree_next(struct pkt_rb_node *node)
  540. {
  541. struct rb_node *n = rb_next(&node->rb_node);
  542. if (!n)
  543. return NULL;
  544. return rb_entry(n, struct pkt_rb_node, rb_node);
  545. }
  546. static void pkt_rbtree_erase(struct pktcdvd_device *pd, struct pkt_rb_node *node)
  547. {
  548. rb_erase(&node->rb_node, &pd->bio_queue);
  549. mempool_free(node, &pd->rb_pool);
  550. pd->bio_queue_size--;
  551. BUG_ON(pd->bio_queue_size < 0);
  552. }
  553. /*
  554. * Find the first node in the pd->bio_queue rb tree with a starting sector >= s.
  555. */
  556. static struct pkt_rb_node *pkt_rbtree_find(struct pktcdvd_device *pd, sector_t s)
  557. {
  558. struct rb_node *n = pd->bio_queue.rb_node;
  559. struct rb_node *next;
  560. struct pkt_rb_node *tmp;
  561. if (!n) {
  562. BUG_ON(pd->bio_queue_size > 0);
  563. return NULL;
  564. }
  565. for (;;) {
  566. tmp = rb_entry(n, struct pkt_rb_node, rb_node);
  567. if (s <= tmp->bio->bi_iter.bi_sector)
  568. next = n->rb_left;
  569. else
  570. next = n->rb_right;
  571. if (!next)
  572. break;
  573. n = next;
  574. }
  575. if (s > tmp->bio->bi_iter.bi_sector) {
  576. tmp = pkt_rbtree_next(tmp);
  577. if (!tmp)
  578. return NULL;
  579. }
  580. BUG_ON(s > tmp->bio->bi_iter.bi_sector);
  581. return tmp;
  582. }
  583. /*
  584. * Insert a node into the pd->bio_queue rb tree.
  585. */
  586. static void pkt_rbtree_insert(struct pktcdvd_device *pd, struct pkt_rb_node *node)
  587. {
  588. struct rb_node **p = &pd->bio_queue.rb_node;
  589. struct rb_node *parent = NULL;
  590. sector_t s = node->bio->bi_iter.bi_sector;
  591. struct pkt_rb_node *tmp;
  592. while (*p) {
  593. parent = *p;
  594. tmp = rb_entry(parent, struct pkt_rb_node, rb_node);
  595. if (s < tmp->bio->bi_iter.bi_sector)
  596. p = &(*p)->rb_left;
  597. else
  598. p = &(*p)->rb_right;
  599. }
  600. rb_link_node(&node->rb_node, parent, p);
  601. rb_insert_color(&node->rb_node, &pd->bio_queue);
  602. pd->bio_queue_size++;
  603. }
  604. /*
  605. * Send a packet_command to the underlying block device and
  606. * wait for completion.
  607. */
  608. static int pkt_generic_packet(struct pktcdvd_device *pd, struct packet_command *cgc)
  609. {
  610. struct request_queue *q = bdev_get_queue(pd->bdev);
  611. struct request *rq;
  612. int ret = 0;
  613. rq = blk_get_request(q, (cgc->data_direction == CGC_DATA_WRITE) ?
  614. REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, 0);
  615. if (IS_ERR(rq))
  616. return PTR_ERR(rq);
  617. if (cgc->buflen) {
  618. ret = blk_rq_map_kern(q, rq, cgc->buffer, cgc->buflen,
  619. GFP_NOIO);
  620. if (ret)
  621. goto out;
  622. }
  623. scsi_req(rq)->cmd_len = COMMAND_SIZE(cgc->cmd[0]);
  624. memcpy(scsi_req(rq)->cmd, cgc->cmd, CDROM_PACKET_SIZE);
  625. rq->timeout = 60*HZ;
  626. if (cgc->quiet)
  627. rq->rq_flags |= RQF_QUIET;
  628. blk_execute_rq(rq->q, pd->bdev->bd_disk, rq, 0);
  629. if (scsi_req(rq)->result)
  630. ret = -EIO;
  631. out:
  632. blk_put_request(rq);
  633. return ret;
  634. }
  635. static const char *sense_key_string(__u8 index)
  636. {
  637. static const char * const info[] = {
  638. "No sense", "Recovered error", "Not ready",
  639. "Medium error", "Hardware error", "Illegal request",
  640. "Unit attention", "Data protect", "Blank check",
  641. };
  642. return index < ARRAY_SIZE(info) ? info[index] : "INVALID";
  643. }
  644. /*
  645. * A generic sense dump / resolve mechanism should be implemented across
  646. * all ATAPI + SCSI devices.
  647. */
  648. static void pkt_dump_sense(struct pktcdvd_device *pd,
  649. struct packet_command *cgc)
  650. {
  651. struct scsi_sense_hdr *sshdr = cgc->sshdr;
  652. if (sshdr)
  653. pkt_err(pd, "%*ph - sense %02x.%02x.%02x (%s)\n",
  654. CDROM_PACKET_SIZE, cgc->cmd,
  655. sshdr->sense_key, sshdr->asc, sshdr->ascq,
  656. sense_key_string(sshdr->sense_key));
  657. else
  658. pkt_err(pd, "%*ph - no sense\n", CDROM_PACKET_SIZE, cgc->cmd);
  659. }
  660. /*
  661. * flush the drive cache to media
  662. */
  663. static int pkt_flush_cache(struct pktcdvd_device *pd)
  664. {
  665. struct packet_command cgc;
  666. init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
  667. cgc.cmd[0] = GPCMD_FLUSH_CACHE;
  668. cgc.quiet = 1;
  669. /*
  670. * the IMMED bit -- we default to not setting it, although that
  671. * would allow a much faster close, this is safer
  672. */
  673. #if 0
  674. cgc.cmd[1] = 1 << 1;
  675. #endif
  676. return pkt_generic_packet(pd, &cgc);
  677. }
  678. /*
  679. * speed is given as the normal factor, e.g. 4 for 4x
  680. */
  681. static noinline_for_stack int pkt_set_speed(struct pktcdvd_device *pd,
  682. unsigned write_speed, unsigned read_speed)
  683. {
  684. struct packet_command cgc;
  685. struct scsi_sense_hdr sshdr;
  686. int ret;
  687. init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
  688. cgc.sshdr = &sshdr;
  689. cgc.cmd[0] = GPCMD_SET_SPEED;
  690. cgc.cmd[2] = (read_speed >> 8) & 0xff;
  691. cgc.cmd[3] = read_speed & 0xff;
  692. cgc.cmd[4] = (write_speed >> 8) & 0xff;
  693. cgc.cmd[5] = write_speed & 0xff;
  694. ret = pkt_generic_packet(pd, &cgc);
  695. if (ret)
  696. pkt_dump_sense(pd, &cgc);
  697. return ret;
  698. }
  699. /*
  700. * Queue a bio for processing by the low-level CD device. Must be called
  701. * from process context.
  702. */
  703. static void pkt_queue_bio(struct pktcdvd_device *pd, struct bio *bio)
  704. {
  705. spin_lock(&pd->iosched.lock);
  706. if (bio_data_dir(bio) == READ)
  707. bio_list_add(&pd->iosched.read_queue, bio);
  708. else
  709. bio_list_add(&pd->iosched.write_queue, bio);
  710. spin_unlock(&pd->iosched.lock);
  711. atomic_set(&pd->iosched.attention, 1);
  712. wake_up(&pd->wqueue);
  713. }
  714. /*
  715. * Process the queued read/write requests. This function handles special
  716. * requirements for CDRW drives:
  717. * - A cache flush command must be inserted before a read request if the
  718. * previous request was a write.
  719. * - Switching between reading and writing is slow, so don't do it more often
  720. * than necessary.
  721. * - Optimize for throughput at the expense of latency. This means that streaming
  722. * writes will never be interrupted by a read, but if the drive has to seek
  723. * before the next write, switch to reading instead if there are any pending
  724. * read requests.
  725. * - Set the read speed according to current usage pattern. When only reading
  726. * from the device, it's best to use the highest possible read speed, but
  727. * when switching often between reading and writing, it's better to have the
  728. * same read and write speeds.
  729. */
  730. static void pkt_iosched_process_queue(struct pktcdvd_device *pd)
  731. {
  732. if (atomic_read(&pd->iosched.attention) == 0)
  733. return;
  734. atomic_set(&pd->iosched.attention, 0);
  735. for (;;) {
  736. struct bio *bio;
  737. int reads_queued, writes_queued;
  738. spin_lock(&pd->iosched.lock);
  739. reads_queued = !bio_list_empty(&pd->iosched.read_queue);
  740. writes_queued = !bio_list_empty(&pd->iosched.write_queue);
  741. spin_unlock(&pd->iosched.lock);
  742. if (!reads_queued && !writes_queued)
  743. break;
  744. if (pd->iosched.writing) {
  745. int need_write_seek = 1;
  746. spin_lock(&pd->iosched.lock);
  747. bio = bio_list_peek(&pd->iosched.write_queue);
  748. spin_unlock(&pd->iosched.lock);
  749. if (bio && (bio->bi_iter.bi_sector ==
  750. pd->iosched.last_write))
  751. need_write_seek = 0;
  752. if (need_write_seek && reads_queued) {
  753. if (atomic_read(&pd->cdrw.pending_bios) > 0) {
  754. pkt_dbg(2, pd, "write, waiting\n");
  755. break;
  756. }
  757. pkt_flush_cache(pd);
  758. pd->iosched.writing = 0;
  759. }
  760. } else {
  761. if (!reads_queued && writes_queued) {
  762. if (atomic_read(&pd->cdrw.pending_bios) > 0) {
  763. pkt_dbg(2, pd, "read, waiting\n");
  764. break;
  765. }
  766. pd->iosched.writing = 1;
  767. }
  768. }
  769. spin_lock(&pd->iosched.lock);
  770. if (pd->iosched.writing)
  771. bio = bio_list_pop(&pd->iosched.write_queue);
  772. else
  773. bio = bio_list_pop(&pd->iosched.read_queue);
  774. spin_unlock(&pd->iosched.lock);
  775. if (!bio)
  776. continue;
  777. if (bio_data_dir(bio) == READ)
  778. pd->iosched.successive_reads +=
  779. bio->bi_iter.bi_size >> 10;
  780. else {
  781. pd->iosched.successive_reads = 0;
  782. pd->iosched.last_write = bio_end_sector(bio);
  783. }
  784. if (pd->iosched.successive_reads >= HI_SPEED_SWITCH) {
  785. if (pd->read_speed == pd->write_speed) {
  786. pd->read_speed = MAX_SPEED;
  787. pkt_set_speed(pd, pd->write_speed, pd->read_speed);
  788. }
  789. } else {
  790. if (pd->read_speed != pd->write_speed) {
  791. pd->read_speed = pd->write_speed;
  792. pkt_set_speed(pd, pd->write_speed, pd->read_speed);
  793. }
  794. }
  795. atomic_inc(&pd->cdrw.pending_bios);
  796. generic_make_request(bio);
  797. }
  798. }
  799. /*
  800. * Special care is needed if the underlying block device has a small
  801. * max_phys_segments value.
  802. */
  803. static int pkt_set_segment_merging(struct pktcdvd_device *pd, struct request_queue *q)
  804. {
  805. if ((pd->settings.size << 9) / CD_FRAMESIZE
  806. <= queue_max_segments(q)) {
  807. /*
  808. * The cdrom device can handle one segment/frame
  809. */
  810. clear_bit(PACKET_MERGE_SEGS, &pd->flags);
  811. return 0;
  812. } else if ((pd->settings.size << 9) / PAGE_SIZE
  813. <= queue_max_segments(q)) {
  814. /*
  815. * We can handle this case at the expense of some extra memory
  816. * copies during write operations
  817. */
  818. set_bit(PACKET_MERGE_SEGS, &pd->flags);
  819. return 0;
  820. } else {
  821. pkt_err(pd, "cdrom max_phys_segments too small\n");
  822. return -EIO;
  823. }
  824. }
  825. static void pkt_end_io_read(struct bio *bio)
  826. {
  827. struct packet_data *pkt = bio->bi_private;
  828. struct pktcdvd_device *pd = pkt->pd;
  829. BUG_ON(!pd);
  830. pkt_dbg(2, pd, "bio=%p sec0=%llx sec=%llx err=%d\n",
  831. bio, (unsigned long long)pkt->sector,
  832. (unsigned long long)bio->bi_iter.bi_sector, bio->bi_status);
  833. if (bio->bi_status)
  834. atomic_inc(&pkt->io_errors);
  835. if (atomic_dec_and_test(&pkt->io_wait)) {
  836. atomic_inc(&pkt->run_sm);
  837. wake_up(&pd->wqueue);
  838. }
  839. pkt_bio_finished(pd);
  840. }
  841. static void pkt_end_io_packet_write(struct bio *bio)
  842. {
  843. struct packet_data *pkt = bio->bi_private;
  844. struct pktcdvd_device *pd = pkt->pd;
  845. BUG_ON(!pd);
  846. pkt_dbg(2, pd, "id=%d, err=%d\n", pkt->id, bio->bi_status);
  847. pd->stats.pkt_ended++;
  848. pkt_bio_finished(pd);
  849. atomic_dec(&pkt->io_wait);
  850. atomic_inc(&pkt->run_sm);
  851. wake_up(&pd->wqueue);
  852. }
  853. /*
  854. * Schedule reads for the holes in a packet
  855. */
  856. static void pkt_gather_data(struct pktcdvd_device *pd, struct packet_data *pkt)
  857. {
  858. int frames_read = 0;
  859. struct bio *bio;
  860. int f;
  861. char written[PACKET_MAX_SIZE];
  862. BUG_ON(bio_list_empty(&pkt->orig_bios));
  863. atomic_set(&pkt->io_wait, 0);
  864. atomic_set(&pkt->io_errors, 0);
  865. /*
  866. * Figure out which frames we need to read before we can write.
  867. */
  868. memset(written, 0, sizeof(written));
  869. spin_lock(&pkt->lock);
  870. bio_list_for_each(bio, &pkt->orig_bios) {
  871. int first_frame = (bio->bi_iter.bi_sector - pkt->sector) /
  872. (CD_FRAMESIZE >> 9);
  873. int num_frames = bio->bi_iter.bi_size / CD_FRAMESIZE;
  874. pd->stats.secs_w += num_frames * (CD_FRAMESIZE >> 9);
  875. BUG_ON(first_frame < 0);
  876. BUG_ON(first_frame + num_frames > pkt->frames);
  877. for (f = first_frame; f < first_frame + num_frames; f++)
  878. written[f] = 1;
  879. }
  880. spin_unlock(&pkt->lock);
  881. if (pkt->cache_valid) {
  882. pkt_dbg(2, pd, "zone %llx cached\n",
  883. (unsigned long long)pkt->sector);
  884. goto out_account;
  885. }
  886. /*
  887. * Schedule reads for missing parts of the packet.
  888. */
  889. for (f = 0; f < pkt->frames; f++) {
  890. int p, offset;
  891. if (written[f])
  892. continue;
  893. bio = pkt->r_bios[f];
  894. bio_reset(bio);
  895. bio->bi_iter.bi_sector = pkt->sector + f * (CD_FRAMESIZE >> 9);
  896. bio_set_dev(bio, pd->bdev);
  897. bio->bi_end_io = pkt_end_io_read;
  898. bio->bi_private = pkt;
  899. p = (f * CD_FRAMESIZE) / PAGE_SIZE;
  900. offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
  901. pkt_dbg(2, pd, "Adding frame %d, page:%p offs:%d\n",
  902. f, pkt->pages[p], offset);
  903. if (!bio_add_page(bio, pkt->pages[p], CD_FRAMESIZE, offset))
  904. BUG();
  905. atomic_inc(&pkt->io_wait);
  906. bio_set_op_attrs(bio, REQ_OP_READ, 0);
  907. pkt_queue_bio(pd, bio);
  908. frames_read++;
  909. }
  910. out_account:
  911. pkt_dbg(2, pd, "need %d frames for zone %llx\n",
  912. frames_read, (unsigned long long)pkt->sector);
  913. pd->stats.pkt_started++;
  914. pd->stats.secs_rg += frames_read * (CD_FRAMESIZE >> 9);
  915. }
  916. /*
  917. * Find a packet matching zone, or the least recently used packet if
  918. * there is no match.
  919. */
  920. static struct packet_data *pkt_get_packet_data(struct pktcdvd_device *pd, int zone)
  921. {
  922. struct packet_data *pkt;
  923. list_for_each_entry(pkt, &pd->cdrw.pkt_free_list, list) {
  924. if (pkt->sector == zone || pkt->list.next == &pd->cdrw.pkt_free_list) {
  925. list_del_init(&pkt->list);
  926. if (pkt->sector != zone)
  927. pkt->cache_valid = 0;
  928. return pkt;
  929. }
  930. }
  931. BUG();
  932. return NULL;
  933. }
  934. static void pkt_put_packet_data(struct pktcdvd_device *pd, struct packet_data *pkt)
  935. {
  936. if (pkt->cache_valid) {
  937. list_add(&pkt->list, &pd->cdrw.pkt_free_list);
  938. } else {
  939. list_add_tail(&pkt->list, &pd->cdrw.pkt_free_list);
  940. }
  941. }
  942. /*
  943. * recover a failed write, query for relocation if possible
  944. *
  945. * returns 1 if recovery is possible, or 0 if not
  946. *
  947. */
  948. static int pkt_start_recovery(struct packet_data *pkt)
  949. {
  950. /*
  951. * FIXME. We need help from the file system to implement
  952. * recovery handling.
  953. */
  954. return 0;
  955. #if 0
  956. struct request *rq = pkt->rq;
  957. struct pktcdvd_device *pd = rq->rq_disk->private_data;
  958. struct block_device *pkt_bdev;
  959. struct super_block *sb = NULL;
  960. unsigned long old_block, new_block;
  961. sector_t new_sector;
  962. pkt_bdev = bdget(kdev_t_to_nr(pd->pkt_dev));
  963. if (pkt_bdev) {
  964. sb = get_super(pkt_bdev);
  965. bdput(pkt_bdev);
  966. }
  967. if (!sb)
  968. return 0;
  969. if (!sb->s_op->relocate_blocks)
  970. goto out;
  971. old_block = pkt->sector / (CD_FRAMESIZE >> 9);
  972. if (sb->s_op->relocate_blocks(sb, old_block, &new_block))
  973. goto out;
  974. new_sector = new_block * (CD_FRAMESIZE >> 9);
  975. pkt->sector = new_sector;
  976. bio_reset(pkt->bio);
  977. bio_set_dev(pkt->bio, pd->bdev);
  978. bio_set_op_attrs(pkt->bio, REQ_OP_WRITE, 0);
  979. pkt->bio->bi_iter.bi_sector = new_sector;
  980. pkt->bio->bi_iter.bi_size = pkt->frames * CD_FRAMESIZE;
  981. pkt->bio->bi_vcnt = pkt->frames;
  982. pkt->bio->bi_end_io = pkt_end_io_packet_write;
  983. pkt->bio->bi_private = pkt;
  984. drop_super(sb);
  985. return 1;
  986. out:
  987. drop_super(sb);
  988. return 0;
  989. #endif
  990. }
  991. static inline void pkt_set_state(struct packet_data *pkt, enum packet_data_state state)
  992. {
  993. #if PACKET_DEBUG > 1
  994. static const char *state_name[] = {
  995. "IDLE", "WAITING", "READ_WAIT", "WRITE_WAIT", "RECOVERY", "FINISHED"
  996. };
  997. enum packet_data_state old_state = pkt->state;
  998. pkt_dbg(2, pd, "pkt %2d : s=%6llx %s -> %s\n",
  999. pkt->id, (unsigned long long)pkt->sector,
  1000. state_name[old_state], state_name[state]);
  1001. #endif
  1002. pkt->state = state;
  1003. }
  1004. /*
  1005. * Scan the work queue to see if we can start a new packet.
  1006. * returns non-zero if any work was done.
  1007. */
  1008. static int pkt_handle_queue(struct pktcdvd_device *pd)
  1009. {
  1010. struct packet_data *pkt, *p;
  1011. struct bio *bio = NULL;
  1012. sector_t zone = 0; /* Suppress gcc warning */
  1013. struct pkt_rb_node *node, *first_node;
  1014. struct rb_node *n;
  1015. int wakeup;
  1016. atomic_set(&pd->scan_queue, 0);
  1017. if (list_empty(&pd->cdrw.pkt_free_list)) {
  1018. pkt_dbg(2, pd, "no pkt\n");
  1019. return 0;
  1020. }
  1021. /*
  1022. * Try to find a zone we are not already working on.
  1023. */
  1024. spin_lock(&pd->lock);
  1025. first_node = pkt_rbtree_find(pd, pd->current_sector);
  1026. if (!first_node) {
  1027. n = rb_first(&pd->bio_queue);
  1028. if (n)
  1029. first_node = rb_entry(n, struct pkt_rb_node, rb_node);
  1030. }
  1031. node = first_node;
  1032. while (node) {
  1033. bio = node->bio;
  1034. zone = get_zone(bio->bi_iter.bi_sector, pd);
  1035. list_for_each_entry(p, &pd->cdrw.pkt_active_list, list) {
  1036. if (p->sector == zone) {
  1037. bio = NULL;
  1038. goto try_next_bio;
  1039. }
  1040. }
  1041. break;
  1042. try_next_bio:
  1043. node = pkt_rbtree_next(node);
  1044. if (!node) {
  1045. n = rb_first(&pd->bio_queue);
  1046. if (n)
  1047. node = rb_entry(n, struct pkt_rb_node, rb_node);
  1048. }
  1049. if (node == first_node)
  1050. node = NULL;
  1051. }
  1052. spin_unlock(&pd->lock);
  1053. if (!bio) {
  1054. pkt_dbg(2, pd, "no bio\n");
  1055. return 0;
  1056. }
  1057. pkt = pkt_get_packet_data(pd, zone);
  1058. pd->current_sector = zone + pd->settings.size;
  1059. pkt->sector = zone;
  1060. BUG_ON(pkt->frames != pd->settings.size >> 2);
  1061. pkt->write_size = 0;
  1062. /*
  1063. * Scan work queue for bios in the same zone and link them
  1064. * to this packet.
  1065. */
  1066. spin_lock(&pd->lock);
  1067. pkt_dbg(2, pd, "looking for zone %llx\n", (unsigned long long)zone);
  1068. while ((node = pkt_rbtree_find(pd, zone)) != NULL) {
  1069. bio = node->bio;
  1070. pkt_dbg(2, pd, "found zone=%llx\n", (unsigned long long)
  1071. get_zone(bio->bi_iter.bi_sector, pd));
  1072. if (get_zone(bio->bi_iter.bi_sector, pd) != zone)
  1073. break;
  1074. pkt_rbtree_erase(pd, node);
  1075. spin_lock(&pkt->lock);
  1076. bio_list_add(&pkt->orig_bios, bio);
  1077. pkt->write_size += bio->bi_iter.bi_size / CD_FRAMESIZE;
  1078. spin_unlock(&pkt->lock);
  1079. }
  1080. /* check write congestion marks, and if bio_queue_size is
  1081. below, wake up any waiters */
  1082. wakeup = (pd->write_congestion_on > 0
  1083. && pd->bio_queue_size <= pd->write_congestion_off);
  1084. spin_unlock(&pd->lock);
  1085. if (wakeup) {
  1086. clear_bdi_congested(pd->disk->queue->backing_dev_info,
  1087. BLK_RW_ASYNC);
  1088. }
  1089. pkt->sleep_time = max(PACKET_WAIT_TIME, 1);
  1090. pkt_set_state(pkt, PACKET_WAITING_STATE);
  1091. atomic_set(&pkt->run_sm, 1);
  1092. spin_lock(&pd->cdrw.active_list_lock);
  1093. list_add(&pkt->list, &pd->cdrw.pkt_active_list);
  1094. spin_unlock(&pd->cdrw.active_list_lock);
  1095. return 1;
  1096. }
  1097. /*
  1098. * Assemble a bio to write one packet and queue the bio for processing
  1099. * by the underlying block device.
  1100. */
  1101. static void pkt_start_write(struct pktcdvd_device *pd, struct packet_data *pkt)
  1102. {
  1103. int f;
  1104. bio_reset(pkt->w_bio);
  1105. pkt->w_bio->bi_iter.bi_sector = pkt->sector;
  1106. bio_set_dev(pkt->w_bio, pd->bdev);
  1107. pkt->w_bio->bi_end_io = pkt_end_io_packet_write;
  1108. pkt->w_bio->bi_private = pkt;
  1109. /* XXX: locking? */
  1110. for (f = 0; f < pkt->frames; f++) {
  1111. struct page *page = pkt->pages[(f * CD_FRAMESIZE) / PAGE_SIZE];
  1112. unsigned offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
  1113. if (!bio_add_page(pkt->w_bio, page, CD_FRAMESIZE, offset))
  1114. BUG();
  1115. }
  1116. pkt_dbg(2, pd, "vcnt=%d\n", pkt->w_bio->bi_vcnt);
  1117. /*
  1118. * Fill-in bvec with data from orig_bios.
  1119. */
  1120. spin_lock(&pkt->lock);
  1121. bio_list_copy_data(pkt->w_bio, pkt->orig_bios.head);
  1122. pkt_set_state(pkt, PACKET_WRITE_WAIT_STATE);
  1123. spin_unlock(&pkt->lock);
  1124. pkt_dbg(2, pd, "Writing %d frames for zone %llx\n",
  1125. pkt->write_size, (unsigned long long)pkt->sector);
  1126. if (test_bit(PACKET_MERGE_SEGS, &pd->flags) || (pkt->write_size < pkt->frames))
  1127. pkt->cache_valid = 1;
  1128. else
  1129. pkt->cache_valid = 0;
  1130. /* Start the write request */
  1131. atomic_set(&pkt->io_wait, 1);
  1132. bio_set_op_attrs(pkt->w_bio, REQ_OP_WRITE, 0);
  1133. pkt_queue_bio(pd, pkt->w_bio);
  1134. }
  1135. static void pkt_finish_packet(struct packet_data *pkt, blk_status_t status)
  1136. {
  1137. struct bio *bio;
  1138. if (status)
  1139. pkt->cache_valid = 0;
  1140. /* Finish all bios corresponding to this packet */
  1141. while ((bio = bio_list_pop(&pkt->orig_bios))) {
  1142. bio->bi_status = status;
  1143. bio_endio(bio);
  1144. }
  1145. }
  1146. static void pkt_run_state_machine(struct pktcdvd_device *pd, struct packet_data *pkt)
  1147. {
  1148. pkt_dbg(2, pd, "pkt %d\n", pkt->id);
  1149. for (;;) {
  1150. switch (pkt->state) {
  1151. case PACKET_WAITING_STATE:
  1152. if ((pkt->write_size < pkt->frames) && (pkt->sleep_time > 0))
  1153. return;
  1154. pkt->sleep_time = 0;
  1155. pkt_gather_data(pd, pkt);
  1156. pkt_set_state(pkt, PACKET_READ_WAIT_STATE);
  1157. break;
  1158. case PACKET_READ_WAIT_STATE:
  1159. if (atomic_read(&pkt->io_wait) > 0)
  1160. return;
  1161. if (atomic_read(&pkt->io_errors) > 0) {
  1162. pkt_set_state(pkt, PACKET_RECOVERY_STATE);
  1163. } else {
  1164. pkt_start_write(pd, pkt);
  1165. }
  1166. break;
  1167. case PACKET_WRITE_WAIT_STATE:
  1168. if (atomic_read(&pkt->io_wait) > 0)
  1169. return;
  1170. if (!pkt->w_bio->bi_status) {
  1171. pkt_set_state(pkt, PACKET_FINISHED_STATE);
  1172. } else {
  1173. pkt_set_state(pkt, PACKET_RECOVERY_STATE);
  1174. }
  1175. break;
  1176. case PACKET_RECOVERY_STATE:
  1177. if (pkt_start_recovery(pkt)) {
  1178. pkt_start_write(pd, pkt);
  1179. } else {
  1180. pkt_dbg(2, pd, "No recovery possible\n");
  1181. pkt_set_state(pkt, PACKET_FINISHED_STATE);
  1182. }
  1183. break;
  1184. case PACKET_FINISHED_STATE:
  1185. pkt_finish_packet(pkt, pkt->w_bio->bi_status);
  1186. return;
  1187. default:
  1188. BUG();
  1189. break;
  1190. }
  1191. }
  1192. }
  1193. static void pkt_handle_packets(struct pktcdvd_device *pd)
  1194. {
  1195. struct packet_data *pkt, *next;
  1196. /*
  1197. * Run state machine for active packets
  1198. */
  1199. list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
  1200. if (atomic_read(&pkt->run_sm) > 0) {
  1201. atomic_set(&pkt->run_sm, 0);
  1202. pkt_run_state_machine(pd, pkt);
  1203. }
  1204. }
  1205. /*
  1206. * Move no longer active packets to the free list
  1207. */
  1208. spin_lock(&pd->cdrw.active_list_lock);
  1209. list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_active_list, list) {
  1210. if (pkt->state == PACKET_FINISHED_STATE) {
  1211. list_del(&pkt->list);
  1212. pkt_put_packet_data(pd, pkt);
  1213. pkt_set_state(pkt, PACKET_IDLE_STATE);
  1214. atomic_set(&pd->scan_queue, 1);
  1215. }
  1216. }
  1217. spin_unlock(&pd->cdrw.active_list_lock);
  1218. }
  1219. static void pkt_count_states(struct pktcdvd_device *pd, int *states)
  1220. {
  1221. struct packet_data *pkt;
  1222. int i;
  1223. for (i = 0; i < PACKET_NUM_STATES; i++)
  1224. states[i] = 0;
  1225. spin_lock(&pd->cdrw.active_list_lock);
  1226. list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
  1227. states[pkt->state]++;
  1228. }
  1229. spin_unlock(&pd->cdrw.active_list_lock);
  1230. }
  1231. /*
  1232. * kcdrwd is woken up when writes have been queued for one of our
  1233. * registered devices
  1234. */
  1235. static int kcdrwd(void *foobar)
  1236. {
  1237. struct pktcdvd_device *pd = foobar;
  1238. struct packet_data *pkt;
  1239. long min_sleep_time, residue;
  1240. set_user_nice(current, MIN_NICE);
  1241. set_freezable();
  1242. for (;;) {
  1243. DECLARE_WAITQUEUE(wait, current);
  1244. /*
  1245. * Wait until there is something to do
  1246. */
  1247. add_wait_queue(&pd->wqueue, &wait);
  1248. for (;;) {
  1249. set_current_state(TASK_INTERRUPTIBLE);
  1250. /* Check if we need to run pkt_handle_queue */
  1251. if (atomic_read(&pd->scan_queue) > 0)
  1252. goto work_to_do;
  1253. /* Check if we need to run the state machine for some packet */
  1254. list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
  1255. if (atomic_read(&pkt->run_sm) > 0)
  1256. goto work_to_do;
  1257. }
  1258. /* Check if we need to process the iosched queues */
  1259. if (atomic_read(&pd->iosched.attention) != 0)
  1260. goto work_to_do;
  1261. /* Otherwise, go to sleep */
  1262. if (PACKET_DEBUG > 1) {
  1263. int states[PACKET_NUM_STATES];
  1264. pkt_count_states(pd, states);
  1265. pkt_dbg(2, pd, "i:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
  1266. states[0], states[1], states[2],
  1267. states[3], states[4], states[5]);
  1268. }
  1269. min_sleep_time = MAX_SCHEDULE_TIMEOUT;
  1270. list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
  1271. if (pkt->sleep_time && pkt->sleep_time < min_sleep_time)
  1272. min_sleep_time = pkt->sleep_time;
  1273. }
  1274. pkt_dbg(2, pd, "sleeping\n");
  1275. residue = schedule_timeout(min_sleep_time);
  1276. pkt_dbg(2, pd, "wake up\n");
  1277. /* make swsusp happy with our thread */
  1278. try_to_freeze();
  1279. list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
  1280. if (!pkt->sleep_time)
  1281. continue;
  1282. pkt->sleep_time -= min_sleep_time - residue;
  1283. if (pkt->sleep_time <= 0) {
  1284. pkt->sleep_time = 0;
  1285. atomic_inc(&pkt->run_sm);
  1286. }
  1287. }
  1288. if (kthread_should_stop())
  1289. break;
  1290. }
  1291. work_to_do:
  1292. set_current_state(TASK_RUNNING);
  1293. remove_wait_queue(&pd->wqueue, &wait);
  1294. if (kthread_should_stop())
  1295. break;
  1296. /*
  1297. * if pkt_handle_queue returns true, we can queue
  1298. * another request.
  1299. */
  1300. while (pkt_handle_queue(pd))
  1301. ;
  1302. /*
  1303. * Handle packet state machine
  1304. */
  1305. pkt_handle_packets(pd);
  1306. /*
  1307. * Handle iosched queues
  1308. */
  1309. pkt_iosched_process_queue(pd);
  1310. }
  1311. return 0;
  1312. }
  1313. static void pkt_print_settings(struct pktcdvd_device *pd)
  1314. {
  1315. pkt_info(pd, "%s packets, %u blocks, Mode-%c disc\n",
  1316. pd->settings.fp ? "Fixed" : "Variable",
  1317. pd->settings.size >> 2,
  1318. pd->settings.block_mode == 8 ? '1' : '2');
  1319. }
  1320. static int pkt_mode_sense(struct pktcdvd_device *pd, struct packet_command *cgc, int page_code, int page_control)
  1321. {
  1322. memset(cgc->cmd, 0, sizeof(cgc->cmd));
  1323. cgc->cmd[0] = GPCMD_MODE_SENSE_10;
  1324. cgc->cmd[2] = page_code | (page_control << 6);
  1325. cgc->cmd[7] = cgc->buflen >> 8;
  1326. cgc->cmd[8] = cgc->buflen & 0xff;
  1327. cgc->data_direction = CGC_DATA_READ;
  1328. return pkt_generic_packet(pd, cgc);
  1329. }
  1330. static int pkt_mode_select(struct pktcdvd_device *pd, struct packet_command *cgc)
  1331. {
  1332. memset(cgc->cmd, 0, sizeof(cgc->cmd));
  1333. memset(cgc->buffer, 0, 2);
  1334. cgc->cmd[0] = GPCMD_MODE_SELECT_10;
  1335. cgc->cmd[1] = 0x10; /* PF */
  1336. cgc->cmd[7] = cgc->buflen >> 8;
  1337. cgc->cmd[8] = cgc->buflen & 0xff;
  1338. cgc->data_direction = CGC_DATA_WRITE;
  1339. return pkt_generic_packet(pd, cgc);
  1340. }
  1341. static int pkt_get_disc_info(struct pktcdvd_device *pd, disc_information *di)
  1342. {
  1343. struct packet_command cgc;
  1344. int ret;
  1345. /* set up command and get the disc info */
  1346. init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
  1347. cgc.cmd[0] = GPCMD_READ_DISC_INFO;
  1348. cgc.cmd[8] = cgc.buflen = 2;
  1349. cgc.quiet = 1;
  1350. ret = pkt_generic_packet(pd, &cgc);
  1351. if (ret)
  1352. return ret;
  1353. /* not all drives have the same disc_info length, so requeue
  1354. * packet with the length the drive tells us it can supply
  1355. */
  1356. cgc.buflen = be16_to_cpu(di->disc_information_length) +
  1357. sizeof(di->disc_information_length);
  1358. if (cgc.buflen > sizeof(disc_information))
  1359. cgc.buflen = sizeof(disc_information);
  1360. cgc.cmd[8] = cgc.buflen;
  1361. return pkt_generic_packet(pd, &cgc);
  1362. }
  1363. static int pkt_get_track_info(struct pktcdvd_device *pd, __u16 track, __u8 type, track_information *ti)
  1364. {
  1365. struct packet_command cgc;
  1366. int ret;
  1367. init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
  1368. cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
  1369. cgc.cmd[1] = type & 3;
  1370. cgc.cmd[4] = (track & 0xff00) >> 8;
  1371. cgc.cmd[5] = track & 0xff;
  1372. cgc.cmd[8] = 8;
  1373. cgc.quiet = 1;
  1374. ret = pkt_generic_packet(pd, &cgc);
  1375. if (ret)
  1376. return ret;
  1377. cgc.buflen = be16_to_cpu(ti->track_information_length) +
  1378. sizeof(ti->track_information_length);
  1379. if (cgc.buflen > sizeof(track_information))
  1380. cgc.buflen = sizeof(track_information);
  1381. cgc.cmd[8] = cgc.buflen;
  1382. return pkt_generic_packet(pd, &cgc);
  1383. }
  1384. static noinline_for_stack int pkt_get_last_written(struct pktcdvd_device *pd,
  1385. long *last_written)
  1386. {
  1387. disc_information di;
  1388. track_information ti;
  1389. __u32 last_track;
  1390. int ret = -1;
  1391. ret = pkt_get_disc_info(pd, &di);
  1392. if (ret)
  1393. return ret;
  1394. last_track = (di.last_track_msb << 8) | di.last_track_lsb;
  1395. ret = pkt_get_track_info(pd, last_track, 1, &ti);
  1396. if (ret)
  1397. return ret;
  1398. /* if this track is blank, try the previous. */
  1399. if (ti.blank) {
  1400. last_track--;
  1401. ret = pkt_get_track_info(pd, last_track, 1, &ti);
  1402. if (ret)
  1403. return ret;
  1404. }
  1405. /* if last recorded field is valid, return it. */
  1406. if (ti.lra_v) {
  1407. *last_written = be32_to_cpu(ti.last_rec_address);
  1408. } else {
  1409. /* make it up instead */
  1410. *last_written = be32_to_cpu(ti.track_start) +
  1411. be32_to_cpu(ti.track_size);
  1412. if (ti.free_blocks)
  1413. *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
  1414. }
  1415. return 0;
  1416. }
  1417. /*
  1418. * write mode select package based on pd->settings
  1419. */
  1420. static noinline_for_stack int pkt_set_write_settings(struct pktcdvd_device *pd)
  1421. {
  1422. struct packet_command cgc;
  1423. struct scsi_sense_hdr sshdr;
  1424. write_param_page *wp;
  1425. char buffer[128];
  1426. int ret, size;
  1427. /* doesn't apply to DVD+RW or DVD-RAM */
  1428. if ((pd->mmc3_profile == 0x1a) || (pd->mmc3_profile == 0x12))
  1429. return 0;
  1430. memset(buffer, 0, sizeof(buffer));
  1431. init_cdrom_command(&cgc, buffer, sizeof(*wp), CGC_DATA_READ);
  1432. cgc.sshdr = &sshdr;
  1433. ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0);
  1434. if (ret) {
  1435. pkt_dump_sense(pd, &cgc);
  1436. return ret;
  1437. }
  1438. size = 2 + ((buffer[0] << 8) | (buffer[1] & 0xff));
  1439. pd->mode_offset = (buffer[6] << 8) | (buffer[7] & 0xff);
  1440. if (size > sizeof(buffer))
  1441. size = sizeof(buffer);
  1442. /*
  1443. * now get it all
  1444. */
  1445. init_cdrom_command(&cgc, buffer, size, CGC_DATA_READ);
  1446. cgc.sshdr = &sshdr;
  1447. ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0);
  1448. if (ret) {
  1449. pkt_dump_sense(pd, &cgc);
  1450. return ret;
  1451. }
  1452. /*
  1453. * write page is offset header + block descriptor length
  1454. */
  1455. wp = (write_param_page *) &buffer[sizeof(struct mode_page_header) + pd->mode_offset];
  1456. wp->fp = pd->settings.fp;
  1457. wp->track_mode = pd->settings.track_mode;
  1458. wp->write_type = pd->settings.write_type;
  1459. wp->data_block_type = pd->settings.block_mode;
  1460. wp->multi_session = 0;
  1461. #ifdef PACKET_USE_LS
  1462. wp->link_size = 7;
  1463. wp->ls_v = 1;
  1464. #endif
  1465. if (wp->data_block_type == PACKET_BLOCK_MODE1) {
  1466. wp->session_format = 0;
  1467. wp->subhdr2 = 0x20;
  1468. } else if (wp->data_block_type == PACKET_BLOCK_MODE2) {
  1469. wp->session_format = 0x20;
  1470. wp->subhdr2 = 8;
  1471. #if 0
  1472. wp->mcn[0] = 0x80;
  1473. memcpy(&wp->mcn[1], PACKET_MCN, sizeof(wp->mcn) - 1);
  1474. #endif
  1475. } else {
  1476. /*
  1477. * paranoia
  1478. */
  1479. pkt_err(pd, "write mode wrong %d\n", wp->data_block_type);
  1480. return 1;
  1481. }
  1482. wp->packet_size = cpu_to_be32(pd->settings.size >> 2);
  1483. cgc.buflen = cgc.cmd[8] = size;
  1484. ret = pkt_mode_select(pd, &cgc);
  1485. if (ret) {
  1486. pkt_dump_sense(pd, &cgc);
  1487. return ret;
  1488. }
  1489. pkt_print_settings(pd);
  1490. return 0;
  1491. }
  1492. /*
  1493. * 1 -- we can write to this track, 0 -- we can't
  1494. */
  1495. static int pkt_writable_track(struct pktcdvd_device *pd, track_information *ti)
  1496. {
  1497. switch (pd->mmc3_profile) {
  1498. case 0x1a: /* DVD+RW */
  1499. case 0x12: /* DVD-RAM */
  1500. /* The track is always writable on DVD+RW/DVD-RAM */
  1501. return 1;
  1502. default:
  1503. break;
  1504. }
  1505. if (!ti->packet || !ti->fp)
  1506. return 0;
  1507. /*
  1508. * "good" settings as per Mt Fuji.
  1509. */
  1510. if (ti->rt == 0 && ti->blank == 0)
  1511. return 1;
  1512. if (ti->rt == 0 && ti->blank == 1)
  1513. return 1;
  1514. if (ti->rt == 1 && ti->blank == 0)
  1515. return 1;
  1516. pkt_err(pd, "bad state %d-%d-%d\n", ti->rt, ti->blank, ti->packet);
  1517. return 0;
  1518. }
  1519. /*
  1520. * 1 -- we can write to this disc, 0 -- we can't
  1521. */
  1522. static int pkt_writable_disc(struct pktcdvd_device *pd, disc_information *di)
  1523. {
  1524. switch (pd->mmc3_profile) {
  1525. case 0x0a: /* CD-RW */
  1526. case 0xffff: /* MMC3 not supported */
  1527. break;
  1528. case 0x1a: /* DVD+RW */
  1529. case 0x13: /* DVD-RW */
  1530. case 0x12: /* DVD-RAM */
  1531. return 1;
  1532. default:
  1533. pkt_dbg(2, pd, "Wrong disc profile (%x)\n",
  1534. pd->mmc3_profile);
  1535. return 0;
  1536. }
  1537. /*
  1538. * for disc type 0xff we should probably reserve a new track.
  1539. * but i'm not sure, should we leave this to user apps? probably.
  1540. */
  1541. if (di->disc_type == 0xff) {
  1542. pkt_notice(pd, "unknown disc - no track?\n");
  1543. return 0;
  1544. }
  1545. if (di->disc_type != 0x20 && di->disc_type != 0) {
  1546. pkt_err(pd, "wrong disc type (%x)\n", di->disc_type);
  1547. return 0;
  1548. }
  1549. if (di->erasable == 0) {
  1550. pkt_notice(pd, "disc not erasable\n");
  1551. return 0;
  1552. }
  1553. if (di->border_status == PACKET_SESSION_RESERVED) {
  1554. pkt_err(pd, "can't write to last track (reserved)\n");
  1555. return 0;
  1556. }
  1557. return 1;
  1558. }
  1559. static noinline_for_stack int pkt_probe_settings(struct pktcdvd_device *pd)
  1560. {
  1561. struct packet_command cgc;
  1562. unsigned char buf[12];
  1563. disc_information di;
  1564. track_information ti;
  1565. int ret, track;
  1566. init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
  1567. cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
  1568. cgc.cmd[8] = 8;
  1569. ret = pkt_generic_packet(pd, &cgc);
  1570. pd->mmc3_profile = ret ? 0xffff : buf[6] << 8 | buf[7];
  1571. memset(&di, 0, sizeof(disc_information));
  1572. memset(&ti, 0, sizeof(track_information));
  1573. ret = pkt_get_disc_info(pd, &di);
  1574. if (ret) {
  1575. pkt_err(pd, "failed get_disc\n");
  1576. return ret;
  1577. }
  1578. if (!pkt_writable_disc(pd, &di))
  1579. return -EROFS;
  1580. pd->type = di.erasable ? PACKET_CDRW : PACKET_CDR;
  1581. track = 1; /* (di.last_track_msb << 8) | di.last_track_lsb; */
  1582. ret = pkt_get_track_info(pd, track, 1, &ti);
  1583. if (ret) {
  1584. pkt_err(pd, "failed get_track\n");
  1585. return ret;
  1586. }
  1587. if (!pkt_writable_track(pd, &ti)) {
  1588. pkt_err(pd, "can't write to this track\n");
  1589. return -EROFS;
  1590. }
  1591. /*
  1592. * we keep packet size in 512 byte units, makes it easier to
  1593. * deal with request calculations.
  1594. */
  1595. pd->settings.size = be32_to_cpu(ti.fixed_packet_size) << 2;
  1596. if (pd->settings.size == 0) {
  1597. pkt_notice(pd, "detected zero packet size!\n");
  1598. return -ENXIO;
  1599. }
  1600. if (pd->settings.size > PACKET_MAX_SECTORS) {
  1601. pkt_err(pd, "packet size is too big\n");
  1602. return -EROFS;
  1603. }
  1604. pd->settings.fp = ti.fp;
  1605. pd->offset = (be32_to_cpu(ti.track_start) << 2) & (pd->settings.size - 1);
  1606. if (ti.nwa_v) {
  1607. pd->nwa = be32_to_cpu(ti.next_writable);
  1608. set_bit(PACKET_NWA_VALID, &pd->flags);
  1609. }
  1610. /*
  1611. * in theory we could use lra on -RW media as well and just zero
  1612. * blocks that haven't been written yet, but in practice that
  1613. * is just a no-go. we'll use that for -R, naturally.
  1614. */
  1615. if (ti.lra_v) {
  1616. pd->lra = be32_to_cpu(ti.last_rec_address);
  1617. set_bit(PACKET_LRA_VALID, &pd->flags);
  1618. } else {
  1619. pd->lra = 0xffffffff;
  1620. set_bit(PACKET_LRA_VALID, &pd->flags);
  1621. }
  1622. /*
  1623. * fine for now
  1624. */
  1625. pd->settings.link_loss = 7;
  1626. pd->settings.write_type = 0; /* packet */
  1627. pd->settings.track_mode = ti.track_mode;
  1628. /*
  1629. * mode1 or mode2 disc
  1630. */
  1631. switch (ti.data_mode) {
  1632. case PACKET_MODE1:
  1633. pd->settings.block_mode = PACKET_BLOCK_MODE1;
  1634. break;
  1635. case PACKET_MODE2:
  1636. pd->settings.block_mode = PACKET_BLOCK_MODE2;
  1637. break;
  1638. default:
  1639. pkt_err(pd, "unknown data mode\n");
  1640. return -EROFS;
  1641. }
  1642. return 0;
  1643. }
  1644. /*
  1645. * enable/disable write caching on drive
  1646. */
  1647. static noinline_for_stack int pkt_write_caching(struct pktcdvd_device *pd,
  1648. int set)
  1649. {
  1650. struct packet_command cgc;
  1651. struct scsi_sense_hdr sshdr;
  1652. unsigned char buf[64];
  1653. int ret;
  1654. init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
  1655. cgc.sshdr = &sshdr;
  1656. cgc.buflen = pd->mode_offset + 12;
  1657. /*
  1658. * caching mode page might not be there, so quiet this command
  1659. */
  1660. cgc.quiet = 1;
  1661. ret = pkt_mode_sense(pd, &cgc, GPMODE_WCACHING_PAGE, 0);
  1662. if (ret)
  1663. return ret;
  1664. buf[pd->mode_offset + 10] |= (!!set << 2);
  1665. cgc.buflen = cgc.cmd[8] = 2 + ((buf[0] << 8) | (buf[1] & 0xff));
  1666. ret = pkt_mode_select(pd, &cgc);
  1667. if (ret) {
  1668. pkt_err(pd, "write caching control failed\n");
  1669. pkt_dump_sense(pd, &cgc);
  1670. } else if (!ret && set)
  1671. pkt_notice(pd, "enabled write caching\n");
  1672. return ret;
  1673. }
  1674. static int pkt_lock_door(struct pktcdvd_device *pd, int lockflag)
  1675. {
  1676. struct packet_command cgc;
  1677. init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
  1678. cgc.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
  1679. cgc.cmd[4] = lockflag ? 1 : 0;
  1680. return pkt_generic_packet(pd, &cgc);
  1681. }
  1682. /*
  1683. * Returns drive maximum write speed
  1684. */
  1685. static noinline_for_stack int pkt_get_max_speed(struct pktcdvd_device *pd,
  1686. unsigned *write_speed)
  1687. {
  1688. struct packet_command cgc;
  1689. struct scsi_sense_hdr sshdr;
  1690. unsigned char buf[256+18];
  1691. unsigned char *cap_buf;
  1692. int ret, offset;
  1693. cap_buf = &buf[sizeof(struct mode_page_header) + pd->mode_offset];
  1694. init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_UNKNOWN);
  1695. cgc.sshdr = &sshdr;
  1696. ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
  1697. if (ret) {
  1698. cgc.buflen = pd->mode_offset + cap_buf[1] + 2 +
  1699. sizeof(struct mode_page_header);
  1700. ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
  1701. if (ret) {
  1702. pkt_dump_sense(pd, &cgc);
  1703. return ret;
  1704. }
  1705. }
  1706. offset = 20; /* Obsoleted field, used by older drives */
  1707. if (cap_buf[1] >= 28)
  1708. offset = 28; /* Current write speed selected */
  1709. if (cap_buf[1] >= 30) {
  1710. /* If the drive reports at least one "Logical Unit Write
  1711. * Speed Performance Descriptor Block", use the information
  1712. * in the first block. (contains the highest speed)
  1713. */
  1714. int num_spdb = (cap_buf[30] << 8) + cap_buf[31];
  1715. if (num_spdb > 0)
  1716. offset = 34;
  1717. }
  1718. *write_speed = (cap_buf[offset] << 8) | cap_buf[offset + 1];
  1719. return 0;
  1720. }
  1721. /* These tables from cdrecord - I don't have orange book */
  1722. /* standard speed CD-RW (1-4x) */
  1723. static char clv_to_speed[16] = {
  1724. /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
  1725. 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  1726. };
  1727. /* high speed CD-RW (-10x) */
  1728. static char hs_clv_to_speed[16] = {
  1729. /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
  1730. 0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  1731. };
  1732. /* ultra high speed CD-RW */
  1733. static char us_clv_to_speed[16] = {
  1734. /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
  1735. 0, 2, 4, 8, 0, 0,16, 0,24,32,40,48, 0, 0, 0, 0
  1736. };
  1737. /*
  1738. * reads the maximum media speed from ATIP
  1739. */
  1740. static noinline_for_stack int pkt_media_speed(struct pktcdvd_device *pd,
  1741. unsigned *speed)
  1742. {
  1743. struct packet_command cgc;
  1744. struct scsi_sense_hdr sshdr;
  1745. unsigned char buf[64];
  1746. unsigned int size, st, sp;
  1747. int ret;
  1748. init_cdrom_command(&cgc, buf, 2, CGC_DATA_READ);
  1749. cgc.sshdr = &sshdr;
  1750. cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
  1751. cgc.cmd[1] = 2;
  1752. cgc.cmd[2] = 4; /* READ ATIP */
  1753. cgc.cmd[8] = 2;
  1754. ret = pkt_generic_packet(pd, &cgc);
  1755. if (ret) {
  1756. pkt_dump_sense(pd, &cgc);
  1757. return ret;
  1758. }
  1759. size = ((unsigned int) buf[0]<<8) + buf[1] + 2;
  1760. if (size > sizeof(buf))
  1761. size = sizeof(buf);
  1762. init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
  1763. cgc.sshdr = &sshdr;
  1764. cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
  1765. cgc.cmd[1] = 2;
  1766. cgc.cmd[2] = 4;
  1767. cgc.cmd[8] = size;
  1768. ret = pkt_generic_packet(pd, &cgc);
  1769. if (ret) {
  1770. pkt_dump_sense(pd, &cgc);
  1771. return ret;
  1772. }
  1773. if (!(buf[6] & 0x40)) {
  1774. pkt_notice(pd, "disc type is not CD-RW\n");
  1775. return 1;
  1776. }
  1777. if (!(buf[6] & 0x4)) {
  1778. pkt_notice(pd, "A1 values on media are not valid, maybe not CDRW?\n");
  1779. return 1;
  1780. }
  1781. st = (buf[6] >> 3) & 0x7; /* disc sub-type */
  1782. sp = buf[16] & 0xf; /* max speed from ATIP A1 field */
  1783. /* Info from cdrecord */
  1784. switch (st) {
  1785. case 0: /* standard speed */
  1786. *speed = clv_to_speed[sp];
  1787. break;
  1788. case 1: /* high speed */
  1789. *speed = hs_clv_to_speed[sp];
  1790. break;
  1791. case 2: /* ultra high speed */
  1792. *speed = us_clv_to_speed[sp];
  1793. break;
  1794. default:
  1795. pkt_notice(pd, "unknown disc sub-type %d\n", st);
  1796. return 1;
  1797. }
  1798. if (*speed) {
  1799. pkt_info(pd, "maximum media speed: %d\n", *speed);
  1800. return 0;
  1801. } else {
  1802. pkt_notice(pd, "unknown speed %d for sub-type %d\n", sp, st);
  1803. return 1;
  1804. }
  1805. }
  1806. static noinline_for_stack int pkt_perform_opc(struct pktcdvd_device *pd)
  1807. {
  1808. struct packet_command cgc;
  1809. struct scsi_sense_hdr sshdr;
  1810. int ret;
  1811. pkt_dbg(2, pd, "Performing OPC\n");
  1812. init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
  1813. cgc.sshdr = &sshdr;
  1814. cgc.timeout = 60*HZ;
  1815. cgc.cmd[0] = GPCMD_SEND_OPC;
  1816. cgc.cmd[1] = 1;
  1817. ret = pkt_generic_packet(pd, &cgc);
  1818. if (ret)
  1819. pkt_dump_sense(pd, &cgc);
  1820. return ret;
  1821. }
  1822. static int pkt_open_write(struct pktcdvd_device *pd)
  1823. {
  1824. int ret;
  1825. unsigned int write_speed, media_write_speed, read_speed;
  1826. ret = pkt_probe_settings(pd);
  1827. if (ret) {
  1828. pkt_dbg(2, pd, "failed probe\n");
  1829. return ret;
  1830. }
  1831. ret = pkt_set_write_settings(pd);
  1832. if (ret) {
  1833. pkt_dbg(1, pd, "failed saving write settings\n");
  1834. return -EIO;
  1835. }
  1836. pkt_write_caching(pd, USE_WCACHING);
  1837. ret = pkt_get_max_speed(pd, &write_speed);
  1838. if (ret)
  1839. write_speed = 16 * 177;
  1840. switch (pd->mmc3_profile) {
  1841. case 0x13: /* DVD-RW */
  1842. case 0x1a: /* DVD+RW */
  1843. case 0x12: /* DVD-RAM */
  1844. pkt_dbg(1, pd, "write speed %ukB/s\n", write_speed);
  1845. break;
  1846. default:
  1847. ret = pkt_media_speed(pd, &media_write_speed);
  1848. if (ret)
  1849. media_write_speed = 16;
  1850. write_speed = min(write_speed, media_write_speed * 177);
  1851. pkt_dbg(1, pd, "write speed %ux\n", write_speed / 176);
  1852. break;
  1853. }
  1854. read_speed = write_speed;
  1855. ret = pkt_set_speed(pd, write_speed, read_speed);
  1856. if (ret) {
  1857. pkt_dbg(1, pd, "couldn't set write speed\n");
  1858. return -EIO;
  1859. }
  1860. pd->write_speed = write_speed;
  1861. pd->read_speed = read_speed;
  1862. ret = pkt_perform_opc(pd);
  1863. if (ret) {
  1864. pkt_dbg(1, pd, "Optimum Power Calibration failed\n");
  1865. }
  1866. return 0;
  1867. }
  1868. /*
  1869. * called at open time.
  1870. */
  1871. static int pkt_open_dev(struct pktcdvd_device *pd, fmode_t write)
  1872. {
  1873. int ret;
  1874. long lba;
  1875. struct request_queue *q;
  1876. /*
  1877. * We need to re-open the cdrom device without O_NONBLOCK to be able
  1878. * to read/write from/to it. It is already opened in O_NONBLOCK mode
  1879. * so bdget() can't fail.
  1880. */
  1881. bdget(pd->bdev->bd_dev);
  1882. ret = blkdev_get(pd->bdev, FMODE_READ | FMODE_EXCL, pd);
  1883. if (ret)
  1884. goto out;
  1885. ret = pkt_get_last_written(pd, &lba);
  1886. if (ret) {
  1887. pkt_err(pd, "pkt_get_last_written failed\n");
  1888. goto out_putdev;
  1889. }
  1890. set_capacity(pd->disk, lba << 2);
  1891. set_capacity(pd->bdev->bd_disk, lba << 2);
  1892. bd_set_size(pd->bdev, (loff_t)lba << 11);
  1893. q = bdev_get_queue(pd->bdev);
  1894. if (write) {
  1895. ret = pkt_open_write(pd);
  1896. if (ret)
  1897. goto out_putdev;
  1898. /*
  1899. * Some CDRW drives can not handle writes larger than one packet,
  1900. * even if the size is a multiple of the packet size.
  1901. */
  1902. spin_lock_irq(q->queue_lock);
  1903. blk_queue_max_hw_sectors(q, pd->settings.size);
  1904. spin_unlock_irq(q->queue_lock);
  1905. set_bit(PACKET_WRITABLE, &pd->flags);
  1906. } else {
  1907. pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
  1908. clear_bit(PACKET_WRITABLE, &pd->flags);
  1909. }
  1910. ret = pkt_set_segment_merging(pd, q);
  1911. if (ret)
  1912. goto out_putdev;
  1913. if (write) {
  1914. if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) {
  1915. pkt_err(pd, "not enough memory for buffers\n");
  1916. ret = -ENOMEM;
  1917. goto out_putdev;
  1918. }
  1919. pkt_info(pd, "%lukB available on disc\n", lba << 1);
  1920. }
  1921. return 0;
  1922. out_putdev:
  1923. blkdev_put(pd->bdev, FMODE_READ | FMODE_EXCL);
  1924. out:
  1925. return ret;
  1926. }
  1927. /*
  1928. * called when the device is closed. makes sure that the device flushes
  1929. * the internal cache before we close.
  1930. */
  1931. static void pkt_release_dev(struct pktcdvd_device *pd, int flush)
  1932. {
  1933. if (flush && pkt_flush_cache(pd))
  1934. pkt_dbg(1, pd, "not flushing cache\n");
  1935. pkt_lock_door(pd, 0);
  1936. pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
  1937. blkdev_put(pd->bdev, FMODE_READ | FMODE_EXCL);
  1938. pkt_shrink_pktlist(pd);
  1939. }
  1940. static struct pktcdvd_device *pkt_find_dev_from_minor(unsigned int dev_minor)
  1941. {
  1942. if (dev_minor >= MAX_WRITERS)
  1943. return NULL;
  1944. dev_minor = array_index_nospec(dev_minor, MAX_WRITERS);
  1945. return pkt_devs[dev_minor];
  1946. }
  1947. static int pkt_open(struct block_device *bdev, fmode_t mode)
  1948. {
  1949. struct pktcdvd_device *pd = NULL;
  1950. int ret;
  1951. mutex_lock(&pktcdvd_mutex);
  1952. mutex_lock(&ctl_mutex);
  1953. pd = pkt_find_dev_from_minor(MINOR(bdev->bd_dev));
  1954. if (!pd) {
  1955. ret = -ENODEV;
  1956. goto out;
  1957. }
  1958. BUG_ON(pd->refcnt < 0);
  1959. pd->refcnt++;
  1960. if (pd->refcnt > 1) {
  1961. if ((mode & FMODE_WRITE) &&
  1962. !test_bit(PACKET_WRITABLE, &pd->flags)) {
  1963. ret = -EBUSY;
  1964. goto out_dec;
  1965. }
  1966. } else {
  1967. ret = pkt_open_dev(pd, mode & FMODE_WRITE);
  1968. if (ret)
  1969. goto out_dec;
  1970. /*
  1971. * needed here as well, since ext2 (among others) may change
  1972. * the blocksize at mount time
  1973. */
  1974. set_blocksize(bdev, CD_FRAMESIZE);
  1975. }
  1976. mutex_unlock(&ctl_mutex);
  1977. mutex_unlock(&pktcdvd_mutex);
  1978. return 0;
  1979. out_dec:
  1980. pd->refcnt--;
  1981. out:
  1982. mutex_unlock(&ctl_mutex);
  1983. mutex_unlock(&pktcdvd_mutex);
  1984. return ret;
  1985. }
  1986. static void pkt_close(struct gendisk *disk, fmode_t mode)
  1987. {
  1988. struct pktcdvd_device *pd = disk->private_data;
  1989. mutex_lock(&pktcdvd_mutex);
  1990. mutex_lock(&ctl_mutex);
  1991. pd->refcnt--;
  1992. BUG_ON(pd->refcnt < 0);
  1993. if (pd->refcnt == 0) {
  1994. int flush = test_bit(PACKET_WRITABLE, &pd->flags);
  1995. pkt_release_dev(pd, flush);
  1996. }
  1997. mutex_unlock(&ctl_mutex);
  1998. mutex_unlock(&pktcdvd_mutex);
  1999. }
  2000. static void pkt_end_io_read_cloned(struct bio *bio)
  2001. {
  2002. struct packet_stacked_data *psd = bio->bi_private;
  2003. struct pktcdvd_device *pd = psd->pd;
  2004. psd->bio->bi_status = bio->bi_status;
  2005. bio_put(bio);
  2006. bio_endio(psd->bio);
  2007. mempool_free(psd, &psd_pool);
  2008. pkt_bio_finished(pd);
  2009. }
  2010. static void pkt_make_request_read(struct pktcdvd_device *pd, struct bio *bio)
  2011. {
  2012. struct bio *cloned_bio = bio_clone_fast(bio, GFP_NOIO, &pkt_bio_set);
  2013. struct packet_stacked_data *psd = mempool_alloc(&psd_pool, GFP_NOIO);
  2014. psd->pd = pd;
  2015. psd->bio = bio;
  2016. bio_set_dev(cloned_bio, pd->bdev);
  2017. cloned_bio->bi_private = psd;
  2018. cloned_bio->bi_end_io = pkt_end_io_read_cloned;
  2019. pd->stats.secs_r += bio_sectors(bio);
  2020. pkt_queue_bio(pd, cloned_bio);
  2021. }
  2022. static void pkt_make_request_write(struct request_queue *q, struct bio *bio)
  2023. {
  2024. struct pktcdvd_device *pd = q->queuedata;
  2025. sector_t zone;
  2026. struct packet_data *pkt;
  2027. int was_empty, blocked_bio;
  2028. struct pkt_rb_node *node;
  2029. zone = get_zone(bio->bi_iter.bi_sector, pd);
  2030. /*
  2031. * If we find a matching packet in state WAITING or READ_WAIT, we can
  2032. * just append this bio to that packet.
  2033. */
  2034. spin_lock(&pd->cdrw.active_list_lock);
  2035. blocked_bio = 0;
  2036. list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
  2037. if (pkt->sector == zone) {
  2038. spin_lock(&pkt->lock);
  2039. if ((pkt->state == PACKET_WAITING_STATE) ||
  2040. (pkt->state == PACKET_READ_WAIT_STATE)) {
  2041. bio_list_add(&pkt->orig_bios, bio);
  2042. pkt->write_size +=
  2043. bio->bi_iter.bi_size / CD_FRAMESIZE;
  2044. if ((pkt->write_size >= pkt->frames) &&
  2045. (pkt->state == PACKET_WAITING_STATE)) {
  2046. atomic_inc(&pkt->run_sm);
  2047. wake_up(&pd->wqueue);
  2048. }
  2049. spin_unlock(&pkt->lock);
  2050. spin_unlock(&pd->cdrw.active_list_lock);
  2051. return;
  2052. } else {
  2053. blocked_bio = 1;
  2054. }
  2055. spin_unlock(&pkt->lock);
  2056. }
  2057. }
  2058. spin_unlock(&pd->cdrw.active_list_lock);
  2059. /*
  2060. * Test if there is enough room left in the bio work queue
  2061. * (queue size >= congestion on mark).
  2062. * If not, wait till the work queue size is below the congestion off mark.
  2063. */
  2064. spin_lock(&pd->lock);
  2065. if (pd->write_congestion_on > 0
  2066. && pd->bio_queue_size >= pd->write_congestion_on) {
  2067. set_bdi_congested(q->backing_dev_info, BLK_RW_ASYNC);
  2068. do {
  2069. spin_unlock(&pd->lock);
  2070. congestion_wait(BLK_RW_ASYNC, HZ);
  2071. spin_lock(&pd->lock);
  2072. } while(pd->bio_queue_size > pd->write_congestion_off);
  2073. }
  2074. spin_unlock(&pd->lock);
  2075. /*
  2076. * No matching packet found. Store the bio in the work queue.
  2077. */
  2078. node = mempool_alloc(&pd->rb_pool, GFP_NOIO);
  2079. node->bio = bio;
  2080. spin_lock(&pd->lock);
  2081. BUG_ON(pd->bio_queue_size < 0);
  2082. was_empty = (pd->bio_queue_size == 0);
  2083. pkt_rbtree_insert(pd, node);
  2084. spin_unlock(&pd->lock);
  2085. /*
  2086. * Wake up the worker thread.
  2087. */
  2088. atomic_set(&pd->scan_queue, 1);
  2089. if (was_empty) {
  2090. /* This wake_up is required for correct operation */
  2091. wake_up(&pd->wqueue);
  2092. } else if (!list_empty(&pd->cdrw.pkt_free_list) && !blocked_bio) {
  2093. /*
  2094. * This wake up is not required for correct operation,
  2095. * but improves performance in some cases.
  2096. */
  2097. wake_up(&pd->wqueue);
  2098. }
  2099. }
  2100. static blk_qc_t pkt_make_request(struct request_queue *q, struct bio *bio)
  2101. {
  2102. struct pktcdvd_device *pd;
  2103. char b[BDEVNAME_SIZE];
  2104. struct bio *split;
  2105. blk_queue_split(q, &bio);
  2106. pd = q->queuedata;
  2107. if (!pd) {
  2108. pr_err("%s incorrect request queue\n", bio_devname(bio, b));
  2109. goto end_io;
  2110. }
  2111. pkt_dbg(2, pd, "start = %6llx stop = %6llx\n",
  2112. (unsigned long long)bio->bi_iter.bi_sector,
  2113. (unsigned long long)bio_end_sector(bio));
  2114. /*
  2115. * Clone READ bios so we can have our own bi_end_io callback.
  2116. */
  2117. if (bio_data_dir(bio) == READ) {
  2118. pkt_make_request_read(pd, bio);
  2119. return BLK_QC_T_NONE;
  2120. }
  2121. if (!test_bit(PACKET_WRITABLE, &pd->flags)) {
  2122. pkt_notice(pd, "WRITE for ro device (%llu)\n",
  2123. (unsigned long long)bio->bi_iter.bi_sector);
  2124. goto end_io;
  2125. }
  2126. if (!bio->bi_iter.bi_size || (bio->bi_iter.bi_size % CD_FRAMESIZE)) {
  2127. pkt_err(pd, "wrong bio size\n");
  2128. goto end_io;
  2129. }
  2130. do {
  2131. sector_t zone = get_zone(bio->bi_iter.bi_sector, pd);
  2132. sector_t last_zone = get_zone(bio_end_sector(bio) - 1, pd);
  2133. if (last_zone != zone) {
  2134. BUG_ON(last_zone != zone + pd->settings.size);
  2135. split = bio_split(bio, last_zone -
  2136. bio->bi_iter.bi_sector,
  2137. GFP_NOIO, &pkt_bio_set);
  2138. bio_chain(split, bio);
  2139. } else {
  2140. split = bio;
  2141. }
  2142. pkt_make_request_write(q, split);
  2143. } while (split != bio);
  2144. return BLK_QC_T_NONE;
  2145. end_io:
  2146. bio_io_error(bio);
  2147. return BLK_QC_T_NONE;
  2148. }
  2149. static void pkt_init_queue(struct pktcdvd_device *pd)
  2150. {
  2151. struct request_queue *q = pd->disk->queue;
  2152. blk_queue_make_request(q, pkt_make_request);
  2153. blk_queue_logical_block_size(q, CD_FRAMESIZE);
  2154. blk_queue_max_hw_sectors(q, PACKET_MAX_SECTORS);
  2155. q->queuedata = pd;
  2156. }
  2157. static int pkt_seq_show(struct seq_file *m, void *p)
  2158. {
  2159. struct pktcdvd_device *pd = m->private;
  2160. char *msg;
  2161. char bdev_buf[BDEVNAME_SIZE];
  2162. int states[PACKET_NUM_STATES];
  2163. seq_printf(m, "Writer %s mapped to %s:\n", pd->name,
  2164. bdevname(pd->bdev, bdev_buf));
  2165. seq_printf(m, "\nSettings:\n");
  2166. seq_printf(m, "\tpacket size:\t\t%dkB\n", pd->settings.size / 2);
  2167. if (pd->settings.write_type == 0)
  2168. msg = "Packet";
  2169. else
  2170. msg = "Unknown";
  2171. seq_printf(m, "\twrite type:\t\t%s\n", msg);
  2172. seq_printf(m, "\tpacket type:\t\t%s\n", pd->settings.fp ? "Fixed" : "Variable");
  2173. seq_printf(m, "\tlink loss:\t\t%d\n", pd->settings.link_loss);
  2174. seq_printf(m, "\ttrack mode:\t\t%d\n", pd->settings.track_mode);
  2175. if (pd->settings.block_mode == PACKET_BLOCK_MODE1)
  2176. msg = "Mode 1";
  2177. else if (pd->settings.block_mode == PACKET_BLOCK_MODE2)
  2178. msg = "Mode 2";
  2179. else
  2180. msg = "Unknown";
  2181. seq_printf(m, "\tblock mode:\t\t%s\n", msg);
  2182. seq_printf(m, "\nStatistics:\n");
  2183. seq_printf(m, "\tpackets started:\t%lu\n", pd->stats.pkt_started);
  2184. seq_printf(m, "\tpackets ended:\t\t%lu\n", pd->stats.pkt_ended);
  2185. seq_printf(m, "\twritten:\t\t%lukB\n", pd->stats.secs_w >> 1);
  2186. seq_printf(m, "\tread gather:\t\t%lukB\n", pd->stats.secs_rg >> 1);
  2187. seq_printf(m, "\tread:\t\t\t%lukB\n", pd->stats.secs_r >> 1);
  2188. seq_printf(m, "\nMisc:\n");
  2189. seq_printf(m, "\treference count:\t%d\n", pd->refcnt);
  2190. seq_printf(m, "\tflags:\t\t\t0x%lx\n", pd->flags);
  2191. seq_printf(m, "\tread speed:\t\t%ukB/s\n", pd->read_speed);
  2192. seq_printf(m, "\twrite speed:\t\t%ukB/s\n", pd->write_speed);
  2193. seq_printf(m, "\tstart offset:\t\t%lu\n", pd->offset);
  2194. seq_printf(m, "\tmode page offset:\t%u\n", pd->mode_offset);
  2195. seq_printf(m, "\nQueue state:\n");
  2196. seq_printf(m, "\tbios queued:\t\t%d\n", pd->bio_queue_size);
  2197. seq_printf(m, "\tbios pending:\t\t%d\n", atomic_read(&pd->cdrw.pending_bios));
  2198. seq_printf(m, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd->current_sector);
  2199. pkt_count_states(pd, states);
  2200. seq_printf(m, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
  2201. states[0], states[1], states[2], states[3], states[4], states[5]);
  2202. seq_printf(m, "\twrite congestion marks:\toff=%d on=%d\n",
  2203. pd->write_congestion_off,
  2204. pd->write_congestion_on);
  2205. return 0;
  2206. }
  2207. static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev)
  2208. {
  2209. int i;
  2210. int ret = 0;
  2211. char b[BDEVNAME_SIZE];
  2212. struct block_device *bdev;
  2213. if (pd->pkt_dev == dev) {
  2214. pkt_err(pd, "recursive setup not allowed\n");
  2215. return -EBUSY;
  2216. }
  2217. for (i = 0; i < MAX_WRITERS; i++) {
  2218. struct pktcdvd_device *pd2 = pkt_devs[i];
  2219. if (!pd2)
  2220. continue;
  2221. if (pd2->bdev->bd_dev == dev) {
  2222. pkt_err(pd, "%s already setup\n",
  2223. bdevname(pd2->bdev, b));
  2224. return -EBUSY;
  2225. }
  2226. if (pd2->pkt_dev == dev) {
  2227. pkt_err(pd, "can't chain pktcdvd devices\n");
  2228. return -EBUSY;
  2229. }
  2230. }
  2231. bdev = bdget(dev);
  2232. if (!bdev)
  2233. return -ENOMEM;
  2234. ret = blkdev_get(bdev, FMODE_READ | FMODE_NDELAY, NULL);
  2235. if (ret)
  2236. return ret;
  2237. if (!blk_queue_scsi_passthrough(bdev_get_queue(bdev))) {
  2238. blkdev_put(bdev, FMODE_READ | FMODE_NDELAY);
  2239. return -EINVAL;
  2240. }
  2241. /* This is safe, since we have a reference from open(). */
  2242. __module_get(THIS_MODULE);
  2243. pd->bdev = bdev;
  2244. set_blocksize(bdev, CD_FRAMESIZE);
  2245. pkt_init_queue(pd);
  2246. atomic_set(&pd->cdrw.pending_bios, 0);
  2247. pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->name);
  2248. if (IS_ERR(pd->cdrw.thread)) {
  2249. pkt_err(pd, "can't start kernel thread\n");
  2250. ret = -ENOMEM;
  2251. goto out_mem;
  2252. }
  2253. proc_create_single_data(pd->name, 0, pkt_proc, pkt_seq_show, pd);
  2254. pkt_dbg(1, pd, "writer mapped to %s\n", bdevname(bdev, b));
  2255. return 0;
  2256. out_mem:
  2257. blkdev_put(bdev, FMODE_READ | FMODE_NDELAY);
  2258. /* This is safe: open() is still holding a reference. */
  2259. module_put(THIS_MODULE);
  2260. return ret;
  2261. }
  2262. static int pkt_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg)
  2263. {
  2264. struct pktcdvd_device *pd = bdev->bd_disk->private_data;
  2265. int ret;
  2266. pkt_dbg(2, pd, "cmd %x, dev %d:%d\n",
  2267. cmd, MAJOR(bdev->bd_dev), MINOR(bdev->bd_dev));
  2268. mutex_lock(&pktcdvd_mutex);
  2269. switch (cmd) {
  2270. case CDROMEJECT:
  2271. /*
  2272. * The door gets locked when the device is opened, so we
  2273. * have to unlock it or else the eject command fails.
  2274. */
  2275. if (pd->refcnt == 1)
  2276. pkt_lock_door(pd, 0);
  2277. /* fallthru */
  2278. /*
  2279. * forward selected CDROM ioctls to CD-ROM, for UDF
  2280. */
  2281. case CDROMMULTISESSION:
  2282. case CDROMREADTOCENTRY:
  2283. case CDROM_LAST_WRITTEN:
  2284. case CDROM_SEND_PACKET:
  2285. case SCSI_IOCTL_SEND_COMMAND:
  2286. ret = __blkdev_driver_ioctl(pd->bdev, mode, cmd, arg);
  2287. break;
  2288. default:
  2289. pkt_dbg(2, pd, "Unknown ioctl (%x)\n", cmd);
  2290. ret = -ENOTTY;
  2291. }
  2292. mutex_unlock(&pktcdvd_mutex);
  2293. return ret;
  2294. }
  2295. static unsigned int pkt_check_events(struct gendisk *disk,
  2296. unsigned int clearing)
  2297. {
  2298. struct pktcdvd_device *pd = disk->private_data;
  2299. struct gendisk *attached_disk;
  2300. if (!pd)
  2301. return 0;
  2302. if (!pd->bdev)
  2303. return 0;
  2304. attached_disk = pd->bdev->bd_disk;
  2305. if (!attached_disk || !attached_disk->fops->check_events)
  2306. return 0;
  2307. return attached_disk->fops->check_events(attached_disk, clearing);
  2308. }
  2309. static const struct block_device_operations pktcdvd_ops = {
  2310. .owner = THIS_MODULE,
  2311. .open = pkt_open,
  2312. .release = pkt_close,
  2313. .ioctl = pkt_ioctl,
  2314. .check_events = pkt_check_events,
  2315. };
  2316. static char *pktcdvd_devnode(struct gendisk *gd, umode_t *mode)
  2317. {
  2318. return kasprintf(GFP_KERNEL, "pktcdvd/%s", gd->disk_name);
  2319. }
  2320. /*
  2321. * Set up mapping from pktcdvd device to CD-ROM device.
  2322. */
  2323. static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev)
  2324. {
  2325. int idx;
  2326. int ret = -ENOMEM;
  2327. struct pktcdvd_device *pd;
  2328. struct gendisk *disk;
  2329. mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
  2330. for (idx = 0; idx < MAX_WRITERS; idx++)
  2331. if (!pkt_devs[idx])
  2332. break;
  2333. if (idx == MAX_WRITERS) {
  2334. pr_err("max %d writers supported\n", MAX_WRITERS);
  2335. ret = -EBUSY;
  2336. goto out_mutex;
  2337. }
  2338. pd = kzalloc(sizeof(struct pktcdvd_device), GFP_KERNEL);
  2339. if (!pd)
  2340. goto out_mutex;
  2341. ret = mempool_init_kmalloc_pool(&pd->rb_pool, PKT_RB_POOL_SIZE,
  2342. sizeof(struct pkt_rb_node));
  2343. if (ret)
  2344. goto out_mem;
  2345. INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
  2346. INIT_LIST_HEAD(&pd->cdrw.pkt_active_list);
  2347. spin_lock_init(&pd->cdrw.active_list_lock);
  2348. spin_lock_init(&pd->lock);
  2349. spin_lock_init(&pd->iosched.lock);
  2350. bio_list_init(&pd->iosched.read_queue);
  2351. bio_list_init(&pd->iosched.write_queue);
  2352. sprintf(pd->name, DRIVER_NAME"%d", idx);
  2353. init_waitqueue_head(&pd->wqueue);
  2354. pd->bio_queue = RB_ROOT;
  2355. pd->write_congestion_on = write_congestion_on;
  2356. pd->write_congestion_off = write_congestion_off;
  2357. ret = -ENOMEM;
  2358. disk = alloc_disk(1);
  2359. if (!disk)
  2360. goto out_mem;
  2361. pd->disk = disk;
  2362. disk->major = pktdev_major;
  2363. disk->first_minor = idx;
  2364. disk->fops = &pktcdvd_ops;
  2365. disk->flags = GENHD_FL_REMOVABLE;
  2366. strcpy(disk->disk_name, pd->name);
  2367. disk->devnode = pktcdvd_devnode;
  2368. disk->private_data = pd;
  2369. disk->queue = blk_alloc_queue(GFP_KERNEL);
  2370. if (!disk->queue)
  2371. goto out_mem2;
  2372. pd->pkt_dev = MKDEV(pktdev_major, idx);
  2373. ret = pkt_new_dev(pd, dev);
  2374. if (ret)
  2375. goto out_mem2;
  2376. /* inherit events of the host device */
  2377. disk->events = pd->bdev->bd_disk->events;
  2378. disk->async_events = pd->bdev->bd_disk->async_events;
  2379. add_disk(disk);
  2380. pkt_sysfs_dev_new(pd);
  2381. pkt_debugfs_dev_new(pd);
  2382. pkt_devs[idx] = pd;
  2383. if (pkt_dev)
  2384. *pkt_dev = pd->pkt_dev;
  2385. mutex_unlock(&ctl_mutex);
  2386. return 0;
  2387. out_mem2:
  2388. put_disk(disk);
  2389. out_mem:
  2390. mempool_exit(&pd->rb_pool);
  2391. kfree(pd);
  2392. out_mutex:
  2393. mutex_unlock(&ctl_mutex);
  2394. pr_err("setup of pktcdvd device failed\n");
  2395. return ret;
  2396. }
  2397. /*
  2398. * Tear down mapping from pktcdvd device to CD-ROM device.
  2399. */
  2400. static int pkt_remove_dev(dev_t pkt_dev)
  2401. {
  2402. struct pktcdvd_device *pd;
  2403. int idx;
  2404. int ret = 0;
  2405. mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
  2406. for (idx = 0; idx < MAX_WRITERS; idx++) {
  2407. pd = pkt_devs[idx];
  2408. if (pd && (pd->pkt_dev == pkt_dev))
  2409. break;
  2410. }
  2411. if (idx == MAX_WRITERS) {
  2412. pr_debug("dev not setup\n");
  2413. ret = -ENXIO;
  2414. goto out;
  2415. }
  2416. if (pd->refcnt > 0) {
  2417. ret = -EBUSY;
  2418. goto out;
  2419. }
  2420. if (!IS_ERR(pd->cdrw.thread))
  2421. kthread_stop(pd->cdrw.thread);
  2422. pkt_devs[idx] = NULL;
  2423. pkt_debugfs_dev_remove(pd);
  2424. pkt_sysfs_dev_remove(pd);
  2425. blkdev_put(pd->bdev, FMODE_READ | FMODE_NDELAY);
  2426. remove_proc_entry(pd->name, pkt_proc);
  2427. pkt_dbg(1, pd, "writer unmapped\n");
  2428. del_gendisk(pd->disk);
  2429. blk_cleanup_queue(pd->disk->queue);
  2430. put_disk(pd->disk);
  2431. mempool_exit(&pd->rb_pool);
  2432. kfree(pd);
  2433. /* This is safe: open() is still holding a reference. */
  2434. module_put(THIS_MODULE);
  2435. out:
  2436. mutex_unlock(&ctl_mutex);
  2437. return ret;
  2438. }
  2439. static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd)
  2440. {
  2441. struct pktcdvd_device *pd;
  2442. mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
  2443. pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index);
  2444. if (pd) {
  2445. ctrl_cmd->dev = new_encode_dev(pd->bdev->bd_dev);
  2446. ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
  2447. } else {
  2448. ctrl_cmd->dev = 0;
  2449. ctrl_cmd->pkt_dev = 0;
  2450. }
  2451. ctrl_cmd->num_devices = MAX_WRITERS;
  2452. mutex_unlock(&ctl_mutex);
  2453. }
  2454. static long pkt_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2455. {
  2456. void __user *argp = (void __user *)arg;
  2457. struct pkt_ctrl_command ctrl_cmd;
  2458. int ret = 0;
  2459. dev_t pkt_dev = 0;
  2460. if (cmd != PACKET_CTRL_CMD)
  2461. return -ENOTTY;
  2462. if (copy_from_user(&ctrl_cmd, argp, sizeof(struct pkt_ctrl_command)))
  2463. return -EFAULT;
  2464. switch (ctrl_cmd.command) {
  2465. case PKT_CTRL_CMD_SETUP:
  2466. if (!capable(CAP_SYS_ADMIN))
  2467. return -EPERM;
  2468. ret = pkt_setup_dev(new_decode_dev(ctrl_cmd.dev), &pkt_dev);
  2469. ctrl_cmd.pkt_dev = new_encode_dev(pkt_dev);
  2470. break;
  2471. case PKT_CTRL_CMD_TEARDOWN:
  2472. if (!capable(CAP_SYS_ADMIN))
  2473. return -EPERM;
  2474. ret = pkt_remove_dev(new_decode_dev(ctrl_cmd.pkt_dev));
  2475. break;
  2476. case PKT_CTRL_CMD_STATUS:
  2477. pkt_get_status(&ctrl_cmd);
  2478. break;
  2479. default:
  2480. return -ENOTTY;
  2481. }
  2482. if (copy_to_user(argp, &ctrl_cmd, sizeof(struct pkt_ctrl_command)))
  2483. return -EFAULT;
  2484. return ret;
  2485. }
  2486. #ifdef CONFIG_COMPAT
  2487. static long pkt_ctl_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2488. {
  2489. return pkt_ctl_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  2490. }
  2491. #endif
  2492. static const struct file_operations pkt_ctl_fops = {
  2493. .open = nonseekable_open,
  2494. .unlocked_ioctl = pkt_ctl_ioctl,
  2495. #ifdef CONFIG_COMPAT
  2496. .compat_ioctl = pkt_ctl_compat_ioctl,
  2497. #endif
  2498. .owner = THIS_MODULE,
  2499. .llseek = no_llseek,
  2500. };
  2501. static struct miscdevice pkt_misc = {
  2502. .minor = MISC_DYNAMIC_MINOR,
  2503. .name = DRIVER_NAME,
  2504. .nodename = "pktcdvd/control",
  2505. .fops = &pkt_ctl_fops
  2506. };
  2507. static int __init pkt_init(void)
  2508. {
  2509. int ret;
  2510. mutex_init(&ctl_mutex);
  2511. ret = mempool_init_kmalloc_pool(&psd_pool, PSD_POOL_SIZE,
  2512. sizeof(struct packet_stacked_data));
  2513. if (ret)
  2514. return ret;
  2515. ret = bioset_init(&pkt_bio_set, BIO_POOL_SIZE, 0, 0);
  2516. if (ret) {
  2517. mempool_exit(&psd_pool);
  2518. return ret;
  2519. }
  2520. ret = register_blkdev(pktdev_major, DRIVER_NAME);
  2521. if (ret < 0) {
  2522. pr_err("unable to register block device\n");
  2523. goto out2;
  2524. }
  2525. if (!pktdev_major)
  2526. pktdev_major = ret;
  2527. ret = pkt_sysfs_init();
  2528. if (ret)
  2529. goto out;
  2530. pkt_debugfs_init();
  2531. ret = misc_register(&pkt_misc);
  2532. if (ret) {
  2533. pr_err("unable to register misc device\n");
  2534. goto out_misc;
  2535. }
  2536. pkt_proc = proc_mkdir("driver/"DRIVER_NAME, NULL);
  2537. return 0;
  2538. out_misc:
  2539. pkt_debugfs_cleanup();
  2540. pkt_sysfs_cleanup();
  2541. out:
  2542. unregister_blkdev(pktdev_major, DRIVER_NAME);
  2543. out2:
  2544. mempool_exit(&psd_pool);
  2545. bioset_exit(&pkt_bio_set);
  2546. return ret;
  2547. }
  2548. static void __exit pkt_exit(void)
  2549. {
  2550. remove_proc_entry("driver/"DRIVER_NAME, NULL);
  2551. misc_deregister(&pkt_misc);
  2552. pkt_debugfs_cleanup();
  2553. pkt_sysfs_cleanup();
  2554. unregister_blkdev(pktdev_major, DRIVER_NAME);
  2555. mempool_exit(&psd_pool);
  2556. bioset_exit(&pkt_bio_set);
  2557. }
  2558. MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives");
  2559. MODULE_AUTHOR("Jens Axboe <axboe@suse.de>");
  2560. MODULE_LICENSE("GPL");
  2561. module_init(pkt_init);
  2562. module_exit(pkt_exit);