debug.c 84 KB

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