debug.c 83 KB

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