master.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. /* This file implements reading and writing the master node */
  23. #include "ubifs.h"
  24. /**
  25. * scan_for_master - search the valid master node.
  26. * @c: UBIFS file-system description object
  27. *
  28. * This function scans the master node LEBs and search for the latest master
  29. * node. Returns zero in case of success, %-EUCLEAN if there master area is
  30. * corrupted and requires recovery, and a negative error code in case of
  31. * failure.
  32. */
  33. static int scan_for_master(struct ubifs_info *c)
  34. {
  35. struct ubifs_scan_leb *sleb;
  36. struct ubifs_scan_node *snod;
  37. int lnum, offs = 0, nodes_cnt;
  38. lnum = UBIFS_MST_LNUM;
  39. #ifdef CONFIG_UBIFS_SHARE_BUFFER
  40. if (mutex_trylock(&ubifs_sbuf_mutex) == 0) {
  41. atomic_long_inc(&ubifs_sbuf_lock_count);
  42. ubifs_err("trylock fail count %ld\n", atomic_long_read(&ubifs_sbuf_lock_count));
  43. mutex_lock(&ubifs_sbuf_mutex);
  44. ubifs_err("locked count %ld\n", atomic_long_read(&ubifs_sbuf_lock_count));
  45. }
  46. #endif
  47. sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
  48. if (IS_ERR(sleb)) {
  49. #ifdef CONFIG_UBIFS_SHARE_BUFFER
  50. mutex_unlock(&ubifs_sbuf_mutex);
  51. #endif
  52. return PTR_ERR(sleb);
  53. }
  54. nodes_cnt = sleb->nodes_cnt;
  55. if (nodes_cnt > 0) {
  56. snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
  57. list);
  58. if (snod->type != UBIFS_MST_NODE)
  59. goto out_dump;
  60. memcpy(c->mst_node, snod->node, snod->len);
  61. offs = snod->offs;
  62. }
  63. ubifs_scan_destroy(sleb);
  64. lnum += 1;
  65. sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
  66. if (IS_ERR(sleb)) {
  67. #ifdef CONFIG_UBIFS_SHARE_BUFFER
  68. mutex_unlock(&ubifs_sbuf_mutex);
  69. #endif
  70. return PTR_ERR(sleb);
  71. }
  72. if (sleb->nodes_cnt != nodes_cnt)
  73. goto out;
  74. if (!sleb->nodes_cnt)
  75. goto out;
  76. snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, list);
  77. if (snod->type != UBIFS_MST_NODE)
  78. goto out_dump;
  79. if (snod->offs != offs)
  80. goto out;
  81. if (memcmp((void *)c->mst_node + UBIFS_CH_SZ,
  82. (void *)snod->node + UBIFS_CH_SZ,
  83. UBIFS_MST_NODE_SZ - UBIFS_CH_SZ))
  84. goto out;
  85. c->mst_offs = offs;
  86. ubifs_scan_destroy(sleb);
  87. #ifdef CONFIG_UBIFS_SHARE_BUFFER
  88. mutex_unlock(&ubifs_sbuf_mutex);
  89. #endif
  90. return 0;
  91. out:
  92. ubifs_scan_destroy(sleb);
  93. #ifdef CONFIG_UBIFS_SHARE_BUFFER
  94. mutex_unlock(&ubifs_sbuf_mutex);
  95. #endif
  96. return -EUCLEAN;
  97. out_dump:
  98. ubifs_err("unexpected node type %d master LEB %d:%d",
  99. snod->type, lnum, snod->offs);
  100. ubifs_scan_destroy(sleb);
  101. #ifdef CONFIG_UBIFS_SHARE_BUFFER
  102. mutex_unlock(&ubifs_sbuf_mutex);
  103. #endif
  104. return -EINVAL;
  105. }
  106. /**
  107. * validate_master - validate master node.
  108. * @c: UBIFS file-system description object
  109. *
  110. * This function validates data which was read from master node. Returns zero
  111. * if the data is all right and %-EINVAL if not.
  112. */
  113. static int validate_master(const struct ubifs_info *c)
  114. {
  115. long long main_sz;
  116. int err;
  117. if (c->max_sqnum >= SQNUM_WATERMARK) {
  118. err = 1;
  119. goto out;
  120. }
  121. if (c->cmt_no >= c->max_sqnum) {
  122. err = 2;
  123. goto out;
  124. }
  125. if (c->highest_inum >= INUM_WATERMARK) {
  126. err = 3;
  127. goto out;
  128. }
  129. if (c->lhead_lnum < UBIFS_LOG_LNUM ||
  130. c->lhead_lnum >= UBIFS_LOG_LNUM + c->log_lebs ||
  131. c->lhead_offs < 0 || c->lhead_offs >= c->leb_size ||
  132. c->lhead_offs & (c->min_io_size - 1)) {
  133. err = 4;
  134. goto out;
  135. }
  136. if (c->zroot.lnum >= c->leb_cnt || c->zroot.lnum < c->main_first ||
  137. c->zroot.offs >= c->leb_size || c->zroot.offs & 7) {
  138. err = 5;
  139. goto out;
  140. }
  141. if (c->zroot.len < c->ranges[UBIFS_IDX_NODE].min_len ||
  142. c->zroot.len > c->ranges[UBIFS_IDX_NODE].max_len) {
  143. err = 6;
  144. goto out;
  145. }
  146. if (c->gc_lnum >= c->leb_cnt || c->gc_lnum < c->main_first) {
  147. err = 7;
  148. goto out;
  149. }
  150. if (c->ihead_lnum >= c->leb_cnt || c->ihead_lnum < c->main_first ||
  151. c->ihead_offs % c->min_io_size || c->ihead_offs < 0 ||
  152. c->ihead_offs > c->leb_size || c->ihead_offs & 7) {
  153. err = 8;
  154. goto out;
  155. }
  156. main_sz = (long long)c->main_lebs * c->leb_size;
  157. if (c->bi.old_idx_sz & 7 || c->bi.old_idx_sz >= main_sz) {
  158. err = 9;
  159. goto out;
  160. }
  161. if (c->lpt_lnum < c->lpt_first || c->lpt_lnum > c->lpt_last ||
  162. c->lpt_offs < 0 || c->lpt_offs + c->nnode_sz > c->leb_size) {
  163. err = 10;
  164. goto out;
  165. }
  166. if (c->nhead_lnum < c->lpt_first || c->nhead_lnum > c->lpt_last ||
  167. c->nhead_offs < 0 || c->nhead_offs % c->min_io_size ||
  168. c->nhead_offs > c->leb_size) {
  169. err = 11;
  170. goto out;
  171. }
  172. if (c->ltab_lnum < c->lpt_first || c->ltab_lnum > c->lpt_last ||
  173. c->ltab_offs < 0 ||
  174. c->ltab_offs + c->ltab_sz > c->leb_size) {
  175. err = 12;
  176. goto out;
  177. }
  178. if (c->big_lpt && (c->lsave_lnum < c->lpt_first ||
  179. c->lsave_lnum > c->lpt_last || c->lsave_offs < 0 ||
  180. c->lsave_offs + c->lsave_sz > c->leb_size)) {
  181. err = 13;
  182. goto out;
  183. }
  184. if (c->lscan_lnum < c->main_first || c->lscan_lnum >= c->leb_cnt) {
  185. err = 14;
  186. goto out;
  187. }
  188. if (c->lst.empty_lebs < 0 || c->lst.empty_lebs > c->main_lebs - 2) {
  189. err = 15;
  190. goto out;
  191. }
  192. if (c->lst.idx_lebs < 0 || c->lst.idx_lebs > c->main_lebs - 1) {
  193. err = 16;
  194. goto out;
  195. }
  196. if (c->lst.total_free < 0 || c->lst.total_free > main_sz ||
  197. c->lst.total_free & 7) {
  198. err = 17;
  199. goto out;
  200. }
  201. if (c->lst.total_dirty < 0 || (c->lst.total_dirty & 7)) {
  202. err = 18;
  203. goto out;
  204. }
  205. if (c->lst.total_used < 0 || (c->lst.total_used & 7)) {
  206. err = 19;
  207. goto out;
  208. }
  209. if (c->lst.total_free + c->lst.total_dirty +
  210. c->lst.total_used > main_sz) {
  211. err = 20;
  212. goto out;
  213. }
  214. if (c->lst.total_dead + c->lst.total_dark +
  215. c->lst.total_used + c->bi.old_idx_sz > main_sz) {
  216. err = 21;
  217. goto out;
  218. }
  219. if (c->lst.total_dead < 0 ||
  220. c->lst.total_dead > c->lst.total_free + c->lst.total_dirty ||
  221. c->lst.total_dead & 7) {
  222. err = 22;
  223. goto out;
  224. }
  225. if (c->lst.total_dark < 0 ||
  226. c->lst.total_dark > c->lst.total_free + c->lst.total_dirty ||
  227. c->lst.total_dark & 7) {
  228. err = 23;
  229. goto out;
  230. }
  231. return 0;
  232. out:
  233. ubifs_err("bad master node at offset %d error %d", c->mst_offs, err);
  234. ubifs_dump_node(c, c->mst_node);
  235. return -EINVAL;
  236. }
  237. /**
  238. * ubifs_read_master - read master node.
  239. * @c: UBIFS file-system description object
  240. *
  241. * This function finds and reads the master node during file-system mount. If
  242. * the flash is empty, it creates default master node as well. Returns zero in
  243. * case of success and a negative error code in case of failure.
  244. */
  245. int ubifs_read_master(struct ubifs_info *c)
  246. {
  247. int err, old_leb_cnt;
  248. c->mst_node = kzalloc(c->mst_node_alsz, GFP_KERNEL);
  249. if (!c->mst_node)
  250. return -ENOMEM;
  251. err = scan_for_master(c);
  252. if (err) {
  253. if (err == -EUCLEAN)
  254. err = ubifs_recover_master_node(c);
  255. if (err)
  256. /*
  257. * Note, we do not free 'c->mst_node' here because the
  258. * unmount routine will take care of this.
  259. */
  260. return err;
  261. } else if ((!c->ro_mount) && (c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY)) != 0) {
  262. /* MTK force recover master node, when unclean reboot */
  263. ubifs_msg("recovery needed, recovery master node");
  264. err = ubifs_recover_master_node(c);
  265. if (err)
  266. return err;
  267. }
  268. /* Make sure that the recovery flag is clear */
  269. c->mst_node->flags &= cpu_to_le32(~UBIFS_MST_RCVRY);
  270. c->max_sqnum = le64_to_cpu(c->mst_node->ch.sqnum);
  271. c->highest_inum = le64_to_cpu(c->mst_node->highest_inum);
  272. c->cmt_no = le64_to_cpu(c->mst_node->cmt_no);
  273. c->zroot.lnum = le32_to_cpu(c->mst_node->root_lnum);
  274. c->zroot.offs = le32_to_cpu(c->mst_node->root_offs);
  275. c->zroot.len = le32_to_cpu(c->mst_node->root_len);
  276. c->lhead_lnum = le32_to_cpu(c->mst_node->log_lnum);
  277. c->gc_lnum = le32_to_cpu(c->mst_node->gc_lnum);
  278. c->ihead_lnum = le32_to_cpu(c->mst_node->ihead_lnum);
  279. c->ihead_offs = le32_to_cpu(c->mst_node->ihead_offs);
  280. c->bi.old_idx_sz = le64_to_cpu(c->mst_node->index_size);
  281. c->lpt_lnum = le32_to_cpu(c->mst_node->lpt_lnum);
  282. c->lpt_offs = le32_to_cpu(c->mst_node->lpt_offs);
  283. c->nhead_lnum = le32_to_cpu(c->mst_node->nhead_lnum);
  284. c->nhead_offs = le32_to_cpu(c->mst_node->nhead_offs);
  285. c->ltab_lnum = le32_to_cpu(c->mst_node->ltab_lnum);
  286. c->ltab_offs = le32_to_cpu(c->mst_node->ltab_offs);
  287. c->lsave_lnum = le32_to_cpu(c->mst_node->lsave_lnum);
  288. c->lsave_offs = le32_to_cpu(c->mst_node->lsave_offs);
  289. c->lscan_lnum = le32_to_cpu(c->mst_node->lscan_lnum);
  290. c->lst.empty_lebs = le32_to_cpu(c->mst_node->empty_lebs);
  291. c->lst.idx_lebs = le32_to_cpu(c->mst_node->idx_lebs);
  292. old_leb_cnt = le32_to_cpu(c->mst_node->leb_cnt);
  293. c->lst.total_free = le64_to_cpu(c->mst_node->total_free);
  294. c->lst.total_dirty = le64_to_cpu(c->mst_node->total_dirty);
  295. c->lst.total_used = le64_to_cpu(c->mst_node->total_used);
  296. c->lst.total_dead = le64_to_cpu(c->mst_node->total_dead);
  297. c->lst.total_dark = le64_to_cpu(c->mst_node->total_dark);
  298. c->calc_idx_sz = c->bi.old_idx_sz;
  299. if (c->mst_node->flags & cpu_to_le32(UBIFS_MST_NO_ORPHS))
  300. c->no_orphs = 1;
  301. if (old_leb_cnt != c->leb_cnt) {
  302. /* The file system has been resized */
  303. int growth = c->leb_cnt - old_leb_cnt;
  304. if (c->leb_cnt < old_leb_cnt ||
  305. c->leb_cnt < UBIFS_MIN_LEB_CNT) {
  306. ubifs_err("bad leb_cnt on master node");
  307. ubifs_dump_node(c, c->mst_node);
  308. return -EINVAL;
  309. }
  310. dbg_mnt("Auto resizing (master) from %d LEBs to %d LEBs",
  311. old_leb_cnt, c->leb_cnt);
  312. c->lst.empty_lebs += growth;
  313. c->lst.total_free += growth * (long long)c->leb_size;
  314. c->lst.total_dark += growth * (long long)c->dark_wm;
  315. /*
  316. * Reflect changes back onto the master node. N.B. the master
  317. * node gets written immediately whenever mounting (or
  318. * remounting) in read-write mode, so we do not need to write it
  319. * here.
  320. */
  321. c->mst_node->leb_cnt = cpu_to_le32(c->leb_cnt);
  322. c->mst_node->empty_lebs = cpu_to_le32(c->lst.empty_lebs);
  323. c->mst_node->total_free = cpu_to_le64(c->lst.total_free);
  324. c->mst_node->total_dark = cpu_to_le64(c->lst.total_dark);
  325. }
  326. err = validate_master(c);
  327. if (err)
  328. return err;
  329. err = dbg_old_index_check_init(c, &c->zroot);
  330. return err;
  331. }
  332. /**
  333. * ubifs_write_master - write master node.
  334. * @c: UBIFS file-system description object
  335. *
  336. * This function writes the master node. Returns zero in case of success and a
  337. * negative error code in case of failure. The master node is written twice to
  338. * enable recovery.
  339. */
  340. int ubifs_write_master(struct ubifs_info *c)
  341. {
  342. int err, lnum, offs, len;
  343. ubifs_assert(!c->ro_media && !c->ro_mount);
  344. if (c->ro_error)
  345. return -EROFS;
  346. lnum = UBIFS_MST_LNUM;
  347. offs = c->mst_offs + c->mst_node_alsz;
  348. len = UBIFS_MST_NODE_SZ;
  349. if (offs + UBIFS_MST_NODE_SZ > c->leb_size) {
  350. err = ubifs_leb_unmap(c, lnum);
  351. if (err)
  352. return err;
  353. offs = 0;
  354. }
  355. c->mst_offs = offs;
  356. c->mst_node->highest_inum = cpu_to_le64(c->highest_inum);
  357. err = ubifs_write_node(c, c->mst_node, len, lnum, offs);
  358. if (err)
  359. return err;
  360. lnum += 1;
  361. if (offs == 0) {
  362. err = ubifs_leb_unmap(c, lnum);
  363. if (err)
  364. return err;
  365. }
  366. err = ubifs_write_node(c, c->mst_node, len, lnum, offs);
  367. return err;
  368. }