debug.c 83 KB

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