debug.c 94 KB

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