debug.c 77 KB

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