debug.c 78 KB

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