debug.c 70 KB

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