debug.c 63 KB

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