debug.c 86 KB

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