debug.c 85 KB

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