pktcdvd.c 73 KB

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