debug.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162
  1. /*
  2. * This file is part of UBIFS.
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc., 51
  17. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * Authors: Artem Bityutskiy (Битюцкий Артём)
  20. * Adrian Hunter
  21. */
  22. /*
  23. * This file implements most of the debugging stuff which is compiled in only
  24. * when it is enabled. But some debugging check functions are implemented in
  25. * corresponding subsystem, just because they are closely related and utilize
  26. * various local functions of those subsystems.
  27. */
  28. #include "ubifs.h"
  29. #include <linux/module.h>
  30. #include <linux/debugfs.h>
  31. #include <linux/math64.h>
  32. #include <linux/uaccess.h>
  33. #ifdef CONFIG_UBIFS_FS_DEBUG
  34. DEFINE_SPINLOCK(dbg_lock);
  35. static char dbg_key_buf0[128];
  36. static char dbg_key_buf1[128];
  37. static const char *get_key_fmt(int fmt)
  38. {
  39. switch (fmt) {
  40. case UBIFS_SIMPLE_KEY_FMT:
  41. return "simple";
  42. default:
  43. return "unknown/invalid format";
  44. }
  45. }
  46. static const char *get_key_hash(int hash)
  47. {
  48. switch (hash) {
  49. case UBIFS_KEY_HASH_R5:
  50. return "R5";
  51. case UBIFS_KEY_HASH_TEST:
  52. return "test";
  53. default:
  54. return "unknown/invalid name hash";
  55. }
  56. }
  57. static const char *get_key_type(int type)
  58. {
  59. switch (type) {
  60. case UBIFS_INO_KEY:
  61. return "inode";
  62. case UBIFS_DENT_KEY:
  63. return "direntry";
  64. case UBIFS_XENT_KEY:
  65. return "xentry";
  66. case UBIFS_DATA_KEY:
  67. return "data";
  68. case UBIFS_TRUN_KEY:
  69. return "truncate";
  70. default:
  71. return "unknown/invalid key";
  72. }
  73. }
  74. static const char *get_dent_type(int type)
  75. {
  76. switch (type) {
  77. case UBIFS_ITYPE_REG:
  78. return "file";
  79. case UBIFS_ITYPE_DIR:
  80. return "dir";
  81. case UBIFS_ITYPE_LNK:
  82. return "symlink";
  83. case UBIFS_ITYPE_BLK:
  84. return "blkdev";
  85. case UBIFS_ITYPE_CHR:
  86. return "char dev";
  87. case UBIFS_ITYPE_FIFO:
  88. return "fifo";
  89. case UBIFS_ITYPE_SOCK:
  90. return "socket";
  91. default:
  92. return "unknown/invalid type";
  93. }
  94. }
  95. static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key,
  96. char *buffer)
  97. {
  98. char *p = buffer;
  99. int type = key_type(c, key);
  100. if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
  101. switch (type) {
  102. case UBIFS_INO_KEY:
  103. sprintf(p, "(%lu, %s)", (unsigned long)key_inum(c, key),
  104. get_key_type(type));
  105. break;
  106. case UBIFS_DENT_KEY:
  107. case UBIFS_XENT_KEY:
  108. sprintf(p, "(%lu, %s, %#08x)",
  109. (unsigned long)key_inum(c, key),
  110. get_key_type(type), key_hash(c, key));
  111. break;
  112. case UBIFS_DATA_KEY:
  113. sprintf(p, "(%lu, %s, %u)",
  114. (unsigned long)key_inum(c, key),
  115. get_key_type(type), key_block(c, key));
  116. break;
  117. case UBIFS_TRUN_KEY:
  118. sprintf(p, "(%lu, %s)",
  119. (unsigned long)key_inum(c, key),
  120. get_key_type(type));
  121. break;
  122. default:
  123. sprintf(p, "(bad key type: %#08x, %#08x)",
  124. key->u32[0], key->u32[1]);
  125. }
  126. } else
  127. sprintf(p, "bad key format %d", c->key_fmt);
  128. }
  129. const char *dbg_key_str0(const struct ubifs_info *c, const union ubifs_key *key)
  130. {
  131. /* dbg_lock must be held */
  132. sprintf_key(c, key, dbg_key_buf0);
  133. return dbg_key_buf0;
  134. }
  135. const char *dbg_key_str1(const struct ubifs_info *c, const union ubifs_key *key)
  136. {
  137. /* dbg_lock must be held */
  138. sprintf_key(c, key, dbg_key_buf1);
  139. return dbg_key_buf1;
  140. }
  141. const char *dbg_ntype(int type)
  142. {
  143. switch (type) {
  144. case UBIFS_PAD_NODE:
  145. return "padding node";
  146. case UBIFS_SB_NODE:
  147. return "superblock node";
  148. case UBIFS_MST_NODE:
  149. return "master node";
  150. case UBIFS_REF_NODE:
  151. return "reference node";
  152. case UBIFS_INO_NODE:
  153. return "inode node";
  154. case UBIFS_DENT_NODE:
  155. return "direntry node";
  156. case UBIFS_XENT_NODE:
  157. return "xentry node";
  158. case UBIFS_DATA_NODE:
  159. return "data node";
  160. case UBIFS_TRUN_NODE:
  161. return "truncate node";
  162. case UBIFS_IDX_NODE:
  163. return "indexing node";
  164. case UBIFS_CS_NODE:
  165. return "commit start node";
  166. case UBIFS_ORPH_NODE:
  167. return "orphan node";
  168. default:
  169. return "unknown node";
  170. }
  171. }
  172. static const char *dbg_gtype(int type)
  173. {
  174. switch (type) {
  175. case UBIFS_NO_NODE_GROUP:
  176. return "no node group";
  177. case UBIFS_IN_NODE_GROUP:
  178. return "in node group";
  179. case UBIFS_LAST_OF_NODE_GROUP:
  180. return "last of node group";
  181. default:
  182. return "unknown";
  183. }
  184. }
  185. const char *dbg_cstate(int cmt_state)
  186. {
  187. switch (cmt_state) {
  188. case COMMIT_RESTING:
  189. return "commit resting";
  190. case COMMIT_BACKGROUND:
  191. return "background commit requested";
  192. case COMMIT_REQUIRED:
  193. return "commit required";
  194. case COMMIT_RUNNING_BACKGROUND:
  195. return "BACKGROUND commit running";
  196. case COMMIT_RUNNING_REQUIRED:
  197. return "commit running and required";
  198. case COMMIT_BROKEN:
  199. return "broken commit";
  200. default:
  201. return "unknown commit state";
  202. }
  203. }
  204. const char *dbg_jhead(int jhead)
  205. {
  206. switch (jhead) {
  207. case GCHD:
  208. return "0 (GC)";
  209. case BASEHD:
  210. return "1 (base)";
  211. case DATAHD:
  212. return "2 (data)";
  213. default:
  214. return "unknown journal head";
  215. }
  216. }
  217. static void dump_ch(const struct ubifs_ch *ch)
  218. {
  219. printk(KERN_DEBUG "\tmagic %#x\n", le32_to_cpu(ch->magic));
  220. printk(KERN_DEBUG "\tcrc %#x\n", le32_to_cpu(ch->crc));
  221. printk(KERN_DEBUG "\tnode_type %d (%s)\n", ch->node_type,
  222. dbg_ntype(ch->node_type));
  223. printk(KERN_DEBUG "\tgroup_type %d (%s)\n", ch->group_type,
  224. dbg_gtype(ch->group_type));
  225. printk(KERN_DEBUG "\tsqnum %llu\n",
  226. (unsigned long long)le64_to_cpu(ch->sqnum));
  227. printk(KERN_DEBUG "\tlen %u\n", le32_to_cpu(ch->len));
  228. }
  229. void dbg_dump_inode(struct ubifs_info *c, const struct inode *inode)
  230. {
  231. const struct ubifs_inode *ui = ubifs_inode(inode);
  232. struct qstr nm = { .name = NULL };
  233. union ubifs_key key;
  234. struct ubifs_dent_node *dent, *pdent = NULL;
  235. int count = 2;
  236. printk(KERN_DEBUG "Dump in-memory inode:");
  237. printk(KERN_DEBUG "\tinode %lu\n", inode->i_ino);
  238. printk(KERN_DEBUG "\tsize %llu\n",
  239. (unsigned long long)i_size_read(inode));
  240. printk(KERN_DEBUG "\tnlink %u\n", inode->i_nlink);
  241. printk(KERN_DEBUG "\tuid %u\n", (unsigned int)inode->i_uid);
  242. printk(KERN_DEBUG "\tgid %u\n", (unsigned int)inode->i_gid);
  243. printk(KERN_DEBUG "\tatime %u.%u\n",
  244. (unsigned int)inode->i_atime.tv_sec,
  245. (unsigned int)inode->i_atime.tv_nsec);
  246. printk(KERN_DEBUG "\tmtime %u.%u\n",
  247. (unsigned int)inode->i_mtime.tv_sec,
  248. (unsigned int)inode->i_mtime.tv_nsec);
  249. printk(KERN_DEBUG "\tctime %u.%u\n",
  250. (unsigned int)inode->i_ctime.tv_sec,
  251. (unsigned int)inode->i_ctime.tv_nsec);
  252. printk(KERN_DEBUG "\tcreat_sqnum %llu\n", ui->creat_sqnum);
  253. printk(KERN_DEBUG "\txattr_size %u\n", ui->xattr_size);
  254. printk(KERN_DEBUG "\txattr_cnt %u\n", ui->xattr_cnt);
  255. printk(KERN_DEBUG "\txattr_names %u\n", ui->xattr_names);
  256. printk(KERN_DEBUG "\tdirty %u\n", ui->dirty);
  257. printk(KERN_DEBUG "\txattr %u\n", ui->xattr);
  258. printk(KERN_DEBUG "\tbulk_read %u\n", ui->xattr);
  259. printk(KERN_DEBUG "\tsynced_i_size %llu\n",
  260. (unsigned long long)ui->synced_i_size);
  261. printk(KERN_DEBUG "\tui_size %llu\n",
  262. (unsigned long long)ui->ui_size);
  263. printk(KERN_DEBUG "\tflags %d\n", ui->flags);
  264. printk(KERN_DEBUG "\tcompr_type %d\n", ui->compr_type);
  265. printk(KERN_DEBUG "\tlast_page_read %lu\n", ui->last_page_read);
  266. printk(KERN_DEBUG "\tread_in_a_row %lu\n", ui->read_in_a_row);
  267. printk(KERN_DEBUG "\tdata_len %d\n", ui->data_len);
  268. if (!S_ISDIR(inode->i_mode))
  269. return;
  270. printk(KERN_DEBUG "List of directory entries:\n");
  271. ubifs_assert(!mutex_is_locked(&c->tnc_mutex));
  272. lowest_dent_key(c, &key, inode->i_ino);
  273. while (1) {
  274. dent = ubifs_tnc_next_ent(c, &key, &nm);
  275. if (IS_ERR(dent)) {
  276. if (PTR_ERR(dent) != -ENOENT)
  277. printk(KERN_DEBUG "error %ld\n", PTR_ERR(dent));
  278. break;
  279. }
  280. printk(KERN_DEBUG "\t%d: %s (%s)\n",
  281. count++, dent->name, get_dent_type(dent->type));
  282. nm.name = dent->name;
  283. nm.len = le16_to_cpu(dent->nlen);
  284. kfree(pdent);
  285. pdent = dent;
  286. key_read(c, &dent->key, &key);
  287. }
  288. kfree(pdent);
  289. }
  290. void dbg_dump_node(const struct ubifs_info *c, const void *node)
  291. {
  292. int i, n;
  293. union ubifs_key key;
  294. const struct ubifs_ch *ch = node;
  295. if (dbg_is_tst_rcvry(c))
  296. return;
  297. /* If the magic is incorrect, just hexdump the first bytes */
  298. if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
  299. printk(KERN_DEBUG "Not a node, first %zu bytes:", UBIFS_CH_SZ);
  300. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
  301. (void *)node, UBIFS_CH_SZ, 1);
  302. return;
  303. }
  304. spin_lock(&dbg_lock);
  305. dump_ch(node);
  306. switch (ch->node_type) {
  307. case UBIFS_PAD_NODE:
  308. {
  309. const struct ubifs_pad_node *pad = node;
  310. printk(KERN_DEBUG "\tpad_len %u\n",
  311. le32_to_cpu(pad->pad_len));
  312. break;
  313. }
  314. case UBIFS_SB_NODE:
  315. {
  316. const struct ubifs_sb_node *sup = node;
  317. unsigned int sup_flags = le32_to_cpu(sup->flags);
  318. printk(KERN_DEBUG "\tkey_hash %d (%s)\n",
  319. (int)sup->key_hash, get_key_hash(sup->key_hash));
  320. printk(KERN_DEBUG "\tkey_fmt %d (%s)\n",
  321. (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
  322. printk(KERN_DEBUG "\tflags %#x\n", sup_flags);
  323. printk(KERN_DEBUG "\t big_lpt %u\n",
  324. !!(sup_flags & UBIFS_FLG_BIGLPT));
  325. printk(KERN_DEBUG "\t space_fixup %u\n",
  326. !!(sup_flags & UBIFS_FLG_SPACE_FIXUP));
  327. printk(KERN_DEBUG "\tmin_io_size %u\n",
  328. le32_to_cpu(sup->min_io_size));
  329. printk(KERN_DEBUG "\tleb_size %u\n",
  330. le32_to_cpu(sup->leb_size));
  331. printk(KERN_DEBUG "\tleb_cnt %u\n",
  332. le32_to_cpu(sup->leb_cnt));
  333. printk(KERN_DEBUG "\tmax_leb_cnt %u\n",
  334. le32_to_cpu(sup->max_leb_cnt));
  335. printk(KERN_DEBUG "\tmax_bud_bytes %llu\n",
  336. (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
  337. printk(KERN_DEBUG "\tlog_lebs %u\n",
  338. le32_to_cpu(sup->log_lebs));
  339. printk(KERN_DEBUG "\tlpt_lebs %u\n",
  340. le32_to_cpu(sup->lpt_lebs));
  341. printk(KERN_DEBUG "\torph_lebs %u\n",
  342. le32_to_cpu(sup->orph_lebs));
  343. printk(KERN_DEBUG "\tjhead_cnt %u\n",
  344. le32_to_cpu(sup->jhead_cnt));
  345. printk(KERN_DEBUG "\tfanout %u\n",
  346. le32_to_cpu(sup->fanout));
  347. printk(KERN_DEBUG "\tlsave_cnt %u\n",
  348. le32_to_cpu(sup->lsave_cnt));
  349. printk(KERN_DEBUG "\tdefault_compr %u\n",
  350. (int)le16_to_cpu(sup->default_compr));
  351. printk(KERN_DEBUG "\trp_size %llu\n",
  352. (unsigned long long)le64_to_cpu(sup->rp_size));
  353. printk(KERN_DEBUG "\trp_uid %u\n",
  354. le32_to_cpu(sup->rp_uid));
  355. printk(KERN_DEBUG "\trp_gid %u\n",
  356. le32_to_cpu(sup->rp_gid));
  357. printk(KERN_DEBUG "\tfmt_version %u\n",
  358. le32_to_cpu(sup->fmt_version));
  359. printk(KERN_DEBUG "\ttime_gran %u\n",
  360. le32_to_cpu(sup->time_gran));
  361. printk(KERN_DEBUG "\tUUID %pUB\n",
  362. sup->uuid);
  363. break;
  364. }
  365. case UBIFS_MST_NODE:
  366. {
  367. const struct ubifs_mst_node *mst = node;
  368. printk(KERN_DEBUG "\thighest_inum %llu\n",
  369. (unsigned long long)le64_to_cpu(mst->highest_inum));
  370. printk(KERN_DEBUG "\tcommit number %llu\n",
  371. (unsigned long long)le64_to_cpu(mst->cmt_no));
  372. printk(KERN_DEBUG "\tflags %#x\n",
  373. le32_to_cpu(mst->flags));
  374. printk(KERN_DEBUG "\tlog_lnum %u\n",
  375. le32_to_cpu(mst->log_lnum));
  376. printk(KERN_DEBUG "\troot_lnum %u\n",
  377. le32_to_cpu(mst->root_lnum));
  378. printk(KERN_DEBUG "\troot_offs %u\n",
  379. le32_to_cpu(mst->root_offs));
  380. printk(KERN_DEBUG "\troot_len %u\n",
  381. le32_to_cpu(mst->root_len));
  382. printk(KERN_DEBUG "\tgc_lnum %u\n",
  383. le32_to_cpu(mst->gc_lnum));
  384. printk(KERN_DEBUG "\tihead_lnum %u\n",
  385. le32_to_cpu(mst->ihead_lnum));
  386. printk(KERN_DEBUG "\tihead_offs %u\n",
  387. le32_to_cpu(mst->ihead_offs));
  388. printk(KERN_DEBUG "\tindex_size %llu\n",
  389. (unsigned long long)le64_to_cpu(mst->index_size));
  390. printk(KERN_DEBUG "\tlpt_lnum %u\n",
  391. le32_to_cpu(mst->lpt_lnum));
  392. printk(KERN_DEBUG "\tlpt_offs %u\n",
  393. le32_to_cpu(mst->lpt_offs));
  394. printk(KERN_DEBUG "\tnhead_lnum %u\n",
  395. le32_to_cpu(mst->nhead_lnum));
  396. printk(KERN_DEBUG "\tnhead_offs %u\n",
  397. le32_to_cpu(mst->nhead_offs));
  398. printk(KERN_DEBUG "\tltab_lnum %u\n",
  399. le32_to_cpu(mst->ltab_lnum));
  400. printk(KERN_DEBUG "\tltab_offs %u\n",
  401. le32_to_cpu(mst->ltab_offs));
  402. printk(KERN_DEBUG "\tlsave_lnum %u\n",
  403. le32_to_cpu(mst->lsave_lnum));
  404. printk(KERN_DEBUG "\tlsave_offs %u\n",
  405. le32_to_cpu(mst->lsave_offs));
  406. printk(KERN_DEBUG "\tlscan_lnum %u\n",
  407. le32_to_cpu(mst->lscan_lnum));
  408. printk(KERN_DEBUG "\tleb_cnt %u\n",
  409. le32_to_cpu(mst->leb_cnt));
  410. printk(KERN_DEBUG "\tempty_lebs %u\n",
  411. le32_to_cpu(mst->empty_lebs));
  412. printk(KERN_DEBUG "\tidx_lebs %u\n",
  413. le32_to_cpu(mst->idx_lebs));
  414. printk(KERN_DEBUG "\ttotal_free %llu\n",
  415. (unsigned long long)le64_to_cpu(mst->total_free));
  416. printk(KERN_DEBUG "\ttotal_dirty %llu\n",
  417. (unsigned long long)le64_to_cpu(mst->total_dirty));
  418. printk(KERN_DEBUG "\ttotal_used %llu\n",
  419. (unsigned long long)le64_to_cpu(mst->total_used));
  420. printk(KERN_DEBUG "\ttotal_dead %llu\n",
  421. (unsigned long long)le64_to_cpu(mst->total_dead));
  422. printk(KERN_DEBUG "\ttotal_dark %llu\n",
  423. (unsigned long long)le64_to_cpu(mst->total_dark));
  424. break;
  425. }
  426. case UBIFS_REF_NODE:
  427. {
  428. const struct ubifs_ref_node *ref = node;
  429. printk(KERN_DEBUG "\tlnum %u\n",
  430. le32_to_cpu(ref->lnum));
  431. printk(KERN_DEBUG "\toffs %u\n",
  432. le32_to_cpu(ref->offs));
  433. printk(KERN_DEBUG "\tjhead %u\n",
  434. le32_to_cpu(ref->jhead));
  435. break;
  436. }
  437. case UBIFS_INO_NODE:
  438. {
  439. const struct ubifs_ino_node *ino = node;
  440. key_read(c, &ino->key, &key);
  441. printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
  442. printk(KERN_DEBUG "\tcreat_sqnum %llu\n",
  443. (unsigned long long)le64_to_cpu(ino->creat_sqnum));
  444. printk(KERN_DEBUG "\tsize %llu\n",
  445. (unsigned long long)le64_to_cpu(ino->size));
  446. printk(KERN_DEBUG "\tnlink %u\n",
  447. le32_to_cpu(ino->nlink));
  448. printk(KERN_DEBUG "\tatime %lld.%u\n",
  449. (long long)le64_to_cpu(ino->atime_sec),
  450. le32_to_cpu(ino->atime_nsec));
  451. printk(KERN_DEBUG "\tmtime %lld.%u\n",
  452. (long long)le64_to_cpu(ino->mtime_sec),
  453. le32_to_cpu(ino->mtime_nsec));
  454. printk(KERN_DEBUG "\tctime %lld.%u\n",
  455. (long long)le64_to_cpu(ino->ctime_sec),
  456. le32_to_cpu(ino->ctime_nsec));
  457. printk(KERN_DEBUG "\tuid %u\n",
  458. le32_to_cpu(ino->uid));
  459. printk(KERN_DEBUG "\tgid %u\n",
  460. le32_to_cpu(ino->gid));
  461. printk(KERN_DEBUG "\tmode %u\n",
  462. le32_to_cpu(ino->mode));
  463. printk(KERN_DEBUG "\tflags %#x\n",
  464. le32_to_cpu(ino->flags));
  465. printk(KERN_DEBUG "\txattr_cnt %u\n",
  466. le32_to_cpu(ino->xattr_cnt));
  467. printk(KERN_DEBUG "\txattr_size %u\n",
  468. le32_to_cpu(ino->xattr_size));
  469. printk(KERN_DEBUG "\txattr_names %u\n",
  470. le32_to_cpu(ino->xattr_names));
  471. printk(KERN_DEBUG "\tcompr_type %#x\n",
  472. (int)le16_to_cpu(ino->compr_type));
  473. printk(KERN_DEBUG "\tdata len %u\n",
  474. le32_to_cpu(ino->data_len));
  475. break;
  476. }
  477. case UBIFS_DENT_NODE:
  478. case UBIFS_XENT_NODE:
  479. {
  480. const struct ubifs_dent_node *dent = node;
  481. int nlen = le16_to_cpu(dent->nlen);
  482. key_read(c, &dent->key, &key);
  483. printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
  484. printk(KERN_DEBUG "\tinum %llu\n",
  485. (unsigned long long)le64_to_cpu(dent->inum));
  486. printk(KERN_DEBUG "\ttype %d\n", (int)dent->type);
  487. printk(KERN_DEBUG "\tnlen %d\n", nlen);
  488. printk(KERN_DEBUG "\tname ");
  489. if (nlen > UBIFS_MAX_NLEN)
  490. printk(KERN_DEBUG "(bad name length, not printing, "
  491. "bad or corrupted node)");
  492. else {
  493. for (i = 0; i < nlen && dent->name[i]; i++)
  494. printk(KERN_CONT "%c", dent->name[i]);
  495. }
  496. printk(KERN_CONT "\n");
  497. break;
  498. }
  499. case UBIFS_DATA_NODE:
  500. {
  501. const struct ubifs_data_node *dn = node;
  502. int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
  503. key_read(c, &dn->key, &key);
  504. printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
  505. printk(KERN_DEBUG "\tsize %u\n",
  506. le32_to_cpu(dn->size));
  507. printk(KERN_DEBUG "\tcompr_typ %d\n",
  508. (int)le16_to_cpu(dn->compr_type));
  509. printk(KERN_DEBUG "\tdata size %d\n",
  510. dlen);
  511. printk(KERN_DEBUG "\tdata:\n");
  512. print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET, 32, 1,
  513. (void *)&dn->data, dlen, 0);
  514. break;
  515. }
  516. case UBIFS_TRUN_NODE:
  517. {
  518. const struct ubifs_trun_node *trun = node;
  519. printk(KERN_DEBUG "\tinum %u\n",
  520. le32_to_cpu(trun->inum));
  521. printk(KERN_DEBUG "\told_size %llu\n",
  522. (unsigned long long)le64_to_cpu(trun->old_size));
  523. printk(KERN_DEBUG "\tnew_size %llu\n",
  524. (unsigned long long)le64_to_cpu(trun->new_size));
  525. break;
  526. }
  527. case UBIFS_IDX_NODE:
  528. {
  529. const struct ubifs_idx_node *idx = node;
  530. n = le16_to_cpu(idx->child_cnt);
  531. printk(KERN_DEBUG "\tchild_cnt %d\n", n);
  532. printk(KERN_DEBUG "\tlevel %d\n",
  533. (int)le16_to_cpu(idx->level));
  534. printk(KERN_DEBUG "\tBranches:\n");
  535. for (i = 0; i < n && i < c->fanout - 1; i++) {
  536. const struct ubifs_branch *br;
  537. br = ubifs_idx_branch(c, idx, i);
  538. key_read(c, &br->key, &key);
  539. printk(KERN_DEBUG "\t%d: LEB %d:%d len %d key %s\n",
  540. i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
  541. le32_to_cpu(br->len), DBGKEY(&key));
  542. }
  543. break;
  544. }
  545. case UBIFS_CS_NODE:
  546. break;
  547. case UBIFS_ORPH_NODE:
  548. {
  549. const struct ubifs_orph_node *orph = node;
  550. printk(KERN_DEBUG "\tcommit number %llu\n",
  551. (unsigned long long)
  552. le64_to_cpu(orph->cmt_no) & LLONG_MAX);
  553. printk(KERN_DEBUG "\tlast node flag %llu\n",
  554. (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
  555. n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
  556. printk(KERN_DEBUG "\t%d orphan inode numbers:\n", n);
  557. for (i = 0; i < n; i++)
  558. printk(KERN_DEBUG "\t ino %llu\n",
  559. (unsigned long long)le64_to_cpu(orph->inos[i]));
  560. break;
  561. }
  562. default:
  563. printk(KERN_DEBUG "node type %d was not recognized\n",
  564. (int)ch->node_type);
  565. }
  566. spin_unlock(&dbg_lock);
  567. }
  568. void dbg_dump_budget_req(const struct ubifs_budget_req *req)
  569. {
  570. spin_lock(&dbg_lock);
  571. printk(KERN_DEBUG "Budgeting request: new_ino %d, dirtied_ino %d\n",
  572. req->new_ino, req->dirtied_ino);
  573. printk(KERN_DEBUG "\tnew_ino_d %d, dirtied_ino_d %d\n",
  574. req->new_ino_d, req->dirtied_ino_d);
  575. printk(KERN_DEBUG "\tnew_page %d, dirtied_page %d\n",
  576. req->new_page, req->dirtied_page);
  577. printk(KERN_DEBUG "\tnew_dent %d, mod_dent %d\n",
  578. req->new_dent, req->mod_dent);
  579. printk(KERN_DEBUG "\tidx_growth %d\n", req->idx_growth);
  580. printk(KERN_DEBUG "\tdata_growth %d dd_growth %d\n",
  581. req->data_growth, req->dd_growth);
  582. spin_unlock(&dbg_lock);
  583. }
  584. void dbg_dump_lstats(const struct ubifs_lp_stats *lst)
  585. {
  586. spin_lock(&dbg_lock);
  587. printk(KERN_DEBUG "(pid %d) Lprops statistics: empty_lebs %d, "
  588. "idx_lebs %d\n", current->pid, lst->empty_lebs, lst->idx_lebs);
  589. printk(KERN_DEBUG "\ttaken_empty_lebs %d, total_free %lld, "
  590. "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free,
  591. lst->total_dirty);
  592. printk(KERN_DEBUG "\ttotal_used %lld, total_dark %lld, "
  593. "total_dead %lld\n", lst->total_used, lst->total_dark,
  594. lst->total_dead);
  595. spin_unlock(&dbg_lock);
  596. }
  597. void dbg_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
  598. {
  599. int i;
  600. struct rb_node *rb;
  601. struct ubifs_bud *bud;
  602. struct ubifs_gced_idx_leb *idx_gc;
  603. long long available, outstanding, free;
  604. spin_lock(&c->space_lock);
  605. spin_lock(&dbg_lock);
  606. printk(KERN_DEBUG "(pid %d) Budgeting info: data budget sum %lld, "
  607. "total budget sum %lld\n", current->pid,
  608. bi->data_growth + bi->dd_growth,
  609. bi->data_growth + bi->dd_growth + bi->idx_growth);
  610. printk(KERN_DEBUG "\tbudg_data_growth %lld, budg_dd_growth %lld, "
  611. "budg_idx_growth %lld\n", bi->data_growth, bi->dd_growth,
  612. bi->idx_growth);
  613. printk(KERN_DEBUG "\tmin_idx_lebs %d, old_idx_sz %llu, "
  614. "uncommitted_idx %lld\n", bi->min_idx_lebs, bi->old_idx_sz,
  615. bi->uncommitted_idx);
  616. printk(KERN_DEBUG "\tpage_budget %d, inode_budget %d, dent_budget %d\n",
  617. bi->page_budget, bi->inode_budget, bi->dent_budget);
  618. printk(KERN_DEBUG "\tnospace %u, nospace_rp %u\n",
  619. bi->nospace, bi->nospace_rp);
  620. printk(KERN_DEBUG "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
  621. c->dark_wm, c->dead_wm, c->max_idx_node_sz);
  622. if (bi != &c->bi)
  623. /*
  624. * If we are dumping saved budgeting data, do not print
  625. * additional information which is about the current state, not
  626. * the old one which corresponded to the saved budgeting data.
  627. */
  628. goto out_unlock;
  629. printk(KERN_DEBUG "\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n",
  630. c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt);
  631. printk(KERN_DEBUG "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, "
  632. "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt),
  633. atomic_long_read(&c->dirty_zn_cnt),
  634. atomic_long_read(&c->clean_zn_cnt));
  635. printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n",
  636. c->gc_lnum, c->ihead_lnum);
  637. /* If we are in R/O mode, journal heads do not exist */
  638. if (c->jheads)
  639. for (i = 0; i < c->jhead_cnt; i++)
  640. printk(KERN_DEBUG "\tjhead %s\t LEB %d\n",
  641. dbg_jhead(c->jheads[i].wbuf.jhead),
  642. c->jheads[i].wbuf.lnum);
  643. for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
  644. bud = rb_entry(rb, struct ubifs_bud, rb);
  645. printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum);
  646. }
  647. list_for_each_entry(bud, &c->old_buds, list)
  648. printk(KERN_DEBUG "\told bud LEB %d\n", bud->lnum);
  649. list_for_each_entry(idx_gc, &c->idx_gc, list)
  650. printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n",
  651. idx_gc->lnum, idx_gc->unmap);
  652. printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state);
  653. /* Print budgeting predictions */
  654. available = ubifs_calc_available(c, c->bi.min_idx_lebs);
  655. outstanding = c->bi.data_growth + c->bi.dd_growth;
  656. free = ubifs_get_free_space_nolock(c);
  657. printk(KERN_DEBUG "Budgeting predictions:\n");
  658. printk(KERN_DEBUG "\tavailable: %lld, outstanding %lld, free %lld\n",
  659. available, outstanding, free);
  660. out_unlock:
  661. spin_unlock(&dbg_lock);
  662. spin_unlock(&c->space_lock);
  663. }
  664. void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
  665. {
  666. int i, spc, dark = 0, dead = 0;
  667. struct rb_node *rb;
  668. struct ubifs_bud *bud;
  669. spc = lp->free + lp->dirty;
  670. if (spc < c->dead_wm)
  671. dead = spc;
  672. else
  673. dark = ubifs_calc_dark(c, spc);
  674. if (lp->flags & LPROPS_INDEX)
  675. printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d "
  676. "free + dirty %-8d flags %#x (", lp->lnum, lp->free,
  677. lp->dirty, c->leb_size - spc, spc, lp->flags);
  678. else
  679. printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d "
  680. "free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d "
  681. "flags %#-4x (", lp->lnum, lp->free, lp->dirty,
  682. c->leb_size - spc, spc, dark, dead,
  683. (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);
  684. if (lp->flags & LPROPS_TAKEN) {
  685. if (lp->flags & LPROPS_INDEX)
  686. printk(KERN_CONT "index, taken");
  687. else
  688. printk(KERN_CONT "taken");
  689. } else {
  690. const char *s;
  691. if (lp->flags & LPROPS_INDEX) {
  692. switch (lp->flags & LPROPS_CAT_MASK) {
  693. case LPROPS_DIRTY_IDX:
  694. s = "dirty index";
  695. break;
  696. case LPROPS_FRDI_IDX:
  697. s = "freeable index";
  698. break;
  699. default:
  700. s = "index";
  701. }
  702. } else {
  703. switch (lp->flags & LPROPS_CAT_MASK) {
  704. case LPROPS_UNCAT:
  705. s = "not categorized";
  706. break;
  707. case LPROPS_DIRTY:
  708. s = "dirty";
  709. break;
  710. case LPROPS_FREE:
  711. s = "free";
  712. break;
  713. case LPROPS_EMPTY:
  714. s = "empty";
  715. break;
  716. case LPROPS_FREEABLE:
  717. s = "freeable";
  718. break;
  719. default:
  720. s = NULL;
  721. break;
  722. }
  723. }
  724. printk(KERN_CONT "%s", s);
  725. }
  726. for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) {
  727. bud = rb_entry(rb, struct ubifs_bud, rb);
  728. if (bud->lnum == lp->lnum) {
  729. int head = 0;
  730. for (i = 0; i < c->jhead_cnt; i++) {
  731. /*
  732. * Note, if we are in R/O mode or in the middle
  733. * of mounting/re-mounting, the write-buffers do
  734. * not exist.
  735. */
  736. if (c->jheads &&
  737. lp->lnum == c->jheads[i].wbuf.lnum) {
  738. printk(KERN_CONT ", jhead %s",
  739. dbg_jhead(i));
  740. head = 1;
  741. }
  742. }
  743. if (!head)
  744. printk(KERN_CONT ", bud of jhead %s",
  745. dbg_jhead(bud->jhead));
  746. }
  747. }
  748. if (lp->lnum == c->gc_lnum)
  749. printk(KERN_CONT ", GC LEB");
  750. printk(KERN_CONT ")\n");
  751. }
  752. void dbg_dump_lprops(struct ubifs_info *c)
  753. {
  754. int lnum, err;
  755. struct ubifs_lprops lp;
  756. struct ubifs_lp_stats lst;
  757. printk(KERN_DEBUG "(pid %d) start dumping LEB properties\n",
  758. current->pid);
  759. ubifs_get_lp_stats(c, &lst);
  760. dbg_dump_lstats(&lst);
  761. for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
  762. err = ubifs_read_one_lp(c, lnum, &lp);
  763. if (err)
  764. ubifs_err("cannot read lprops for LEB %d", lnum);
  765. dbg_dump_lprop(c, &lp);
  766. }
  767. printk(KERN_DEBUG "(pid %d) finish dumping LEB properties\n",
  768. current->pid);
  769. }
  770. void dbg_dump_lpt_info(struct ubifs_info *c)
  771. {
  772. int i;
  773. spin_lock(&dbg_lock);
  774. printk(KERN_DEBUG "(pid %d) dumping LPT information\n", current->pid);
  775. printk(KERN_DEBUG "\tlpt_sz: %lld\n", c->lpt_sz);
  776. printk(KERN_DEBUG "\tpnode_sz: %d\n", c->pnode_sz);
  777. printk(KERN_DEBUG "\tnnode_sz: %d\n", c->nnode_sz);
  778. printk(KERN_DEBUG "\tltab_sz: %d\n", c->ltab_sz);
  779. printk(KERN_DEBUG "\tlsave_sz: %d\n", c->lsave_sz);
  780. printk(KERN_DEBUG "\tbig_lpt: %d\n", c->big_lpt);
  781. printk(KERN_DEBUG "\tlpt_hght: %d\n", c->lpt_hght);
  782. printk(KERN_DEBUG "\tpnode_cnt: %d\n", c->pnode_cnt);
  783. printk(KERN_DEBUG "\tnnode_cnt: %d\n", c->nnode_cnt);
  784. printk(KERN_DEBUG "\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
  785. printk(KERN_DEBUG "\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
  786. printk(KERN_DEBUG "\tlsave_cnt: %d\n", c->lsave_cnt);
  787. printk(KERN_DEBUG "\tspace_bits: %d\n", c->space_bits);
  788. printk(KERN_DEBUG "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
  789. printk(KERN_DEBUG "\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
  790. printk(KERN_DEBUG "\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
  791. printk(KERN_DEBUG "\tpcnt_bits: %d\n", c->pcnt_bits);
  792. printk(KERN_DEBUG "\tlnum_bits: %d\n", c->lnum_bits);
  793. printk(KERN_DEBUG "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
  794. printk(KERN_DEBUG "\tLPT head is at %d:%d\n",
  795. c->nhead_lnum, c->nhead_offs);
  796. printk(KERN_DEBUG "\tLPT ltab is at %d:%d\n",
  797. c->ltab_lnum, c->ltab_offs);
  798. if (c->big_lpt)
  799. printk(KERN_DEBUG "\tLPT lsave is at %d:%d\n",
  800. c->lsave_lnum, c->lsave_offs);
  801. for (i = 0; i < c->lpt_lebs; i++)
  802. printk(KERN_DEBUG "\tLPT LEB %d free %d dirty %d tgc %d "
  803. "cmt %d\n", i + c->lpt_first, c->ltab[i].free,
  804. c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt);
  805. spin_unlock(&dbg_lock);
  806. }
  807. void dbg_dump_leb(const struct ubifs_info *c, int lnum)
  808. {
  809. struct ubifs_scan_leb *sleb;
  810. struct ubifs_scan_node *snod;
  811. void *buf;
  812. if (dbg_is_tst_rcvry(c))
  813. return;
  814. printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
  815. current->pid, lnum);
  816. buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
  817. if (!buf) {
  818. ubifs_err("cannot allocate memory for dumping LEB %d", lnum);
  819. return;
  820. }
  821. sleb = ubifs_scan(c, lnum, 0, buf, 0);
  822. if (IS_ERR(sleb)) {
  823. ubifs_err("scan error %d", (int)PTR_ERR(sleb));
  824. goto out;
  825. }
  826. printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum,
  827. sleb->nodes_cnt, sleb->endpt);
  828. list_for_each_entry(snod, &sleb->nodes, list) {
  829. cond_resched();
  830. printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum,
  831. snod->offs, snod->len);
  832. dbg_dump_node(c, snod->node);
  833. }
  834. printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
  835. current->pid, lnum);
  836. ubifs_scan_destroy(sleb);
  837. out:
  838. vfree(buf);
  839. return;
  840. }
  841. void dbg_dump_znode(const struct ubifs_info *c,
  842. const struct ubifs_znode *znode)
  843. {
  844. int n;
  845. const struct ubifs_zbranch *zbr;
  846. spin_lock(&dbg_lock);
  847. if (znode->parent)
  848. zbr = &znode->parent->zbranch[znode->iip];
  849. else
  850. zbr = &c->zroot;
  851. printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d"
  852. " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs,
  853. zbr->len, znode->parent, znode->iip, znode->level,
  854. znode->child_cnt, znode->flags);
  855. if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
  856. spin_unlock(&dbg_lock);
  857. return;
  858. }
  859. printk(KERN_DEBUG "zbranches:\n");
  860. for (n = 0; n < znode->child_cnt; n++) {
  861. zbr = &znode->zbranch[n];
  862. if (znode->level > 0)
  863. printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key "
  864. "%s\n", n, zbr->znode, zbr->lnum,
  865. zbr->offs, zbr->len,
  866. DBGKEY(&zbr->key));
  867. else
  868. printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key "
  869. "%s\n", n, zbr->znode, zbr->lnum,
  870. zbr->offs, zbr->len,
  871. DBGKEY(&zbr->key));
  872. }
  873. spin_unlock(&dbg_lock);
  874. }
  875. void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
  876. {
  877. int i;
  878. printk(KERN_DEBUG "(pid %d) start dumping heap cat %d (%d elements)\n",
  879. current->pid, cat, heap->cnt);
  880. for (i = 0; i < heap->cnt; i++) {
  881. struct ubifs_lprops *lprops = heap->arr[i];
  882. printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d "
  883. "flags %d\n", i, lprops->lnum, lprops->hpos,
  884. lprops->free, lprops->dirty, lprops->flags);
  885. }
  886. printk(KERN_DEBUG "(pid %d) finish dumping heap\n", current->pid);
  887. }
  888. void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
  889. struct ubifs_nnode *parent, int iip)
  890. {
  891. int i;
  892. printk(KERN_DEBUG "(pid %d) dumping pnode:\n", current->pid);
  893. printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n",
  894. (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
  895. printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n",
  896. pnode->flags, iip, pnode->level, pnode->num);
  897. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  898. struct ubifs_lprops *lp = &pnode->lprops[i];
  899. printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n",
  900. i, lp->free, lp->dirty, lp->flags, lp->lnum);
  901. }
  902. }
  903. void dbg_dump_tnc(struct ubifs_info *c)
  904. {
  905. struct ubifs_znode *znode;
  906. int level;
  907. printk(KERN_DEBUG "\n");
  908. printk(KERN_DEBUG "(pid %d) start dumping TNC tree\n", current->pid);
  909. znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
  910. level = znode->level;
  911. printk(KERN_DEBUG "== Level %d ==\n", level);
  912. while (znode) {
  913. if (level != znode->level) {
  914. level = znode->level;
  915. printk(KERN_DEBUG "== Level %d ==\n", level);
  916. }
  917. dbg_dump_znode(c, znode);
  918. znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
  919. }
  920. printk(KERN_DEBUG "(pid %d) finish dumping TNC tree\n", current->pid);
  921. }
  922. static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
  923. void *priv)
  924. {
  925. dbg_dump_znode(c, znode);
  926. return 0;
  927. }
  928. /**
  929. * dbg_dump_index - dump the on-flash index.
  930. * @c: UBIFS file-system description object
  931. *
  932. * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()'
  933. * which dumps only in-memory znodes and does not read znodes which from flash.
  934. */
  935. void dbg_dump_index(struct ubifs_info *c)
  936. {
  937. dbg_walk_index(c, NULL, dump_znode, NULL);
  938. }
  939. /**
  940. * dbg_save_space_info - save information about flash space.
  941. * @c: UBIFS file-system description object
  942. *
  943. * This function saves information about UBIFS free space, dirty space, etc, in
  944. * order to check it later.
  945. */
  946. void dbg_save_space_info(struct ubifs_info *c)
  947. {
  948. struct ubifs_debug_info *d = c->dbg;
  949. int freeable_cnt;
  950. spin_lock(&c->space_lock);
  951. memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats));
  952. memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info));
  953. d->saved_idx_gc_cnt = c->idx_gc_cnt;
  954. /*
  955. * We use a dirty hack here and zero out @c->freeable_cnt, because it
  956. * affects the free space calculations, and UBIFS might not know about
  957. * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
  958. * only when we read their lprops, and we do this only lazily, upon the
  959. * need. So at any given point of time @c->freeable_cnt might be not
  960. * exactly accurate.
  961. *
  962. * Just one example about the issue we hit when we did not zero
  963. * @c->freeable_cnt.
  964. * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
  965. * amount of free space in @d->saved_free
  966. * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
  967. * information from flash, where we cache LEBs from various
  968. * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
  969. * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
  970. * -> 'ubifs_get_pnode()' -> 'update_cats()'
  971. * -> 'ubifs_add_to_cat()').
  972. * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
  973. * becomes %1.
  974. * 4. We calculate the amount of free space when the re-mount is
  975. * finished in 'dbg_check_space_info()' and it does not match
  976. * @d->saved_free.
  977. */
  978. freeable_cnt = c->freeable_cnt;
  979. c->freeable_cnt = 0;
  980. d->saved_free = ubifs_get_free_space_nolock(c);
  981. c->freeable_cnt = freeable_cnt;
  982. spin_unlock(&c->space_lock);
  983. }
  984. /**
  985. * dbg_check_space_info - check flash space information.
  986. * @c: UBIFS file-system description object
  987. *
  988. * This function compares current flash space information with the information
  989. * which was saved when the 'dbg_save_space_info()' function was called.
  990. * Returns zero if the information has not changed, and %-EINVAL it it has
  991. * changed.
  992. */
  993. int dbg_check_space_info(struct ubifs_info *c)
  994. {
  995. struct ubifs_debug_info *d = c->dbg;
  996. struct ubifs_lp_stats lst;
  997. long long free;
  998. int freeable_cnt;
  999. spin_lock(&c->space_lock);
  1000. freeable_cnt = c->freeable_cnt;
  1001. c->freeable_cnt = 0;
  1002. free = ubifs_get_free_space_nolock(c);
  1003. c->freeable_cnt = freeable_cnt;
  1004. spin_unlock(&c->space_lock);
  1005. if (free != d->saved_free) {
  1006. ubifs_err("free space changed from %lld to %lld",
  1007. d->saved_free, free);
  1008. goto out;
  1009. }
  1010. return 0;
  1011. out:
  1012. ubifs_msg("saved lprops statistics dump");
  1013. dbg_dump_lstats(&d->saved_lst);
  1014. ubifs_msg("saved budgeting info dump");
  1015. dbg_dump_budg(c, &d->saved_bi);
  1016. ubifs_msg("saved idx_gc_cnt %d", d->saved_idx_gc_cnt);
  1017. ubifs_msg("current lprops statistics dump");
  1018. ubifs_get_lp_stats(c, &lst);
  1019. dbg_dump_lstats(&lst);
  1020. ubifs_msg("current budgeting info dump");
  1021. dbg_dump_budg(c, &c->bi);
  1022. dump_stack();
  1023. return -EINVAL;
  1024. }
  1025. /**
  1026. * dbg_check_synced_i_size - check synchronized inode size.
  1027. * @c: UBIFS file-system description object
  1028. * @inode: inode to check
  1029. *
  1030. * If inode is clean, synchronized inode size has to be equivalent to current
  1031. * inode size. This function has to be called only for locked inodes (@i_mutex
  1032. * has to be locked). Returns %0 if synchronized inode size if correct, and
  1033. * %-EINVAL if not.
  1034. */
  1035. int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode)
  1036. {
  1037. int err = 0;
  1038. struct ubifs_inode *ui = ubifs_inode(inode);
  1039. if (!dbg_is_chk_gen(c))
  1040. return 0;
  1041. if (!S_ISREG(inode->i_mode))
  1042. return 0;
  1043. mutex_lock(&ui->ui_mutex);
  1044. spin_lock(&ui->ui_lock);
  1045. if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
  1046. ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
  1047. "is clean", ui->ui_size, ui->synced_i_size);
  1048. ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
  1049. inode->i_mode, i_size_read(inode));
  1050. dbg_dump_stack();
  1051. err = -EINVAL;
  1052. }
  1053. spin_unlock(&ui->ui_lock);
  1054. mutex_unlock(&ui->ui_mutex);
  1055. return err;
  1056. }
  1057. /*
  1058. * dbg_check_dir - check directory inode size and link count.
  1059. * @c: UBIFS file-system description object
  1060. * @dir: the directory to calculate size for
  1061. * @size: the result is returned here
  1062. *
  1063. * This function makes sure that directory size and link count are correct.
  1064. * Returns zero in case of success and a negative error code in case of
  1065. * failure.
  1066. *
  1067. * Note, it is good idea to make sure the @dir->i_mutex is locked before
  1068. * calling this function.
  1069. */
  1070. int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
  1071. {
  1072. unsigned int nlink = 2;
  1073. union ubifs_key key;
  1074. struct ubifs_dent_node *dent, *pdent = NULL;
  1075. struct qstr nm = { .name = NULL };
  1076. loff_t size = UBIFS_INO_NODE_SZ;
  1077. if (!dbg_is_chk_gen(c))
  1078. return 0;
  1079. if (!S_ISDIR(dir->i_mode))
  1080. return 0;
  1081. lowest_dent_key(c, &key, dir->i_ino);
  1082. while (1) {
  1083. int err;
  1084. dent = ubifs_tnc_next_ent(c, &key, &nm);
  1085. if (IS_ERR(dent)) {
  1086. err = PTR_ERR(dent);
  1087. if (err == -ENOENT)
  1088. break;
  1089. return err;
  1090. }
  1091. nm.name = dent->name;
  1092. nm.len = le16_to_cpu(dent->nlen);
  1093. size += CALC_DENT_SIZE(nm.len);
  1094. if (dent->type == UBIFS_ITYPE_DIR)
  1095. nlink += 1;
  1096. kfree(pdent);
  1097. pdent = dent;
  1098. key_read(c, &dent->key, &key);
  1099. }
  1100. kfree(pdent);
  1101. if (i_size_read(dir) != size) {
  1102. ubifs_err("directory inode %lu has size %llu, "
  1103. "but calculated size is %llu", dir->i_ino,
  1104. (unsigned long long)i_size_read(dir),
  1105. (unsigned long long)size);
  1106. dbg_dump_inode(c, dir);
  1107. dump_stack();
  1108. return -EINVAL;
  1109. }
  1110. if (dir->i_nlink != nlink) {
  1111. ubifs_err("directory inode %lu has nlink %u, but calculated "
  1112. "nlink is %u", dir->i_ino, dir->i_nlink, nlink);
  1113. dbg_dump_inode(c, dir);
  1114. dump_stack();
  1115. return -EINVAL;
  1116. }
  1117. return 0;
  1118. }
  1119. /**
  1120. * dbg_check_key_order - make sure that colliding keys are properly ordered.
  1121. * @c: UBIFS file-system description object
  1122. * @zbr1: first zbranch
  1123. * @zbr2: following zbranch
  1124. *
  1125. * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
  1126. * names of the direntries/xentries which are referred by the keys. This
  1127. * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
  1128. * sure the name of direntry/xentry referred by @zbr1 is less than
  1129. * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
  1130. * and a negative error code in case of failure.
  1131. */
  1132. static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
  1133. struct ubifs_zbranch *zbr2)
  1134. {
  1135. int err, nlen1, nlen2, cmp;
  1136. struct ubifs_dent_node *dent1, *dent2;
  1137. union ubifs_key key;
  1138. ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
  1139. dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
  1140. if (!dent1)
  1141. return -ENOMEM;
  1142. dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
  1143. if (!dent2) {
  1144. err = -ENOMEM;
  1145. goto out_free;
  1146. }
  1147. err = ubifs_tnc_read_node(c, zbr1, dent1);
  1148. if (err)
  1149. goto out_free;
  1150. err = ubifs_validate_entry(c, dent1);
  1151. if (err)
  1152. goto out_free;
  1153. err = ubifs_tnc_read_node(c, zbr2, dent2);
  1154. if (err)
  1155. goto out_free;
  1156. err = ubifs_validate_entry(c, dent2);
  1157. if (err)
  1158. goto out_free;
  1159. /* Make sure node keys are the same as in zbranch */
  1160. err = 1;
  1161. key_read(c, &dent1->key, &key);
  1162. if (keys_cmp(c, &zbr1->key, &key)) {
  1163. dbg_err("1st entry at %d:%d has key %s", zbr1->lnum,
  1164. zbr1->offs, DBGKEY(&key));
  1165. dbg_err("but it should have key %s according to tnc",
  1166. DBGKEY(&zbr1->key));
  1167. dbg_dump_node(c, dent1);
  1168. goto out_free;
  1169. }
  1170. key_read(c, &dent2->key, &key);
  1171. if (keys_cmp(c, &zbr2->key, &key)) {
  1172. dbg_err("2nd entry at %d:%d has key %s", zbr1->lnum,
  1173. zbr1->offs, DBGKEY(&key));
  1174. dbg_err("but it should have key %s according to tnc",
  1175. DBGKEY(&zbr2->key));
  1176. dbg_dump_node(c, dent2);
  1177. goto out_free;
  1178. }
  1179. nlen1 = le16_to_cpu(dent1->nlen);
  1180. nlen2 = le16_to_cpu(dent2->nlen);
  1181. cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
  1182. if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
  1183. err = 0;
  1184. goto out_free;
  1185. }
  1186. if (cmp == 0 && nlen1 == nlen2)
  1187. dbg_err("2 xent/dent nodes with the same name");
  1188. else
  1189. dbg_err("bad order of colliding key %s",
  1190. DBGKEY(&key));
  1191. ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
  1192. dbg_dump_node(c, dent1);
  1193. ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
  1194. dbg_dump_node(c, dent2);
  1195. out_free:
  1196. kfree(dent2);
  1197. kfree(dent1);
  1198. return err;
  1199. }
  1200. /**
  1201. * dbg_check_znode - check if znode is all right.
  1202. * @c: UBIFS file-system description object
  1203. * @zbr: zbranch which points to this znode
  1204. *
  1205. * This function makes sure that znode referred to by @zbr is all right.
  1206. * Returns zero if it is, and %-EINVAL if it is not.
  1207. */
  1208. static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
  1209. {
  1210. struct ubifs_znode *znode = zbr->znode;
  1211. struct ubifs_znode *zp = znode->parent;
  1212. int n, err, cmp;
  1213. if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
  1214. err = 1;
  1215. goto out;
  1216. }
  1217. if (znode->level < 0) {
  1218. err = 2;
  1219. goto out;
  1220. }
  1221. if (znode->iip < 0 || znode->iip >= c->fanout) {
  1222. err = 3;
  1223. goto out;
  1224. }
  1225. if (zbr->len == 0)
  1226. /* Only dirty zbranch may have no on-flash nodes */
  1227. if (!ubifs_zn_dirty(znode)) {
  1228. err = 4;
  1229. goto out;
  1230. }
  1231. if (ubifs_zn_dirty(znode)) {
  1232. /*
  1233. * If znode is dirty, its parent has to be dirty as well. The
  1234. * order of the operation is important, so we have to have
  1235. * memory barriers.
  1236. */
  1237. smp_mb();
  1238. if (zp && !ubifs_zn_dirty(zp)) {
  1239. /*
  1240. * The dirty flag is atomic and is cleared outside the
  1241. * TNC mutex, so znode's dirty flag may now have
  1242. * been cleared. The child is always cleared before the
  1243. * parent, so we just need to check again.
  1244. */
  1245. smp_mb();
  1246. if (ubifs_zn_dirty(znode)) {
  1247. err = 5;
  1248. goto out;
  1249. }
  1250. }
  1251. }
  1252. if (zp) {
  1253. const union ubifs_key *min, *max;
  1254. if (znode->level != zp->level - 1) {
  1255. err = 6;
  1256. goto out;
  1257. }
  1258. /* Make sure the 'parent' pointer in our znode is correct */
  1259. err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
  1260. if (!err) {
  1261. /* This zbranch does not exist in the parent */
  1262. err = 7;
  1263. goto out;
  1264. }
  1265. if (znode->iip >= zp->child_cnt) {
  1266. err = 8;
  1267. goto out;
  1268. }
  1269. if (znode->iip != n) {
  1270. /* This may happen only in case of collisions */
  1271. if (keys_cmp(c, &zp->zbranch[n].key,
  1272. &zp->zbranch[znode->iip].key)) {
  1273. err = 9;
  1274. goto out;
  1275. }
  1276. n = znode->iip;
  1277. }
  1278. /*
  1279. * Make sure that the first key in our znode is greater than or
  1280. * equal to the key in the pointing zbranch.
  1281. */
  1282. min = &zbr->key;
  1283. cmp = keys_cmp(c, min, &znode->zbranch[0].key);
  1284. if (cmp == 1) {
  1285. err = 10;
  1286. goto out;
  1287. }
  1288. if (n + 1 < zp->child_cnt) {
  1289. max = &zp->zbranch[n + 1].key;
  1290. /*
  1291. * Make sure the last key in our znode is less or
  1292. * equivalent than the key in the zbranch which goes
  1293. * after our pointing zbranch.
  1294. */
  1295. cmp = keys_cmp(c, max,
  1296. &znode->zbranch[znode->child_cnt - 1].key);
  1297. if (cmp == -1) {
  1298. err = 11;
  1299. goto out;
  1300. }
  1301. }
  1302. } else {
  1303. /* This may only be root znode */
  1304. if (zbr != &c->zroot) {
  1305. err = 12;
  1306. goto out;
  1307. }
  1308. }
  1309. /*
  1310. * Make sure that next key is greater or equivalent then the previous
  1311. * one.
  1312. */
  1313. for (n = 1; n < znode->child_cnt; n++) {
  1314. cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
  1315. &znode->zbranch[n].key);
  1316. if (cmp > 0) {
  1317. err = 13;
  1318. goto out;
  1319. }
  1320. if (cmp == 0) {
  1321. /* This can only be keys with colliding hash */
  1322. if (!is_hash_key(c, &znode->zbranch[n].key)) {
  1323. err = 14;
  1324. goto out;
  1325. }
  1326. if (znode->level != 0 || c->replaying)
  1327. continue;
  1328. /*
  1329. * Colliding keys should follow binary order of
  1330. * corresponding xentry/dentry names.
  1331. */
  1332. err = dbg_check_key_order(c, &znode->zbranch[n - 1],
  1333. &znode->zbranch[n]);
  1334. if (err < 0)
  1335. return err;
  1336. if (err) {
  1337. err = 15;
  1338. goto out;
  1339. }
  1340. }
  1341. }
  1342. for (n = 0; n < znode->child_cnt; n++) {
  1343. if (!znode->zbranch[n].znode &&
  1344. (znode->zbranch[n].lnum == 0 ||
  1345. znode->zbranch[n].len == 0)) {
  1346. err = 16;
  1347. goto out;
  1348. }
  1349. if (znode->zbranch[n].lnum != 0 &&
  1350. znode->zbranch[n].len == 0) {
  1351. err = 17;
  1352. goto out;
  1353. }
  1354. if (znode->zbranch[n].lnum == 0 &&
  1355. znode->zbranch[n].len != 0) {
  1356. err = 18;
  1357. goto out;
  1358. }
  1359. if (znode->zbranch[n].lnum == 0 &&
  1360. znode->zbranch[n].offs != 0) {
  1361. err = 19;
  1362. goto out;
  1363. }
  1364. if (znode->level != 0 && znode->zbranch[n].znode)
  1365. if (znode->zbranch[n].znode->parent != znode) {
  1366. err = 20;
  1367. goto out;
  1368. }
  1369. }
  1370. return 0;
  1371. out:
  1372. ubifs_err("failed, error %d", err);
  1373. ubifs_msg("dump of the znode");
  1374. dbg_dump_znode(c, znode);
  1375. if (zp) {
  1376. ubifs_msg("dump of the parent znode");
  1377. dbg_dump_znode(c, zp);
  1378. }
  1379. dump_stack();
  1380. return -EINVAL;
  1381. }
  1382. /**
  1383. * dbg_check_tnc - check TNC tree.
  1384. * @c: UBIFS file-system description object
  1385. * @extra: do extra checks that are possible at start commit
  1386. *
  1387. * This function traverses whole TNC tree and checks every znode. Returns zero
  1388. * if everything is all right and %-EINVAL if something is wrong with TNC.
  1389. */
  1390. int dbg_check_tnc(struct ubifs_info *c, int extra)
  1391. {
  1392. struct ubifs_znode *znode;
  1393. long clean_cnt = 0, dirty_cnt = 0;
  1394. int err, last;
  1395. if (!dbg_is_chk_index(c))
  1396. return 0;
  1397. ubifs_assert(mutex_is_locked(&c->tnc_mutex));
  1398. if (!c->zroot.znode)
  1399. return 0;
  1400. znode = ubifs_tnc_postorder_first(c->zroot.znode);
  1401. while (1) {
  1402. struct ubifs_znode *prev;
  1403. struct ubifs_zbranch *zbr;
  1404. if (!znode->parent)
  1405. zbr = &c->zroot;
  1406. else
  1407. zbr = &znode->parent->zbranch[znode->iip];
  1408. err = dbg_check_znode(c, zbr);
  1409. if (err)
  1410. return err;
  1411. if (extra) {
  1412. if (ubifs_zn_dirty(znode))
  1413. dirty_cnt += 1;
  1414. else
  1415. clean_cnt += 1;
  1416. }
  1417. prev = znode;
  1418. znode = ubifs_tnc_postorder_next(znode);
  1419. if (!znode)
  1420. break;
  1421. /*
  1422. * If the last key of this znode is equivalent to the first key
  1423. * of the next znode (collision), then check order of the keys.
  1424. */
  1425. last = prev->child_cnt - 1;
  1426. if (prev->level == 0 && znode->level == 0 && !c->replaying &&
  1427. !keys_cmp(c, &prev->zbranch[last].key,
  1428. &znode->zbranch[0].key)) {
  1429. err = dbg_check_key_order(c, &prev->zbranch[last],
  1430. &znode->zbranch[0]);
  1431. if (err < 0)
  1432. return err;
  1433. if (err) {
  1434. ubifs_msg("first znode");
  1435. dbg_dump_znode(c, prev);
  1436. ubifs_msg("second znode");
  1437. dbg_dump_znode(c, znode);
  1438. return -EINVAL;
  1439. }
  1440. }
  1441. }
  1442. if (extra) {
  1443. if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
  1444. ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
  1445. atomic_long_read(&c->clean_zn_cnt),
  1446. clean_cnt);
  1447. return -EINVAL;
  1448. }
  1449. if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
  1450. ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
  1451. atomic_long_read(&c->dirty_zn_cnt),
  1452. dirty_cnt);
  1453. return -EINVAL;
  1454. }
  1455. }
  1456. return 0;
  1457. }
  1458. /**
  1459. * dbg_walk_index - walk the on-flash index.
  1460. * @c: UBIFS file-system description object
  1461. * @leaf_cb: called for each leaf node
  1462. * @znode_cb: called for each indexing node
  1463. * @priv: private data which is passed to callbacks
  1464. *
  1465. * This function walks the UBIFS index and calls the @leaf_cb for each leaf
  1466. * node and @znode_cb for each indexing node. Returns zero in case of success
  1467. * and a negative error code in case of failure.
  1468. *
  1469. * It would be better if this function removed every znode it pulled to into
  1470. * the TNC, so that the behavior more closely matched the non-debugging
  1471. * behavior.
  1472. */
  1473. int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
  1474. dbg_znode_callback znode_cb, void *priv)
  1475. {
  1476. int err;
  1477. struct ubifs_zbranch *zbr;
  1478. struct ubifs_znode *znode, *child;
  1479. mutex_lock(&c->tnc_mutex);
  1480. /* If the root indexing node is not in TNC - pull it */
  1481. if (!c->zroot.znode) {
  1482. c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
  1483. if (IS_ERR(c->zroot.znode)) {
  1484. err = PTR_ERR(c->zroot.znode);
  1485. c->zroot.znode = NULL;
  1486. goto out_unlock;
  1487. }
  1488. }
  1489. /*
  1490. * We are going to traverse the indexing tree in the postorder manner.
  1491. * Go down and find the leftmost indexing node where we are going to
  1492. * start from.
  1493. */
  1494. znode = c->zroot.znode;
  1495. while (znode->level > 0) {
  1496. zbr = &znode->zbranch[0];
  1497. child = zbr->znode;
  1498. if (!child) {
  1499. child = ubifs_load_znode(c, zbr, znode, 0);
  1500. if (IS_ERR(child)) {
  1501. err = PTR_ERR(child);
  1502. goto out_unlock;
  1503. }
  1504. zbr->znode = child;
  1505. }
  1506. znode = child;
  1507. }
  1508. /* Iterate over all indexing nodes */
  1509. while (1) {
  1510. int idx;
  1511. cond_resched();
  1512. if (znode_cb) {
  1513. err = znode_cb(c, znode, priv);
  1514. if (err) {
  1515. ubifs_err("znode checking function returned "
  1516. "error %d", err);
  1517. dbg_dump_znode(c, znode);
  1518. goto out_dump;
  1519. }
  1520. }
  1521. if (leaf_cb && znode->level == 0) {
  1522. for (idx = 0; idx < znode->child_cnt; idx++) {
  1523. zbr = &znode->zbranch[idx];
  1524. err = leaf_cb(c, zbr, priv);
  1525. if (err) {
  1526. ubifs_err("leaf checking function "
  1527. "returned error %d, for leaf "
  1528. "at LEB %d:%d",
  1529. err, zbr->lnum, zbr->offs);
  1530. goto out_dump;
  1531. }
  1532. }
  1533. }
  1534. if (!znode->parent)
  1535. break;
  1536. idx = znode->iip + 1;
  1537. znode = znode->parent;
  1538. if (idx < znode->child_cnt) {
  1539. /* Switch to the next index in the parent */
  1540. zbr = &znode->zbranch[idx];
  1541. child = zbr->znode;
  1542. if (!child) {
  1543. child = ubifs_load_znode(c, zbr, znode, idx);
  1544. if (IS_ERR(child)) {
  1545. err = PTR_ERR(child);
  1546. goto out_unlock;
  1547. }
  1548. zbr->znode = child;
  1549. }
  1550. znode = child;
  1551. } else
  1552. /*
  1553. * This is the last child, switch to the parent and
  1554. * continue.
  1555. */
  1556. continue;
  1557. /* Go to the lowest leftmost znode in the new sub-tree */
  1558. while (znode->level > 0) {
  1559. zbr = &znode->zbranch[0];
  1560. child = zbr->znode;
  1561. if (!child) {
  1562. child = ubifs_load_znode(c, zbr, znode, 0);
  1563. if (IS_ERR(child)) {
  1564. err = PTR_ERR(child);
  1565. goto out_unlock;
  1566. }
  1567. zbr->znode = child;
  1568. }
  1569. znode = child;
  1570. }
  1571. }
  1572. mutex_unlock(&c->tnc_mutex);
  1573. return 0;
  1574. out_dump:
  1575. if (znode->parent)
  1576. zbr = &znode->parent->zbranch[znode->iip];
  1577. else
  1578. zbr = &c->zroot;
  1579. ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
  1580. dbg_dump_znode(c, znode);
  1581. out_unlock:
  1582. mutex_unlock(&c->tnc_mutex);
  1583. return err;
  1584. }
  1585. /**
  1586. * add_size - add znode size to partially calculated index size.
  1587. * @c: UBIFS file-system description object
  1588. * @znode: znode to add size for
  1589. * @priv: partially calculated index size
  1590. *
  1591. * This is a helper function for 'dbg_check_idx_size()' which is called for
  1592. * every indexing node and adds its size to the 'long long' variable pointed to
  1593. * by @priv.
  1594. */
  1595. static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
  1596. {
  1597. long long *idx_size = priv;
  1598. int add;
  1599. add = ubifs_idx_node_sz(c, znode->child_cnt);
  1600. add = ALIGN(add, 8);
  1601. *idx_size += add;
  1602. return 0;
  1603. }
  1604. /**
  1605. * dbg_check_idx_size - check index size.
  1606. * @c: UBIFS file-system description object
  1607. * @idx_size: size to check
  1608. *
  1609. * This function walks the UBIFS index, calculates its size and checks that the
  1610. * size is equivalent to @idx_size. Returns zero in case of success and a
  1611. * negative error code in case of failure.
  1612. */
  1613. int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
  1614. {
  1615. int err;
  1616. long long calc = 0;
  1617. if (!dbg_is_chk_index(c))
  1618. return 0;
  1619. err = dbg_walk_index(c, NULL, add_size, &calc);
  1620. if (err) {
  1621. ubifs_err("error %d while walking the index", err);
  1622. return err;
  1623. }
  1624. if (calc != idx_size) {
  1625. ubifs_err("index size check failed: calculated size is %lld, "
  1626. "should be %lld", calc, idx_size);
  1627. dump_stack();
  1628. return -EINVAL;
  1629. }
  1630. return 0;
  1631. }
  1632. /**
  1633. * struct fsck_inode - information about an inode used when checking the file-system.
  1634. * @rb: link in the RB-tree of inodes
  1635. * @inum: inode number
  1636. * @mode: inode type, permissions, etc
  1637. * @nlink: inode link count
  1638. * @xattr_cnt: count of extended attributes
  1639. * @references: how many directory/xattr entries refer this inode (calculated
  1640. * while walking the index)
  1641. * @calc_cnt: for directory inode count of child directories
  1642. * @size: inode size (read from on-flash inode)
  1643. * @xattr_sz: summary size of all extended attributes (read from on-flash
  1644. * inode)
  1645. * @calc_sz: for directories calculated directory size
  1646. * @calc_xcnt: count of extended attributes
  1647. * @calc_xsz: calculated summary size of all extended attributes
  1648. * @xattr_nms: sum of lengths of all extended attribute names belonging to this
  1649. * inode (read from on-flash inode)
  1650. * @calc_xnms: calculated sum of lengths of all extended attribute names
  1651. */
  1652. struct fsck_inode {
  1653. struct rb_node rb;
  1654. ino_t inum;
  1655. umode_t mode;
  1656. unsigned int nlink;
  1657. unsigned int xattr_cnt;
  1658. int references;
  1659. int calc_cnt;
  1660. long long size;
  1661. unsigned int xattr_sz;
  1662. long long calc_sz;
  1663. long long calc_xcnt;
  1664. long long calc_xsz;
  1665. unsigned int xattr_nms;
  1666. long long calc_xnms;
  1667. };
  1668. /**
  1669. * struct fsck_data - private FS checking information.
  1670. * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
  1671. */
  1672. struct fsck_data {
  1673. struct rb_root inodes;
  1674. };
  1675. /**
  1676. * add_inode - add inode information to RB-tree of inodes.
  1677. * @c: UBIFS file-system description object
  1678. * @fsckd: FS checking information
  1679. * @ino: raw UBIFS inode to add
  1680. *
  1681. * This is a helper function for 'check_leaf()' which adds information about
  1682. * inode @ino to the RB-tree of inodes. Returns inode information pointer in
  1683. * case of success and a negative error code in case of failure.
  1684. */
  1685. static struct fsck_inode *add_inode(struct ubifs_info *c,
  1686. struct fsck_data *fsckd,
  1687. struct ubifs_ino_node *ino)
  1688. {
  1689. struct rb_node **p, *parent = NULL;
  1690. struct fsck_inode *fscki;
  1691. ino_t inum = key_inum_flash(c, &ino->key);
  1692. struct inode *inode;
  1693. struct ubifs_inode *ui;
  1694. p = &fsckd->inodes.rb_node;
  1695. while (*p) {
  1696. parent = *p;
  1697. fscki = rb_entry(parent, struct fsck_inode, rb);
  1698. if (inum < fscki->inum)
  1699. p = &(*p)->rb_left;
  1700. else if (inum > fscki->inum)
  1701. p = &(*p)->rb_right;
  1702. else
  1703. return fscki;
  1704. }
  1705. if (inum > c->highest_inum) {
  1706. ubifs_err("too high inode number, max. is %lu",
  1707. (unsigned long)c->highest_inum);
  1708. return ERR_PTR(-EINVAL);
  1709. }
  1710. fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
  1711. if (!fscki)
  1712. return ERR_PTR(-ENOMEM);
  1713. inode = ilookup(c->vfs_sb, inum);
  1714. fscki->inum = inum;
  1715. /*
  1716. * If the inode is present in the VFS inode cache, use it instead of
  1717. * the on-flash inode which might be out-of-date. E.g., the size might
  1718. * be out-of-date. If we do not do this, the following may happen, for
  1719. * example:
  1720. * 1. A power cut happens
  1721. * 2. We mount the file-system R/O, the replay process fixes up the
  1722. * inode size in the VFS cache, but on on-flash.
  1723. * 3. 'check_leaf()' fails because it hits a data node beyond inode
  1724. * size.
  1725. */
  1726. if (!inode) {
  1727. fscki->nlink = le32_to_cpu(ino->nlink);
  1728. fscki->size = le64_to_cpu(ino->size);
  1729. fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
  1730. fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
  1731. fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
  1732. fscki->mode = le32_to_cpu(ino->mode);
  1733. } else {
  1734. ui = ubifs_inode(inode);
  1735. fscki->nlink = inode->i_nlink;
  1736. fscki->size = inode->i_size;
  1737. fscki->xattr_cnt = ui->xattr_cnt;
  1738. fscki->xattr_sz = ui->xattr_size;
  1739. fscki->xattr_nms = ui->xattr_names;
  1740. fscki->mode = inode->i_mode;
  1741. iput(inode);
  1742. }
  1743. if (S_ISDIR(fscki->mode)) {
  1744. fscki->calc_sz = UBIFS_INO_NODE_SZ;
  1745. fscki->calc_cnt = 2;
  1746. }
  1747. rb_link_node(&fscki->rb, parent, p);
  1748. rb_insert_color(&fscki->rb, &fsckd->inodes);
  1749. return fscki;
  1750. }
  1751. /**
  1752. * search_inode - search inode in the RB-tree of inodes.
  1753. * @fsckd: FS checking information
  1754. * @inum: inode number to search
  1755. *
  1756. * This is a helper function for 'check_leaf()' which searches inode @inum in
  1757. * the RB-tree of inodes and returns an inode information pointer or %NULL if
  1758. * the inode was not found.
  1759. */
  1760. static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
  1761. {
  1762. struct rb_node *p;
  1763. struct fsck_inode *fscki;
  1764. p = fsckd->inodes.rb_node;
  1765. while (p) {
  1766. fscki = rb_entry(p, struct fsck_inode, rb);
  1767. if (inum < fscki->inum)
  1768. p = p->rb_left;
  1769. else if (inum > fscki->inum)
  1770. p = p->rb_right;
  1771. else
  1772. return fscki;
  1773. }
  1774. return NULL;
  1775. }
  1776. /**
  1777. * read_add_inode - read inode node and add it to RB-tree of inodes.
  1778. * @c: UBIFS file-system description object
  1779. * @fsckd: FS checking information
  1780. * @inum: inode number to read
  1781. *
  1782. * This is a helper function for 'check_leaf()' which finds inode node @inum in
  1783. * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
  1784. * information pointer in case of success and a negative error code in case of
  1785. * failure.
  1786. */
  1787. static struct fsck_inode *read_add_inode(struct ubifs_info *c,
  1788. struct fsck_data *fsckd, ino_t inum)
  1789. {
  1790. int n, err;
  1791. union ubifs_key key;
  1792. struct ubifs_znode *znode;
  1793. struct ubifs_zbranch *zbr;
  1794. struct ubifs_ino_node *ino;
  1795. struct fsck_inode *fscki;
  1796. fscki = search_inode(fsckd, inum);
  1797. if (fscki)
  1798. return fscki;
  1799. ino_key_init(c, &key, inum);
  1800. err = ubifs_lookup_level0(c, &key, &znode, &n);
  1801. if (!err) {
  1802. ubifs_err("inode %lu not found in index", (unsigned long)inum);
  1803. return ERR_PTR(-ENOENT);
  1804. } else if (err < 0) {
  1805. ubifs_err("error %d while looking up inode %lu",
  1806. err, (unsigned long)inum);
  1807. return ERR_PTR(err);
  1808. }
  1809. zbr = &znode->zbranch[n];
  1810. if (zbr->len < UBIFS_INO_NODE_SZ) {
  1811. ubifs_err("bad node %lu node length %d",
  1812. (unsigned long)inum, zbr->len);
  1813. return ERR_PTR(-EINVAL);
  1814. }
  1815. ino = kmalloc(zbr->len, GFP_NOFS);
  1816. if (!ino)
  1817. return ERR_PTR(-ENOMEM);
  1818. err = ubifs_tnc_read_node(c, zbr, ino);
  1819. if (err) {
  1820. ubifs_err("cannot read inode node at LEB %d:%d, error %d",
  1821. zbr->lnum, zbr->offs, err);
  1822. kfree(ino);
  1823. return ERR_PTR(err);
  1824. }
  1825. fscki = add_inode(c, fsckd, ino);
  1826. kfree(ino);
  1827. if (IS_ERR(fscki)) {
  1828. ubifs_err("error %ld while adding inode %lu node",
  1829. PTR_ERR(fscki), (unsigned long)inum);
  1830. return fscki;
  1831. }
  1832. return fscki;
  1833. }
  1834. /**
  1835. * check_leaf - check leaf node.
  1836. * @c: UBIFS file-system description object
  1837. * @zbr: zbranch of the leaf node to check
  1838. * @priv: FS checking information
  1839. *
  1840. * This is a helper function for 'dbg_check_filesystem()' which is called for
  1841. * every single leaf node while walking the indexing tree. It checks that the
  1842. * leaf node referred from the indexing tree exists, has correct CRC, and does
  1843. * some other basic validation. This function is also responsible for building
  1844. * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
  1845. * calculates reference count, size, etc for each inode in order to later
  1846. * compare them to the information stored inside the inodes and detect possible
  1847. * inconsistencies. Returns zero in case of success and a negative error code
  1848. * in case of failure.
  1849. */
  1850. static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
  1851. void *priv)
  1852. {
  1853. ino_t inum;
  1854. void *node;
  1855. struct ubifs_ch *ch;
  1856. int err, type = key_type(c, &zbr->key);
  1857. struct fsck_inode *fscki;
  1858. if (zbr->len < UBIFS_CH_SZ) {
  1859. ubifs_err("bad leaf length %d (LEB %d:%d)",
  1860. zbr->len, zbr->lnum, zbr->offs);
  1861. return -EINVAL;
  1862. }
  1863. node = kmalloc(zbr->len, GFP_NOFS);
  1864. if (!node)
  1865. return -ENOMEM;
  1866. err = ubifs_tnc_read_node(c, zbr, node);
  1867. if (err) {
  1868. ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
  1869. zbr->lnum, zbr->offs, err);
  1870. goto out_free;
  1871. }
  1872. /* If this is an inode node, add it to RB-tree of inodes */
  1873. if (type == UBIFS_INO_KEY) {
  1874. fscki = add_inode(c, priv, node);
  1875. if (IS_ERR(fscki)) {
  1876. err = PTR_ERR(fscki);
  1877. ubifs_err("error %d while adding inode node", err);
  1878. goto out_dump;
  1879. }
  1880. goto out;
  1881. }
  1882. if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
  1883. type != UBIFS_DATA_KEY) {
  1884. ubifs_err("unexpected node type %d at LEB %d:%d",
  1885. type, zbr->lnum, zbr->offs);
  1886. err = -EINVAL;
  1887. goto out_free;
  1888. }
  1889. ch = node;
  1890. if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
  1891. ubifs_err("too high sequence number, max. is %llu",
  1892. c->max_sqnum);
  1893. err = -EINVAL;
  1894. goto out_dump;
  1895. }
  1896. if (type == UBIFS_DATA_KEY) {
  1897. long long blk_offs;
  1898. struct ubifs_data_node *dn = node;
  1899. /*
  1900. * Search the inode node this data node belongs to and insert
  1901. * it to the RB-tree of inodes.
  1902. */
  1903. inum = key_inum_flash(c, &dn->key);
  1904. fscki = read_add_inode(c, priv, inum);
  1905. if (IS_ERR(fscki)) {
  1906. err = PTR_ERR(fscki);
  1907. ubifs_err("error %d while processing data node and "
  1908. "trying to find inode node %lu",
  1909. err, (unsigned long)inum);
  1910. goto out_dump;
  1911. }
  1912. /* Make sure the data node is within inode size */
  1913. blk_offs = key_block_flash(c, &dn->key);
  1914. blk_offs <<= UBIFS_BLOCK_SHIFT;
  1915. blk_offs += le32_to_cpu(dn->size);
  1916. if (blk_offs > fscki->size) {
  1917. ubifs_err("data node at LEB %d:%d is not within inode "
  1918. "size %lld", zbr->lnum, zbr->offs,
  1919. fscki->size);
  1920. err = -EINVAL;
  1921. goto out_dump;
  1922. }
  1923. } else {
  1924. int nlen;
  1925. struct ubifs_dent_node *dent = node;
  1926. struct fsck_inode *fscki1;
  1927. err = ubifs_validate_entry(c, dent);
  1928. if (err)
  1929. goto out_dump;
  1930. /*
  1931. * Search the inode node this entry refers to and the parent
  1932. * inode node and insert them to the RB-tree of inodes.
  1933. */
  1934. inum = le64_to_cpu(dent->inum);
  1935. fscki = read_add_inode(c, priv, inum);
  1936. if (IS_ERR(fscki)) {
  1937. err = PTR_ERR(fscki);
  1938. ubifs_err("error %d while processing entry node and "
  1939. "trying to find inode node %lu",
  1940. err, (unsigned long)inum);
  1941. goto out_dump;
  1942. }
  1943. /* Count how many direntries or xentries refers this inode */
  1944. fscki->references += 1;
  1945. inum = key_inum_flash(c, &dent->key);
  1946. fscki1 = read_add_inode(c, priv, inum);
  1947. if (IS_ERR(fscki1)) {
  1948. err = PTR_ERR(fscki1);
  1949. ubifs_err("error %d while processing entry node and "
  1950. "trying to find parent inode node %lu",
  1951. err, (unsigned long)inum);
  1952. goto out_dump;
  1953. }
  1954. nlen = le16_to_cpu(dent->nlen);
  1955. if (type == UBIFS_XENT_KEY) {
  1956. fscki1->calc_xcnt += 1;
  1957. fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
  1958. fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
  1959. fscki1->calc_xnms += nlen;
  1960. } else {
  1961. fscki1->calc_sz += CALC_DENT_SIZE(nlen);
  1962. if (dent->type == UBIFS_ITYPE_DIR)
  1963. fscki1->calc_cnt += 1;
  1964. }
  1965. }
  1966. out:
  1967. kfree(node);
  1968. return 0;
  1969. out_dump:
  1970. ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
  1971. dbg_dump_node(c, node);
  1972. out_free:
  1973. kfree(node);
  1974. return err;
  1975. }
  1976. /**
  1977. * free_inodes - free RB-tree of inodes.
  1978. * @fsckd: FS checking information
  1979. */
  1980. static void free_inodes(struct fsck_data *fsckd)
  1981. {
  1982. struct rb_node *this = fsckd->inodes.rb_node;
  1983. struct fsck_inode *fscki;
  1984. while (this) {
  1985. if (this->rb_left)
  1986. this = this->rb_left;
  1987. else if (this->rb_right)
  1988. this = this->rb_right;
  1989. else {
  1990. fscki = rb_entry(this, struct fsck_inode, rb);
  1991. this = rb_parent(this);
  1992. if (this) {
  1993. if (this->rb_left == &fscki->rb)
  1994. this->rb_left = NULL;
  1995. else
  1996. this->rb_right = NULL;
  1997. }
  1998. kfree(fscki);
  1999. }
  2000. }
  2001. }
  2002. /**
  2003. * check_inodes - checks all inodes.
  2004. * @c: UBIFS file-system description object
  2005. * @fsckd: FS checking information
  2006. *
  2007. * This is a helper function for 'dbg_check_filesystem()' which walks the
  2008. * RB-tree of inodes after the index scan has been finished, and checks that
  2009. * inode nlink, size, etc are correct. Returns zero if inodes are fine,
  2010. * %-EINVAL if not, and a negative error code in case of failure.
  2011. */
  2012. static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
  2013. {
  2014. int n, err;
  2015. union ubifs_key key;
  2016. struct ubifs_znode *znode;
  2017. struct ubifs_zbranch *zbr;
  2018. struct ubifs_ino_node *ino;
  2019. struct fsck_inode *fscki;
  2020. struct rb_node *this = rb_first(&fsckd->inodes);
  2021. while (this) {
  2022. fscki = rb_entry(this, struct fsck_inode, rb);
  2023. this = rb_next(this);
  2024. if (S_ISDIR(fscki->mode)) {
  2025. /*
  2026. * Directories have to have exactly one reference (they
  2027. * cannot have hardlinks), although root inode is an
  2028. * exception.
  2029. */
  2030. if (fscki->inum != UBIFS_ROOT_INO &&
  2031. fscki->references != 1) {
  2032. ubifs_err("directory inode %lu has %d "
  2033. "direntries which refer it, but "
  2034. "should be 1",
  2035. (unsigned long)fscki->inum,
  2036. fscki->references);
  2037. goto out_dump;
  2038. }
  2039. if (fscki->inum == UBIFS_ROOT_INO &&
  2040. fscki->references != 0) {
  2041. ubifs_err("root inode %lu has non-zero (%d) "
  2042. "direntries which refer it",
  2043. (unsigned long)fscki->inum,
  2044. fscki->references);
  2045. goto out_dump;
  2046. }
  2047. if (fscki->calc_sz != fscki->size) {
  2048. ubifs_err("directory inode %lu size is %lld, "
  2049. "but calculated size is %lld",
  2050. (unsigned long)fscki->inum,
  2051. fscki->size, fscki->calc_sz);
  2052. goto out_dump;
  2053. }
  2054. if (fscki->calc_cnt != fscki->nlink) {
  2055. ubifs_err("directory inode %lu nlink is %d, "
  2056. "but calculated nlink is %d",
  2057. (unsigned long)fscki->inum,
  2058. fscki->nlink, fscki->calc_cnt);
  2059. goto out_dump;
  2060. }
  2061. } else {
  2062. if (fscki->references != fscki->nlink) {
  2063. ubifs_err("inode %lu nlink is %d, but "
  2064. "calculated nlink is %d",
  2065. (unsigned long)fscki->inum,
  2066. fscki->nlink, fscki->references);
  2067. goto out_dump;
  2068. }
  2069. }
  2070. if (fscki->xattr_sz != fscki->calc_xsz) {
  2071. ubifs_err("inode %lu has xattr size %u, but "
  2072. "calculated size is %lld",
  2073. (unsigned long)fscki->inum, fscki->xattr_sz,
  2074. fscki->calc_xsz);
  2075. goto out_dump;
  2076. }
  2077. if (fscki->xattr_cnt != fscki->calc_xcnt) {
  2078. ubifs_err("inode %lu has %u xattrs, but "
  2079. "calculated count is %lld",
  2080. (unsigned long)fscki->inum,
  2081. fscki->xattr_cnt, fscki->calc_xcnt);
  2082. goto out_dump;
  2083. }
  2084. if (fscki->xattr_nms != fscki->calc_xnms) {
  2085. ubifs_err("inode %lu has xattr names' size %u, but "
  2086. "calculated names' size is %lld",
  2087. (unsigned long)fscki->inum, fscki->xattr_nms,
  2088. fscki->calc_xnms);
  2089. goto out_dump;
  2090. }
  2091. }
  2092. return 0;
  2093. out_dump:
  2094. /* Read the bad inode and dump it */
  2095. ino_key_init(c, &key, fscki->inum);
  2096. err = ubifs_lookup_level0(c, &key, &znode, &n);
  2097. if (!err) {
  2098. ubifs_err("inode %lu not found in index",
  2099. (unsigned long)fscki->inum);
  2100. return -ENOENT;
  2101. } else if (err < 0) {
  2102. ubifs_err("error %d while looking up inode %lu",
  2103. err, (unsigned long)fscki->inum);
  2104. return err;
  2105. }
  2106. zbr = &znode->zbranch[n];
  2107. ino = kmalloc(zbr->len, GFP_NOFS);
  2108. if (!ino)
  2109. return -ENOMEM;
  2110. err = ubifs_tnc_read_node(c, zbr, ino);
  2111. if (err) {
  2112. ubifs_err("cannot read inode node at LEB %d:%d, error %d",
  2113. zbr->lnum, zbr->offs, err);
  2114. kfree(ino);
  2115. return err;
  2116. }
  2117. ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
  2118. (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
  2119. dbg_dump_node(c, ino);
  2120. kfree(ino);
  2121. return -EINVAL;
  2122. }
  2123. /**
  2124. * dbg_check_filesystem - check the file-system.
  2125. * @c: UBIFS file-system description object
  2126. *
  2127. * This function checks the file system, namely:
  2128. * o makes sure that all leaf nodes exist and their CRCs are correct;
  2129. * o makes sure inode nlink, size, xattr size/count are correct (for all
  2130. * inodes).
  2131. *
  2132. * The function reads whole indexing tree and all nodes, so it is pretty
  2133. * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
  2134. * not, and a negative error code in case of failure.
  2135. */
  2136. int dbg_check_filesystem(struct ubifs_info *c)
  2137. {
  2138. int err;
  2139. struct fsck_data fsckd;
  2140. if (!dbg_is_chk_fs(c))
  2141. return 0;
  2142. fsckd.inodes = RB_ROOT;
  2143. err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
  2144. if (err)
  2145. goto out_free;
  2146. err = check_inodes(c, &fsckd);
  2147. if (err)
  2148. goto out_free;
  2149. free_inodes(&fsckd);
  2150. return 0;
  2151. out_free:
  2152. ubifs_err("file-system check failed with error %d", err);
  2153. dump_stack();
  2154. free_inodes(&fsckd);
  2155. return err;
  2156. }
  2157. /**
  2158. * dbg_check_data_nodes_order - check that list of data nodes is sorted.
  2159. * @c: UBIFS file-system description object
  2160. * @head: the list of nodes ('struct ubifs_scan_node' objects)
  2161. *
  2162. * This function returns zero if the list of data nodes is sorted correctly,
  2163. * and %-EINVAL if not.
  2164. */
  2165. int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)
  2166. {
  2167. struct list_head *cur;
  2168. struct ubifs_scan_node *sa, *sb;
  2169. if (!dbg_is_chk_gen(c))
  2170. return 0;
  2171. for (cur = head->next; cur->next != head; cur = cur->next) {
  2172. ino_t inuma, inumb;
  2173. uint32_t blka, blkb;
  2174. cond_resched();
  2175. sa = container_of(cur, struct ubifs_scan_node, list);
  2176. sb = container_of(cur->next, struct ubifs_scan_node, list);
  2177. if (sa->type != UBIFS_DATA_NODE) {
  2178. ubifs_err("bad node type %d", sa->type);
  2179. dbg_dump_node(c, sa->node);
  2180. return -EINVAL;
  2181. }
  2182. if (sb->type != UBIFS_DATA_NODE) {
  2183. ubifs_err("bad node type %d", sb->type);
  2184. dbg_dump_node(c, sb->node);
  2185. return -EINVAL;
  2186. }
  2187. inuma = key_inum(c, &sa->key);
  2188. inumb = key_inum(c, &sb->key);
  2189. if (inuma < inumb)
  2190. continue;
  2191. if (inuma > inumb) {
  2192. ubifs_err("larger inum %lu goes before inum %lu",
  2193. (unsigned long)inuma, (unsigned long)inumb);
  2194. goto error_dump;
  2195. }
  2196. blka = key_block(c, &sa->key);
  2197. blkb = key_block(c, &sb->key);
  2198. if (blka > blkb) {
  2199. ubifs_err("larger block %u goes before %u", blka, blkb);
  2200. goto error_dump;
  2201. }
  2202. if (blka == blkb) {
  2203. ubifs_err("two data nodes for the same block");
  2204. goto error_dump;
  2205. }
  2206. }
  2207. return 0;
  2208. error_dump:
  2209. dbg_dump_node(c, sa->node);
  2210. dbg_dump_node(c, sb->node);
  2211. return -EINVAL;
  2212. }
  2213. /**
  2214. * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
  2215. * @c: UBIFS file-system description object
  2216. * @head: the list of nodes ('struct ubifs_scan_node' objects)
  2217. *
  2218. * This function returns zero if the list of non-data nodes is sorted correctly,
  2219. * and %-EINVAL if not.
  2220. */
  2221. int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
  2222. {
  2223. struct list_head *cur;
  2224. struct ubifs_scan_node *sa, *sb;
  2225. if (!dbg_is_chk_gen(c))
  2226. return 0;
  2227. for (cur = head->next; cur->next != head; cur = cur->next) {
  2228. ino_t inuma, inumb;
  2229. uint32_t hasha, hashb;
  2230. cond_resched();
  2231. sa = container_of(cur, struct ubifs_scan_node, list);
  2232. sb = container_of(cur->next, struct ubifs_scan_node, list);
  2233. if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
  2234. sa->type != UBIFS_XENT_NODE) {
  2235. ubifs_err("bad node type %d", sa->type);
  2236. dbg_dump_node(c, sa->node);
  2237. return -EINVAL;
  2238. }
  2239. if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
  2240. sa->type != UBIFS_XENT_NODE) {
  2241. ubifs_err("bad node type %d", sb->type);
  2242. dbg_dump_node(c, sb->node);
  2243. return -EINVAL;
  2244. }
  2245. if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
  2246. ubifs_err("non-inode node goes before inode node");
  2247. goto error_dump;
  2248. }
  2249. if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE)
  2250. continue;
  2251. if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
  2252. /* Inode nodes are sorted in descending size order */
  2253. if (sa->len < sb->len) {
  2254. ubifs_err("smaller inode node goes first");
  2255. goto error_dump;
  2256. }
  2257. continue;
  2258. }
  2259. /*
  2260. * This is either a dentry or xentry, which should be sorted in
  2261. * ascending (parent ino, hash) order.
  2262. */
  2263. inuma = key_inum(c, &sa->key);
  2264. inumb = key_inum(c, &sb->key);
  2265. if (inuma < inumb)
  2266. continue;
  2267. if (inuma > inumb) {
  2268. ubifs_err("larger inum %lu goes before inum %lu",
  2269. (unsigned long)inuma, (unsigned long)inumb);
  2270. goto error_dump;
  2271. }
  2272. hasha = key_block(c, &sa->key);
  2273. hashb = key_block(c, &sb->key);
  2274. if (hasha > hashb) {
  2275. ubifs_err("larger hash %u goes before %u",
  2276. hasha, hashb);
  2277. goto error_dump;
  2278. }
  2279. }
  2280. return 0;
  2281. error_dump:
  2282. ubifs_msg("dumping first node");
  2283. dbg_dump_node(c, sa->node);
  2284. ubifs_msg("dumping second node");
  2285. dbg_dump_node(c, sb->node);
  2286. return -EINVAL;
  2287. return 0;
  2288. }
  2289. /* Failure mode for recovery testing */
  2290. #define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
  2291. static unsigned int next;
  2292. static int simple_rand(void)
  2293. {
  2294. if (next == 0)
  2295. next = current->pid;
  2296. next = next * 1103515245 + 12345;
  2297. return (next >> 16) & 32767;
  2298. }
  2299. static int do_fail(struct ubifs_info *c, int lnum, int write)
  2300. {
  2301. struct ubifs_debug_info *d = c->dbg;
  2302. ubifs_assert(dbg_is_tst_rcvry(c));
  2303. if (d->failure_mode)
  2304. return 1;
  2305. if (!d->fail_cnt) {
  2306. /* First call - decide delay to failure */
  2307. if (chance(1, 2)) {
  2308. unsigned int delay = 1 << (simple_rand() >> 11);
  2309. if (chance(1, 2)) {
  2310. d->fail_delay = 1;
  2311. d->fail_timeout = jiffies +
  2312. msecs_to_jiffies(delay);
  2313. ubifs_warn("failing after %ums", delay);
  2314. } else {
  2315. d->fail_delay = 2;
  2316. d->fail_cnt_max = delay;
  2317. ubifs_warn("failing after %u calls", delay);
  2318. }
  2319. }
  2320. d->fail_cnt += 1;
  2321. }
  2322. /* Determine if failure delay has expired */
  2323. if (d->fail_delay == 1) {
  2324. if (time_before(jiffies, d->fail_timeout))
  2325. return 0;
  2326. } else if (d->fail_delay == 2)
  2327. if (d->fail_cnt++ < d->fail_cnt_max)
  2328. return 0;
  2329. if (lnum == UBIFS_SB_LNUM) {
  2330. if (write) {
  2331. if (chance(1, 2))
  2332. return 0;
  2333. } else if (chance(19, 20))
  2334. return 0;
  2335. ubifs_warn("failing in super block LEB %d", lnum);
  2336. } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
  2337. if (chance(19, 20))
  2338. return 0;
  2339. ubifs_warn("failing in master LEB %d", lnum);
  2340. } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
  2341. if (write) {
  2342. if (chance(99, 100))
  2343. return 0;
  2344. } else if (chance(399, 400))
  2345. return 0;
  2346. ubifs_warn("failing in log LEB %d", lnum);
  2347. } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
  2348. if (write) {
  2349. if (chance(7, 8))
  2350. return 0;
  2351. } else if (chance(19, 20))
  2352. return 0;
  2353. ubifs_warn("failing in LPT LEB %d", lnum);
  2354. } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
  2355. if (write) {
  2356. if (chance(1, 2))
  2357. return 0;
  2358. } else if (chance(9, 10))
  2359. return 0;
  2360. ubifs_warn("failing in orphan LEB %d", lnum);
  2361. } else if (lnum == c->ihead_lnum) {
  2362. if (chance(99, 100))
  2363. return 0;
  2364. ubifs_warn("failing in index head LEB %d", lnum);
  2365. } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
  2366. if (chance(9, 10))
  2367. return 0;
  2368. ubifs_warn("failing in GC head LEB %d", lnum);
  2369. } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
  2370. !ubifs_search_bud(c, lnum)) {
  2371. if (chance(19, 20))
  2372. return 0;
  2373. ubifs_warn("failing in non-bud LEB %d", lnum);
  2374. } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
  2375. c->cmt_state == COMMIT_RUNNING_REQUIRED) {
  2376. if (chance(999, 1000))
  2377. return 0;
  2378. ubifs_warn("failing in bud LEB %d commit running", lnum);
  2379. } else {
  2380. if (chance(9999, 10000))
  2381. return 0;
  2382. ubifs_warn("failing in bud LEB %d commit not running", lnum);
  2383. }
  2384. d->failure_mode = 1;
  2385. dump_stack();
  2386. return 1;
  2387. }
  2388. static void cut_data(const void *buf, int len)
  2389. {
  2390. int flen, i;
  2391. unsigned char *p = (void *)buf;
  2392. flen = (len * (long long)simple_rand()) >> 15;
  2393. for (i = flen; i < len; i++)
  2394. p[i] = 0xff;
  2395. }
  2396. int dbg_leb_write(struct ubifs_info *c, int lnum, const void *buf,
  2397. int offs, int len, int dtype)
  2398. {
  2399. int err, failing;
  2400. if (c->dbg->failure_mode)
  2401. return -EROFS;
  2402. failing = do_fail(c, lnum, 1);
  2403. if (failing)
  2404. cut_data(buf, len);
  2405. err = ubi_leb_write(c->ubi, lnum, buf, offs, len, dtype);
  2406. if (err)
  2407. return err;
  2408. if (failing)
  2409. return -EROFS;
  2410. return 0;
  2411. }
  2412. int dbg_leb_change(struct ubifs_info *c, int lnum, const void *buf,
  2413. int len, int dtype)
  2414. {
  2415. int err;
  2416. if (do_fail(c, lnum, 1))
  2417. return -EROFS;
  2418. err = ubi_leb_change(c->ubi, lnum, buf, len, dtype);
  2419. if (err)
  2420. return err;
  2421. if (do_fail(c, lnum, 1))
  2422. return -EROFS;
  2423. return 0;
  2424. }
  2425. int dbg_leb_unmap(struct ubifs_info *c, int lnum)
  2426. {
  2427. int err;
  2428. if (do_fail(c, lnum, 0))
  2429. return -EROFS;
  2430. err = ubi_leb_unmap(c->ubi, lnum);
  2431. if (err)
  2432. return err;
  2433. if (do_fail(c, lnum, 0))
  2434. return -EROFS;
  2435. return 0;
  2436. }
  2437. int dbg_leb_map(struct ubifs_info *c, int lnum, int dtype)
  2438. {
  2439. int err;
  2440. if (do_fail(c, lnum, 0))
  2441. return -EROFS;
  2442. err = ubi_leb_map(c->ubi, lnum, dtype);
  2443. if (err)
  2444. return err;
  2445. if (do_fail(c, lnum, 0))
  2446. return -EROFS;
  2447. return 0;
  2448. }
  2449. /*
  2450. * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
  2451. * contain the stuff specific to particular file-system mounts.
  2452. */
  2453. static struct dentry *dfs_rootdir;
  2454. static int dfs_file_open(struct inode *inode, struct file *file)
  2455. {
  2456. file->private_data = inode->i_private;
  2457. return nonseekable_open(inode, file);
  2458. }
  2459. /**
  2460. * provide_user_output - provide output to the user reading a debugfs file.
  2461. * @val: boolean value for the answer
  2462. * @u: the buffer to store the answer at
  2463. * @count: size of the buffer
  2464. * @ppos: position in the @u output buffer
  2465. *
  2466. * This is a simple helper function which stores @val boolean value in the user
  2467. * buffer when the user reads one of UBIFS debugfs files. Returns amount of
  2468. * bytes written to @u in case of success and a negative error code in case of
  2469. * failure.
  2470. */
  2471. static int provide_user_output(int val, char __user *u, size_t count,
  2472. loff_t *ppos)
  2473. {
  2474. char buf[3];
  2475. if (val)
  2476. buf[0] = '1';
  2477. else
  2478. buf[0] = '0';
  2479. buf[1] = '\n';
  2480. buf[2] = 0x00;
  2481. return simple_read_from_buffer(u, count, ppos, buf, 2);
  2482. }
  2483. static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count,
  2484. loff_t *ppos)
  2485. {
  2486. struct dentry *dent = file->f_path.dentry;
  2487. struct ubifs_info *c = file->private_data;
  2488. struct ubifs_debug_info *d = c->dbg;
  2489. int val;
  2490. if (dent == d->dfs_chk_gen)
  2491. val = d->chk_gen;
  2492. else if (dent == d->dfs_chk_index)
  2493. val = d->chk_index;
  2494. else if (dent == d->dfs_chk_orph)
  2495. val = d->chk_orph;
  2496. else if (dent == d->dfs_chk_lprops)
  2497. val = d->chk_lprops;
  2498. else if (dent == d->dfs_chk_fs)
  2499. val = d->chk_fs;
  2500. else if (dent == d->dfs_tst_rcvry)
  2501. val = d->tst_rcvry;
  2502. else
  2503. return -EINVAL;
  2504. return provide_user_output(val, u, count, ppos);
  2505. }
  2506. /**
  2507. * interpret_user_input - interpret user debugfs file input.
  2508. * @u: user-provided buffer with the input
  2509. * @count: buffer size
  2510. *
  2511. * This is a helper function which interpret user input to a boolean UBIFS
  2512. * debugfs file. Returns %0 or %1 in case of success and a negative error code
  2513. * in case of failure.
  2514. */
  2515. static int interpret_user_input(const char __user *u, size_t count)
  2516. {
  2517. size_t buf_size;
  2518. char buf[8];
  2519. buf_size = min_t(size_t, count, (sizeof(buf) - 1));
  2520. if (copy_from_user(buf, u, buf_size))
  2521. return -EFAULT;
  2522. if (buf[0] == '1')
  2523. return 1;
  2524. else if (buf[0] == '0')
  2525. return 0;
  2526. return -EINVAL;
  2527. }
  2528. static ssize_t dfs_file_write(struct file *file, const char __user *u,
  2529. size_t count, loff_t *ppos)
  2530. {
  2531. struct ubifs_info *c = file->private_data;
  2532. struct ubifs_debug_info *d = c->dbg;
  2533. struct dentry *dent = file->f_path.dentry;
  2534. int val;
  2535. /*
  2536. * TODO: this is racy - the file-system might have already been
  2537. * unmounted and we'd oops in this case. The plan is to fix it with
  2538. * help of 'iterate_supers_type()' which we should have in v3.0: when
  2539. * a debugfs opened, we rember FS's UUID in file->private_data. Then
  2540. * whenever we access the FS via a debugfs file, we iterate all UBIFS
  2541. * superblocks and fine the one with the same UUID, and take the
  2542. * locking right.
  2543. *
  2544. * The other way to go suggested by Al Viro is to create a separate
  2545. * 'ubifs-debug' file-system instead.
  2546. */
  2547. if (file->f_path.dentry == d->dfs_dump_lprops) {
  2548. dbg_dump_lprops(c);
  2549. return count;
  2550. }
  2551. if (file->f_path.dentry == d->dfs_dump_budg) {
  2552. dbg_dump_budg(c, &c->bi);
  2553. return count;
  2554. }
  2555. if (file->f_path.dentry == d->dfs_dump_tnc) {
  2556. mutex_lock(&c->tnc_mutex);
  2557. dbg_dump_tnc(c);
  2558. mutex_unlock(&c->tnc_mutex);
  2559. return count;
  2560. }
  2561. val = interpret_user_input(u, count);
  2562. if (val < 0)
  2563. return val;
  2564. if (dent == d->dfs_chk_gen)
  2565. d->chk_gen = val;
  2566. else if (dent == d->dfs_chk_index)
  2567. d->chk_index = val;
  2568. else if (dent == d->dfs_chk_orph)
  2569. d->chk_orph = val;
  2570. else if (dent == d->dfs_chk_lprops)
  2571. d->chk_lprops = val;
  2572. else if (dent == d->dfs_chk_fs)
  2573. d->chk_fs = val;
  2574. else if (dent == d->dfs_tst_rcvry)
  2575. d->tst_rcvry = val;
  2576. else
  2577. return -EINVAL;
  2578. return count;
  2579. }
  2580. static const struct file_operations dfs_fops = {
  2581. .open = dfs_file_open,
  2582. .read = dfs_file_read,
  2583. .write = dfs_file_write,
  2584. .owner = THIS_MODULE,
  2585. .llseek = no_llseek,
  2586. };
  2587. /**
  2588. * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
  2589. * @c: UBIFS file-system description object
  2590. *
  2591. * This function creates all debugfs files for this instance of UBIFS. Returns
  2592. * zero in case of success and a negative error code in case of failure.
  2593. *
  2594. * Note, the only reason we have not merged this function with the
  2595. * 'ubifs_debugging_init()' function is because it is better to initialize
  2596. * debugfs interfaces at the very end of the mount process, and remove them at
  2597. * the very beginning of the mount process.
  2598. */
  2599. int dbg_debugfs_init_fs(struct ubifs_info *c)
  2600. {
  2601. int err, n;
  2602. const char *fname;
  2603. struct dentry *dent;
  2604. struct ubifs_debug_info *d = c->dbg;
  2605. n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME,
  2606. c->vi.ubi_num, c->vi.vol_id);
  2607. if (n == UBIFS_DFS_DIR_LEN) {
  2608. /* The array size is too small */
  2609. fname = UBIFS_DFS_DIR_NAME;
  2610. dent = ERR_PTR(-EINVAL);
  2611. goto out;
  2612. }
  2613. fname = d->dfs_dir_name;
  2614. dent = debugfs_create_dir(fname, dfs_rootdir);
  2615. if (IS_ERR_OR_NULL(dent))
  2616. goto out;
  2617. d->dfs_dir = dent;
  2618. fname = "dump_lprops";
  2619. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
  2620. if (IS_ERR_OR_NULL(dent))
  2621. goto out_remove;
  2622. d->dfs_dump_lprops = dent;
  2623. fname = "dump_budg";
  2624. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
  2625. if (IS_ERR_OR_NULL(dent))
  2626. goto out_remove;
  2627. d->dfs_dump_budg = dent;
  2628. fname = "dump_tnc";
  2629. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
  2630. if (IS_ERR_OR_NULL(dent))
  2631. goto out_remove;
  2632. d->dfs_dump_tnc = dent;
  2633. fname = "chk_general";
  2634. dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
  2635. &dfs_fops);
  2636. if (IS_ERR_OR_NULL(dent))
  2637. goto out_remove;
  2638. d->dfs_chk_gen = dent;
  2639. fname = "chk_index";
  2640. dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
  2641. &dfs_fops);
  2642. if (IS_ERR_OR_NULL(dent))
  2643. goto out_remove;
  2644. d->dfs_chk_index = dent;
  2645. fname = "chk_orphans";
  2646. dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
  2647. &dfs_fops);
  2648. if (IS_ERR_OR_NULL(dent))
  2649. goto out_remove;
  2650. d->dfs_chk_orph = dent;
  2651. fname = "chk_lprops";
  2652. dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
  2653. &dfs_fops);
  2654. if (IS_ERR_OR_NULL(dent))
  2655. goto out_remove;
  2656. d->dfs_chk_lprops = dent;
  2657. fname = "chk_fs";
  2658. dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
  2659. &dfs_fops);
  2660. if (IS_ERR_OR_NULL(dent))
  2661. goto out_remove;
  2662. d->dfs_chk_fs = dent;
  2663. fname = "tst_recovery";
  2664. dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
  2665. &dfs_fops);
  2666. if (IS_ERR_OR_NULL(dent))
  2667. goto out_remove;
  2668. d->dfs_tst_rcvry = dent;
  2669. return 0;
  2670. out_remove:
  2671. debugfs_remove_recursive(d->dfs_dir);
  2672. out:
  2673. err = dent ? PTR_ERR(dent) : -ENODEV;
  2674. ubifs_err("cannot create \"%s\" debugfs file or directory, error %d\n",
  2675. fname, err);
  2676. return err;
  2677. }
  2678. /**
  2679. * dbg_debugfs_exit_fs - remove all debugfs files.
  2680. * @c: UBIFS file-system description object
  2681. */
  2682. void dbg_debugfs_exit_fs(struct ubifs_info *c)
  2683. {
  2684. debugfs_remove_recursive(c->dbg->dfs_dir);
  2685. }
  2686. struct ubifs_global_debug_info ubifs_dbg;
  2687. static struct dentry *dfs_chk_gen;
  2688. static struct dentry *dfs_chk_index;
  2689. static struct dentry *dfs_chk_orph;
  2690. static struct dentry *dfs_chk_lprops;
  2691. static struct dentry *dfs_chk_fs;
  2692. static struct dentry *dfs_tst_rcvry;
  2693. static ssize_t dfs_global_file_read(struct file *file, char __user *u,
  2694. size_t count, loff_t *ppos)
  2695. {
  2696. struct dentry *dent = file->f_path.dentry;
  2697. int val;
  2698. if (dent == dfs_chk_gen)
  2699. val = ubifs_dbg.chk_gen;
  2700. else if (dent == dfs_chk_index)
  2701. val = ubifs_dbg.chk_index;
  2702. else if (dent == dfs_chk_orph)
  2703. val = ubifs_dbg.chk_orph;
  2704. else if (dent == dfs_chk_lprops)
  2705. val = ubifs_dbg.chk_lprops;
  2706. else if (dent == dfs_chk_fs)
  2707. val = ubifs_dbg.chk_fs;
  2708. else if (dent == dfs_tst_rcvry)
  2709. val = ubifs_dbg.tst_rcvry;
  2710. else
  2711. return -EINVAL;
  2712. return provide_user_output(val, u, count, ppos);
  2713. }
  2714. static ssize_t dfs_global_file_write(struct file *file, const char __user *u,
  2715. size_t count, loff_t *ppos)
  2716. {
  2717. struct dentry *dent = file->f_path.dentry;
  2718. int val;
  2719. val = interpret_user_input(u, count);
  2720. if (val < 0)
  2721. return val;
  2722. if (dent == dfs_chk_gen)
  2723. ubifs_dbg.chk_gen = val;
  2724. else if (dent == dfs_chk_index)
  2725. ubifs_dbg.chk_index = val;
  2726. else if (dent == dfs_chk_orph)
  2727. ubifs_dbg.chk_orph = val;
  2728. else if (dent == dfs_chk_lprops)
  2729. ubifs_dbg.chk_lprops = val;
  2730. else if (dent == dfs_chk_fs)
  2731. ubifs_dbg.chk_fs = val;
  2732. else if (dent == dfs_tst_rcvry)
  2733. ubifs_dbg.tst_rcvry = val;
  2734. else
  2735. return -EINVAL;
  2736. return count;
  2737. }
  2738. static const struct file_operations dfs_global_fops = {
  2739. .read = dfs_global_file_read,
  2740. .write = dfs_global_file_write,
  2741. .owner = THIS_MODULE,
  2742. .llseek = no_llseek,
  2743. };
  2744. /**
  2745. * dbg_debugfs_init - initialize debugfs file-system.
  2746. *
  2747. * UBIFS uses debugfs file-system to expose various debugging knobs to
  2748. * user-space. This function creates "ubifs" directory in the debugfs
  2749. * file-system. Returns zero in case of success and a negative error code in
  2750. * case of failure.
  2751. */
  2752. int dbg_debugfs_init(void)
  2753. {
  2754. int err;
  2755. const char *fname;
  2756. struct dentry *dent;
  2757. fname = "ubifs";
  2758. dent = debugfs_create_dir(fname, NULL);
  2759. if (IS_ERR_OR_NULL(dent))
  2760. goto out;
  2761. dfs_rootdir = dent;
  2762. fname = "chk_general";
  2763. dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
  2764. &dfs_global_fops);
  2765. if (IS_ERR_OR_NULL(dent))
  2766. goto out_remove;
  2767. dfs_chk_gen = dent;
  2768. fname = "chk_index";
  2769. dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
  2770. &dfs_global_fops);
  2771. if (IS_ERR_OR_NULL(dent))
  2772. goto out_remove;
  2773. dfs_chk_index = dent;
  2774. fname = "chk_orphans";
  2775. dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
  2776. &dfs_global_fops);
  2777. if (IS_ERR_OR_NULL(dent))
  2778. goto out_remove;
  2779. dfs_chk_orph = dent;
  2780. fname = "chk_lprops";
  2781. dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
  2782. &dfs_global_fops);
  2783. if (IS_ERR_OR_NULL(dent))
  2784. goto out_remove;
  2785. dfs_chk_lprops = dent;
  2786. fname = "chk_fs";
  2787. dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
  2788. &dfs_global_fops);
  2789. if (IS_ERR_OR_NULL(dent))
  2790. goto out_remove;
  2791. dfs_chk_fs = dent;
  2792. fname = "tst_recovery";
  2793. dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
  2794. &dfs_global_fops);
  2795. if (IS_ERR_OR_NULL(dent))
  2796. goto out_remove;
  2797. dfs_tst_rcvry = dent;
  2798. return 0;
  2799. out_remove:
  2800. debugfs_remove_recursive(dfs_rootdir);
  2801. out:
  2802. err = dent ? PTR_ERR(dent) : -ENODEV;
  2803. ubifs_err("cannot create \"%s\" debugfs file or directory, error %d\n",
  2804. fname, err);
  2805. return err;
  2806. }
  2807. /**
  2808. * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
  2809. */
  2810. void dbg_debugfs_exit(void)
  2811. {
  2812. debugfs_remove_recursive(dfs_rootdir);
  2813. }
  2814. /**
  2815. * ubifs_debugging_init - initialize UBIFS debugging.
  2816. * @c: UBIFS file-system description object
  2817. *
  2818. * This function initializes debugging-related data for the file system.
  2819. * Returns zero in case of success and a negative error code in case of
  2820. * failure.
  2821. */
  2822. int ubifs_debugging_init(struct ubifs_info *c)
  2823. {
  2824. c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
  2825. if (!c->dbg)
  2826. return -ENOMEM;
  2827. return 0;
  2828. }
  2829. /**
  2830. * ubifs_debugging_exit - free debugging data.
  2831. * @c: UBIFS file-system description object
  2832. */
  2833. void ubifs_debugging_exit(struct ubifs_info *c)
  2834. {
  2835. kfree(c->dbg);
  2836. }
  2837. #endif /* CONFIG_UBIFS_FS_DEBUG */