debug.c 83 KB

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