debug.c 69 KB

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