debug.c 72 KB

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