debug.c 61 KB

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