debug.c 76 KB

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