orphan.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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. * Author: Adrian Hunter
  20. */
  21. #include "ubifs.h"
  22. /*
  23. * An orphan is an inode number whose inode node has been committed to the index
  24. * with a link count of zero. That happens when an open file is deleted
  25. * (unlinked) and then a commit is run. In the normal course of events the inode
  26. * would be deleted when the file is closed. However in the case of an unclean
  27. * unmount, orphans need to be accounted for. After an unclean unmount, the
  28. * orphans' inodes must be deleted which means either scanning the entire index
  29. * looking for them, or keeping a list on flash somewhere. This unit implements
  30. * the latter approach.
  31. *
  32. * The orphan area is a fixed number of LEBs situated between the LPT area and
  33. * the main area. The number of orphan area LEBs is specified when the file
  34. * system is created. The minimum number is 1. The size of the orphan area
  35. * should be so that it can hold the maximum number of orphans that are expected
  36. * to ever exist at one time.
  37. *
  38. * The number of orphans that can fit in a LEB is:
  39. *
  40. * (c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64)
  41. *
  42. * For example: a 15872 byte LEB can fit 1980 orphans so 1 LEB may be enough.
  43. *
  44. * Orphans are accumulated in a rb-tree. When an inode's link count drops to
  45. * zero, the inode number is added to the rb-tree. It is removed from the tree
  46. * when the inode is deleted. Any new orphans that are in the orphan tree when
  47. * the commit is run, are written to the orphan area in 1 or more orphan nodes.
  48. * If the orphan area is full, it is consolidated to make space. There is
  49. * always enough space because validation prevents the user from creating more
  50. * than the maximum number of orphans allowed.
  51. */
  52. static int dbg_check_orphans(struct ubifs_info *c);
  53. /**
  54. * ubifs_add_orphan - add an orphan.
  55. * @c: UBIFS file-system description object
  56. * @inum: orphan inode number
  57. *
  58. * Add an orphan. This function is called when an inodes link count drops to
  59. * zero.
  60. */
  61. int ubifs_add_orphan(struct ubifs_info *c, ino_t inum)
  62. {
  63. struct ubifs_orphan *orphan, *o;
  64. struct rb_node **p, *parent = NULL;
  65. orphan = kzalloc(sizeof(struct ubifs_orphan), GFP_NOFS);
  66. if (!orphan)
  67. return -ENOMEM;
  68. orphan->inum = inum;
  69. orphan->new = 1;
  70. spin_lock(&c->orphan_lock);
  71. if (c->tot_orphans >= c->max_orphans) {
  72. spin_unlock(&c->orphan_lock);
  73. kfree(orphan);
  74. return -ENFILE;
  75. }
  76. p = &c->orph_tree.rb_node;
  77. while (*p) {
  78. parent = *p;
  79. o = rb_entry(parent, struct ubifs_orphan, rb);
  80. if (inum < o->inum)
  81. p = &(*p)->rb_left;
  82. else if (inum > o->inum)
  83. p = &(*p)->rb_right;
  84. else {
  85. dbg_err("orphaned twice");
  86. spin_unlock(&c->orphan_lock);
  87. kfree(orphan);
  88. return 0;
  89. }
  90. }
  91. c->tot_orphans += 1;
  92. c->new_orphans += 1;
  93. rb_link_node(&orphan->rb, parent, p);
  94. rb_insert_color(&orphan->rb, &c->orph_tree);
  95. list_add_tail(&orphan->list, &c->orph_list);
  96. list_add_tail(&orphan->new_list, &c->orph_new);
  97. spin_unlock(&c->orphan_lock);
  98. dbg_gen("ino %lu", (unsigned long)inum);
  99. return 0;
  100. }
  101. /**
  102. * ubifs_delete_orphan - delete an orphan.
  103. * @c: UBIFS file-system description object
  104. * @inum: orphan inode number
  105. *
  106. * Delete an orphan. This function is called when an inode is deleted.
  107. */
  108. void ubifs_delete_orphan(struct ubifs_info *c, ino_t inum)
  109. {
  110. struct ubifs_orphan *o;
  111. struct rb_node *p;
  112. spin_lock(&c->orphan_lock);
  113. p = c->orph_tree.rb_node;
  114. while (p) {
  115. o = rb_entry(p, struct ubifs_orphan, rb);
  116. if (inum < o->inum)
  117. p = p->rb_left;
  118. else if (inum > o->inum)
  119. p = p->rb_right;
  120. else {
  121. if (o->dnext) {
  122. spin_unlock(&c->orphan_lock);
  123. dbg_gen("deleted twice ino %lu",
  124. (unsigned long)inum);
  125. return;
  126. }
  127. if (o->cnext) {
  128. o->dnext = c->orph_dnext;
  129. c->orph_dnext = o;
  130. spin_unlock(&c->orphan_lock);
  131. dbg_gen("delete later ino %lu",
  132. (unsigned long)inum);
  133. return;
  134. }
  135. rb_erase(p, &c->orph_tree);
  136. list_del(&o->list);
  137. c->tot_orphans -= 1;
  138. if (o->new) {
  139. list_del(&o->new_list);
  140. c->new_orphans -= 1;
  141. }
  142. spin_unlock(&c->orphan_lock);
  143. kfree(o);
  144. dbg_gen("inum %lu", (unsigned long)inum);
  145. return;
  146. }
  147. }
  148. spin_unlock(&c->orphan_lock);
  149. dbg_err("missing orphan ino %lu", (unsigned long)inum);
  150. dump_stack();
  151. }
  152. /**
  153. * ubifs_orphan_start_commit - start commit of orphans.
  154. * @c: UBIFS file-system description object
  155. *
  156. * Start commit of orphans.
  157. */
  158. int ubifs_orphan_start_commit(struct ubifs_info *c)
  159. {
  160. struct ubifs_orphan *orphan, **last;
  161. spin_lock(&c->orphan_lock);
  162. last = &c->orph_cnext;
  163. list_for_each_entry(orphan, &c->orph_new, new_list) {
  164. ubifs_assert(orphan->new);
  165. orphan->new = 0;
  166. *last = orphan;
  167. last = &orphan->cnext;
  168. }
  169. *last = orphan->cnext;
  170. c->cmt_orphans = c->new_orphans;
  171. c->new_orphans = 0;
  172. dbg_cmt("%d orphans to commit", c->cmt_orphans);
  173. INIT_LIST_HEAD(&c->orph_new);
  174. if (c->tot_orphans == 0)
  175. c->no_orphs = 1;
  176. else
  177. c->no_orphs = 0;
  178. spin_unlock(&c->orphan_lock);
  179. return 0;
  180. }
  181. /**
  182. * avail_orphs - calculate available space.
  183. * @c: UBIFS file-system description object
  184. *
  185. * This function returns the number of orphans that can be written in the
  186. * available space.
  187. */
  188. static int avail_orphs(struct ubifs_info *c)
  189. {
  190. int avail_lebs, avail, gap;
  191. avail_lebs = c->orph_lebs - (c->ohead_lnum - c->orph_first) - 1;
  192. avail = avail_lebs *
  193. ((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
  194. gap = c->leb_size - c->ohead_offs;
  195. if (gap >= UBIFS_ORPH_NODE_SZ + sizeof(__le64))
  196. avail += (gap - UBIFS_ORPH_NODE_SZ) / sizeof(__le64);
  197. return avail;
  198. }
  199. /**
  200. * tot_avail_orphs - calculate total space.
  201. * @c: UBIFS file-system description object
  202. *
  203. * This function returns the number of orphans that can be written in half
  204. * the total space. That leaves half the space for adding new orphans.
  205. */
  206. static int tot_avail_orphs(struct ubifs_info *c)
  207. {
  208. int avail_lebs, avail;
  209. avail_lebs = c->orph_lebs;
  210. avail = avail_lebs *
  211. ((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
  212. return avail / 2;
  213. }
  214. /**
  215. * do_write_orph_node - write a node to the orphan head.
  216. * @c: UBIFS file-system description object
  217. * @len: length of node
  218. * @atomic: write atomically
  219. *
  220. * This function writes a node to the orphan head from the orphan buffer. If
  221. * %atomic is not zero, then the write is done atomically. On success, %0 is
  222. * returned, otherwise a negative error code is returned.
  223. */
  224. static int do_write_orph_node(struct ubifs_info *c, int len, int atomic)
  225. {
  226. int err = 0;
  227. if (atomic) {
  228. ubifs_assert(c->ohead_offs == 0);
  229. ubifs_prepare_node(c, c->orph_buf, len, 1);
  230. len = ALIGN(len, c->min_io_size);
  231. err = ubifs_leb_change(c, c->ohead_lnum, c->orph_buf, len,
  232. UBI_SHORTTERM);
  233. } else {
  234. if (c->ohead_offs == 0) {
  235. /* Ensure LEB has been unmapped */
  236. err = ubifs_leb_unmap(c, c->ohead_lnum);
  237. if (err)
  238. return err;
  239. }
  240. err = ubifs_write_node(c, c->orph_buf, len, c->ohead_lnum,
  241. c->ohead_offs, UBI_SHORTTERM);
  242. }
  243. return err;
  244. }
  245. /**
  246. * write_orph_node - write an orphan node.
  247. * @c: UBIFS file-system description object
  248. * @atomic: write atomically
  249. *
  250. * This function builds an orphan node from the cnext list and writes it to the
  251. * orphan head. On success, %0 is returned, otherwise a negative error code
  252. * is returned.
  253. */
  254. static int write_orph_node(struct ubifs_info *c, int atomic)
  255. {
  256. struct ubifs_orphan *orphan, *cnext;
  257. struct ubifs_orph_node *orph;
  258. int gap, err, len, cnt, i;
  259. ubifs_assert(c->cmt_orphans > 0);
  260. gap = c->leb_size - c->ohead_offs;
  261. if (gap < UBIFS_ORPH_NODE_SZ + sizeof(__le64)) {
  262. c->ohead_lnum += 1;
  263. c->ohead_offs = 0;
  264. gap = c->leb_size;
  265. if (c->ohead_lnum > c->orph_last) {
  266. /*
  267. * We limit the number of orphans so that this should
  268. * never happen.
  269. */
  270. ubifs_err("out of space in orphan area");
  271. return -EINVAL;
  272. }
  273. }
  274. cnt = (gap - UBIFS_ORPH_NODE_SZ) / sizeof(__le64);
  275. if (cnt > c->cmt_orphans)
  276. cnt = c->cmt_orphans;
  277. len = UBIFS_ORPH_NODE_SZ + cnt * sizeof(__le64);
  278. ubifs_assert(c->orph_buf);
  279. orph = c->orph_buf;
  280. orph->ch.node_type = UBIFS_ORPH_NODE;
  281. spin_lock(&c->orphan_lock);
  282. cnext = c->orph_cnext;
  283. for (i = 0; i < cnt; i++) {
  284. orphan = cnext;
  285. orph->inos[i] = cpu_to_le64(orphan->inum);
  286. cnext = orphan->cnext;
  287. orphan->cnext = NULL;
  288. }
  289. c->orph_cnext = cnext;
  290. c->cmt_orphans -= cnt;
  291. spin_unlock(&c->orphan_lock);
  292. if (c->cmt_orphans)
  293. orph->cmt_no = cpu_to_le64(c->cmt_no);
  294. else
  295. /* Mark the last node of the commit */
  296. orph->cmt_no = cpu_to_le64((c->cmt_no) | (1ULL << 63));
  297. ubifs_assert(c->ohead_offs + len <= c->leb_size);
  298. ubifs_assert(c->ohead_lnum >= c->orph_first);
  299. ubifs_assert(c->ohead_lnum <= c->orph_last);
  300. err = do_write_orph_node(c, len, atomic);
  301. c->ohead_offs += ALIGN(len, c->min_io_size);
  302. c->ohead_offs = ALIGN(c->ohead_offs, 8);
  303. return err;
  304. }
  305. /**
  306. * write_orph_nodes - write orphan nodes until there are no more to commit.
  307. * @c: UBIFS file-system description object
  308. * @atomic: write atomically
  309. *
  310. * This function writes orphan nodes for all the orphans to commit. On success,
  311. * %0 is returned, otherwise a negative error code is returned.
  312. */
  313. static int write_orph_nodes(struct ubifs_info *c, int atomic)
  314. {
  315. int err;
  316. while (c->cmt_orphans > 0) {
  317. err = write_orph_node(c, atomic);
  318. if (err)
  319. return err;
  320. }
  321. if (atomic) {
  322. int lnum;
  323. /* Unmap any unused LEBs after consolidation */
  324. lnum = c->ohead_lnum + 1;
  325. for (lnum = c->ohead_lnum + 1; lnum <= c->orph_last; lnum++) {
  326. err = ubifs_leb_unmap(c, lnum);
  327. if (err)
  328. return err;
  329. }
  330. }
  331. return 0;
  332. }
  333. /**
  334. * consolidate - consolidate the orphan area.
  335. * @c: UBIFS file-system description object
  336. *
  337. * This function enables consolidation by putting all the orphans into the list
  338. * to commit. The list is in the order that the orphans were added, and the
  339. * LEBs are written atomically in order, so at no time can orphans be lost by
  340. * an unclean unmount.
  341. *
  342. * This function returns %0 on success and a negative error code on failure.
  343. */
  344. static int consolidate(struct ubifs_info *c)
  345. {
  346. int tot_avail = tot_avail_orphs(c), err = 0;
  347. spin_lock(&c->orphan_lock);
  348. dbg_cmt("there is space for %d orphans and there are %d",
  349. tot_avail, c->tot_orphans);
  350. if (c->tot_orphans - c->new_orphans <= tot_avail) {
  351. struct ubifs_orphan *orphan, **last;
  352. int cnt = 0;
  353. /* Change the cnext list to include all non-new orphans */
  354. last = &c->orph_cnext;
  355. list_for_each_entry(orphan, &c->orph_list, list) {
  356. if (orphan->new)
  357. continue;
  358. *last = orphan;
  359. last = &orphan->cnext;
  360. cnt += 1;
  361. }
  362. *last = orphan->cnext;
  363. ubifs_assert(cnt == c->tot_orphans - c->new_orphans);
  364. c->cmt_orphans = cnt;
  365. c->ohead_lnum = c->orph_first;
  366. c->ohead_offs = 0;
  367. } else {
  368. /*
  369. * We limit the number of orphans so that this should
  370. * never happen.
  371. */
  372. ubifs_err("out of space in orphan area");
  373. err = -EINVAL;
  374. }
  375. spin_unlock(&c->orphan_lock);
  376. return err;
  377. }
  378. /**
  379. * commit_orphans - commit orphans.
  380. * @c: UBIFS file-system description object
  381. *
  382. * This function commits orphans to flash. On success, %0 is returned,
  383. * otherwise a negative error code is returned.
  384. */
  385. static int commit_orphans(struct ubifs_info *c)
  386. {
  387. int avail, atomic = 0, err;
  388. ubifs_assert(c->cmt_orphans > 0);
  389. avail = avail_orphs(c);
  390. if (avail < c->cmt_orphans) {
  391. /* Not enough space to write new orphans, so consolidate */
  392. err = consolidate(c);
  393. if (err)
  394. return err;
  395. atomic = 1;
  396. }
  397. err = write_orph_nodes(c, atomic);
  398. return err;
  399. }
  400. /**
  401. * erase_deleted - erase the orphans marked for deletion.
  402. * @c: UBIFS file-system description object
  403. *
  404. * During commit, the orphans being committed cannot be deleted, so they are
  405. * marked for deletion and deleted by this function. Also, the recovery
  406. * adds killed orphans to the deletion list, and therefore they are deleted
  407. * here too.
  408. */
  409. static void erase_deleted(struct ubifs_info *c)
  410. {
  411. struct ubifs_orphan *orphan, *dnext;
  412. spin_lock(&c->orphan_lock);
  413. dnext = c->orph_dnext;
  414. while (dnext) {
  415. orphan = dnext;
  416. dnext = orphan->dnext;
  417. ubifs_assert(!orphan->new);
  418. rb_erase(&orphan->rb, &c->orph_tree);
  419. list_del(&orphan->list);
  420. c->tot_orphans -= 1;
  421. dbg_gen("deleting orphan ino %lu", (unsigned long)orphan->inum);
  422. kfree(orphan);
  423. }
  424. c->orph_dnext = NULL;
  425. spin_unlock(&c->orphan_lock);
  426. }
  427. /**
  428. * ubifs_orphan_end_commit - end commit of orphans.
  429. * @c: UBIFS file-system description object
  430. *
  431. * End commit of orphans.
  432. */
  433. int ubifs_orphan_end_commit(struct ubifs_info *c)
  434. {
  435. int err;
  436. if (c->cmt_orphans != 0) {
  437. err = commit_orphans(c);
  438. if (err)
  439. return err;
  440. }
  441. erase_deleted(c);
  442. err = dbg_check_orphans(c);
  443. return err;
  444. }
  445. /**
  446. * ubifs_clear_orphans - erase all LEBs used for orphans.
  447. * @c: UBIFS file-system description object
  448. *
  449. * If recovery is not required, then the orphans from the previous session
  450. * are not needed. This function locates the LEBs used to record
  451. * orphans, and un-maps them.
  452. */
  453. int ubifs_clear_orphans(struct ubifs_info *c)
  454. {
  455. int lnum, err;
  456. for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
  457. err = ubifs_leb_unmap(c, lnum);
  458. if (err)
  459. return err;
  460. }
  461. c->ohead_lnum = c->orph_first;
  462. c->ohead_offs = 0;
  463. return 0;
  464. }
  465. /**
  466. * insert_dead_orphan - insert an orphan.
  467. * @c: UBIFS file-system description object
  468. * @inum: orphan inode number
  469. *
  470. * This function is a helper to the 'do_kill_orphans()' function. The orphan
  471. * must be kept until the next commit, so it is added to the rb-tree and the
  472. * deletion list.
  473. */
  474. static int insert_dead_orphan(struct ubifs_info *c, ino_t inum)
  475. {
  476. struct ubifs_orphan *orphan, *o;
  477. struct rb_node **p, *parent = NULL;
  478. orphan = kzalloc(sizeof(struct ubifs_orphan), GFP_KERNEL);
  479. if (!orphan)
  480. return -ENOMEM;
  481. orphan->inum = inum;
  482. p = &c->orph_tree.rb_node;
  483. while (*p) {
  484. parent = *p;
  485. o = rb_entry(parent, struct ubifs_orphan, rb);
  486. if (inum < o->inum)
  487. p = &(*p)->rb_left;
  488. else if (inum > o->inum)
  489. p = &(*p)->rb_right;
  490. else {
  491. /* Already added - no problem */
  492. kfree(orphan);
  493. return 0;
  494. }
  495. }
  496. c->tot_orphans += 1;
  497. rb_link_node(&orphan->rb, parent, p);
  498. rb_insert_color(&orphan->rb, &c->orph_tree);
  499. list_add_tail(&orphan->list, &c->orph_list);
  500. orphan->dnext = c->orph_dnext;
  501. c->orph_dnext = orphan;
  502. dbg_mnt("ino %lu, new %d, tot %d", (unsigned long)inum,
  503. c->new_orphans, c->tot_orphans);
  504. return 0;
  505. }
  506. /**
  507. * do_kill_orphans - remove orphan inodes from the index.
  508. * @c: UBIFS file-system description object
  509. * @sleb: scanned LEB
  510. * @last_cmt_no: cmt_no of last orphan node read is passed and returned here
  511. * @outofdate: whether the LEB is out of date is returned here
  512. * @last_flagged: whether the end orphan node is encountered
  513. *
  514. * This function is a helper to the 'kill_orphans()' function. It goes through
  515. * every orphan node in a LEB and for every inode number recorded, removes
  516. * all keys for that inode from the TNC.
  517. */
  518. static int do_kill_orphans(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
  519. unsigned long long *last_cmt_no, int *outofdate,
  520. int *last_flagged)
  521. {
  522. struct ubifs_scan_node *snod;
  523. struct ubifs_orph_node *orph;
  524. unsigned long long cmt_no;
  525. ino_t inum;
  526. int i, n, err, first = 1;
  527. list_for_each_entry(snod, &sleb->nodes, list) {
  528. if (snod->type != UBIFS_ORPH_NODE) {
  529. ubifs_err("invalid node type %d in orphan area at "
  530. "%d:%d", snod->type, sleb->lnum, snod->offs);
  531. ubifs_dump_node(c, snod->node);
  532. return -EINVAL;
  533. }
  534. orph = snod->node;
  535. /* Check commit number */
  536. cmt_no = le64_to_cpu(orph->cmt_no) & LLONG_MAX;
  537. /*
  538. * The commit number on the master node may be less, because
  539. * of a failed commit. If there are several failed commits in a
  540. * row, the commit number written on orphan nodes will continue
  541. * to increase (because the commit number is adjusted here) even
  542. * though the commit number on the master node stays the same
  543. * because the master node has not been re-written.
  544. */
  545. if (cmt_no > c->cmt_no)
  546. c->cmt_no = cmt_no;
  547. if (cmt_no < *last_cmt_no && *last_flagged) {
  548. /*
  549. * The last orphan node had a higher commit number and
  550. * was flagged as the last written for that commit
  551. * number. That makes this orphan node, out of date.
  552. */
  553. if (!first) {
  554. ubifs_err("out of order commit number %llu in "
  555. "orphan node at %d:%d",
  556. cmt_no, sleb->lnum, snod->offs);
  557. ubifs_dump_node(c, snod->node);
  558. return -EINVAL;
  559. }
  560. dbg_rcvry("out of date LEB %d", sleb->lnum);
  561. *outofdate = 1;
  562. return 0;
  563. }
  564. if (first)
  565. first = 0;
  566. n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
  567. for (i = 0; i < n; i++) {
  568. inum = le64_to_cpu(orph->inos[i]);
  569. dbg_rcvry("deleting orphaned inode %lu",
  570. (unsigned long)inum);
  571. err = ubifs_tnc_remove_ino(c, inum);
  572. if (err)
  573. return err;
  574. err = insert_dead_orphan(c, inum);
  575. if (err)
  576. return err;
  577. }
  578. *last_cmt_no = cmt_no;
  579. if (le64_to_cpu(orph->cmt_no) & (1ULL << 63)) {
  580. dbg_rcvry("last orph node for commit %llu at %d:%d",
  581. cmt_no, sleb->lnum, snod->offs);
  582. *last_flagged = 1;
  583. } else
  584. *last_flagged = 0;
  585. }
  586. return 0;
  587. }
  588. /**
  589. * kill_orphans - remove all orphan inodes from the index.
  590. * @c: UBIFS file-system description object
  591. *
  592. * If recovery is required, then orphan inodes recorded during the previous
  593. * session (which ended with an unclean unmount) must be deleted from the index.
  594. * This is done by updating the TNC, but since the index is not updated until
  595. * the next commit, the LEBs where the orphan information is recorded are not
  596. * erased until the next commit.
  597. */
  598. static int kill_orphans(struct ubifs_info *c)
  599. {
  600. unsigned long long last_cmt_no = 0;
  601. int lnum, err = 0, outofdate = 0, last_flagged = 0;
  602. c->ohead_lnum = c->orph_first;
  603. c->ohead_offs = 0;
  604. /* Check no-orphans flag and skip this if no orphans */
  605. if (c->no_orphs) {
  606. dbg_rcvry("no orphans");
  607. return 0;
  608. }
  609. /*
  610. * Orph nodes always start at c->orph_first and are written to each
  611. * successive LEB in turn. Generally unused LEBs will have been unmapped
  612. * but may contain out of date orphan nodes if the unmap didn't go
  613. * through. In addition, the last orphan node written for each commit is
  614. * marked (top bit of orph->cmt_no is set to 1). It is possible that
  615. * there are orphan nodes from the next commit (i.e. the commit did not
  616. * complete successfully). In that case, no orphans will have been lost
  617. * due to the way that orphans are written, and any orphans added will
  618. * be valid orphans anyway and so can be deleted.
  619. */
  620. for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
  621. struct ubifs_scan_leb *sleb;
  622. dbg_rcvry("LEB %d", lnum);
  623. sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
  624. if (IS_ERR(sleb)) {
  625. if (PTR_ERR(sleb) == -EUCLEAN)
  626. sleb = ubifs_recover_leb(c, lnum, 0,
  627. c->sbuf, -1);
  628. if (IS_ERR(sleb)) {
  629. err = PTR_ERR(sleb);
  630. break;
  631. }
  632. }
  633. err = do_kill_orphans(c, sleb, &last_cmt_no, &outofdate,
  634. &last_flagged);
  635. if (err || outofdate) {
  636. ubifs_scan_destroy(sleb);
  637. break;
  638. }
  639. if (sleb->endpt) {
  640. c->ohead_lnum = lnum;
  641. c->ohead_offs = sleb->endpt;
  642. }
  643. ubifs_scan_destroy(sleb);
  644. }
  645. return err;
  646. }
  647. /**
  648. * ubifs_mount_orphans - delete orphan inodes and erase LEBs that recorded them.
  649. * @c: UBIFS file-system description object
  650. * @unclean: indicates recovery from unclean unmount
  651. * @read_only: indicates read only mount
  652. *
  653. * This function is called when mounting to erase orphans from the previous
  654. * session. If UBIFS was not unmounted cleanly, then the inodes recorded as
  655. * orphans are deleted.
  656. */
  657. int ubifs_mount_orphans(struct ubifs_info *c, int unclean, int read_only)
  658. {
  659. int err = 0;
  660. c->max_orphans = tot_avail_orphs(c);
  661. if (!read_only) {
  662. c->orph_buf = vmalloc(c->leb_size);
  663. if (!c->orph_buf)
  664. return -ENOMEM;
  665. }
  666. if (unclean)
  667. err = kill_orphans(c);
  668. else if (!read_only)
  669. err = ubifs_clear_orphans(c);
  670. return err;
  671. }
  672. /*
  673. * Everything below is related to debugging.
  674. */
  675. struct check_orphan {
  676. struct rb_node rb;
  677. ino_t inum;
  678. };
  679. struct check_info {
  680. unsigned long last_ino;
  681. unsigned long tot_inos;
  682. unsigned long missing;
  683. unsigned long long leaf_cnt;
  684. struct ubifs_ino_node *node;
  685. struct rb_root root;
  686. };
  687. static int dbg_find_orphan(struct ubifs_info *c, ino_t inum)
  688. {
  689. struct ubifs_orphan *o;
  690. struct rb_node *p;
  691. spin_lock(&c->orphan_lock);
  692. p = c->orph_tree.rb_node;
  693. while (p) {
  694. o = rb_entry(p, struct ubifs_orphan, rb);
  695. if (inum < o->inum)
  696. p = p->rb_left;
  697. else if (inum > o->inum)
  698. p = p->rb_right;
  699. else {
  700. spin_unlock(&c->orphan_lock);
  701. return 1;
  702. }
  703. }
  704. spin_unlock(&c->orphan_lock);
  705. return 0;
  706. }
  707. static int dbg_ins_check_orphan(struct rb_root *root, ino_t inum)
  708. {
  709. struct check_orphan *orphan, *o;
  710. struct rb_node **p, *parent = NULL;
  711. orphan = kzalloc(sizeof(struct check_orphan), GFP_NOFS);
  712. if (!orphan)
  713. return -ENOMEM;
  714. orphan->inum = inum;
  715. p = &root->rb_node;
  716. while (*p) {
  717. parent = *p;
  718. o = rb_entry(parent, struct check_orphan, rb);
  719. if (inum < o->inum)
  720. p = &(*p)->rb_left;
  721. else if (inum > o->inum)
  722. p = &(*p)->rb_right;
  723. else {
  724. kfree(orphan);
  725. return 0;
  726. }
  727. }
  728. rb_link_node(&orphan->rb, parent, p);
  729. rb_insert_color(&orphan->rb, root);
  730. return 0;
  731. }
  732. static int dbg_find_check_orphan(struct rb_root *root, ino_t inum)
  733. {
  734. struct check_orphan *o;
  735. struct rb_node *p;
  736. p = root->rb_node;
  737. while (p) {
  738. o = rb_entry(p, struct check_orphan, rb);
  739. if (inum < o->inum)
  740. p = p->rb_left;
  741. else if (inum > o->inum)
  742. p = p->rb_right;
  743. else
  744. return 1;
  745. }
  746. return 0;
  747. }
  748. static void dbg_free_check_tree(struct rb_root *root)
  749. {
  750. struct rb_node *this = root->rb_node;
  751. struct check_orphan *o;
  752. while (this) {
  753. if (this->rb_left) {
  754. this = this->rb_left;
  755. continue;
  756. } else if (this->rb_right) {
  757. this = this->rb_right;
  758. continue;
  759. }
  760. o = rb_entry(this, struct check_orphan, rb);
  761. this = rb_parent(this);
  762. if (this) {
  763. if (this->rb_left == &o->rb)
  764. this->rb_left = NULL;
  765. else
  766. this->rb_right = NULL;
  767. }
  768. kfree(o);
  769. }
  770. }
  771. static int dbg_orphan_check(struct ubifs_info *c, struct ubifs_zbranch *zbr,
  772. void *priv)
  773. {
  774. struct check_info *ci = priv;
  775. ino_t inum;
  776. int err;
  777. inum = key_inum(c, &zbr->key);
  778. if (inum != ci->last_ino) {
  779. /* Lowest node type is the inode node, so it comes first */
  780. if (key_type(c, &zbr->key) != UBIFS_INO_KEY)
  781. ubifs_err("found orphan node ino %lu, type %d",
  782. (unsigned long)inum, key_type(c, &zbr->key));
  783. ci->last_ino = inum;
  784. ci->tot_inos += 1;
  785. err = ubifs_tnc_read_node(c, zbr, ci->node);
  786. if (err) {
  787. ubifs_err("node read failed, error %d", err);
  788. return err;
  789. }
  790. if (ci->node->nlink == 0)
  791. /* Must be recorded as an orphan */
  792. if (!dbg_find_check_orphan(&ci->root, inum) &&
  793. !dbg_find_orphan(c, inum)) {
  794. ubifs_err("missing orphan, ino %lu",
  795. (unsigned long)inum);
  796. ci->missing += 1;
  797. }
  798. }
  799. ci->leaf_cnt += 1;
  800. return 0;
  801. }
  802. static int dbg_read_orphans(struct check_info *ci, struct ubifs_scan_leb *sleb)
  803. {
  804. struct ubifs_scan_node *snod;
  805. struct ubifs_orph_node *orph;
  806. ino_t inum;
  807. int i, n, err;
  808. list_for_each_entry(snod, &sleb->nodes, list) {
  809. cond_resched();
  810. if (snod->type != UBIFS_ORPH_NODE)
  811. continue;
  812. orph = snod->node;
  813. n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
  814. for (i = 0; i < n; i++) {
  815. inum = le64_to_cpu(orph->inos[i]);
  816. err = dbg_ins_check_orphan(&ci->root, inum);
  817. if (err)
  818. return err;
  819. }
  820. }
  821. return 0;
  822. }
  823. static int dbg_scan_orphans(struct ubifs_info *c, struct check_info *ci)
  824. {
  825. int lnum, err = 0;
  826. void *buf;
  827. /* Check no-orphans flag and skip this if no orphans */
  828. if (c->no_orphs)
  829. return 0;
  830. buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
  831. if (!buf) {
  832. ubifs_err("cannot allocate memory to check orphans");
  833. return 0;
  834. }
  835. for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
  836. struct ubifs_scan_leb *sleb;
  837. sleb = ubifs_scan(c, lnum, 0, buf, 0);
  838. if (IS_ERR(sleb)) {
  839. err = PTR_ERR(sleb);
  840. break;
  841. }
  842. err = dbg_read_orphans(ci, sleb);
  843. ubifs_scan_destroy(sleb);
  844. if (err)
  845. break;
  846. }
  847. vfree(buf);
  848. return err;
  849. }
  850. static int dbg_check_orphans(struct ubifs_info *c)
  851. {
  852. struct check_info ci;
  853. int err;
  854. if (!dbg_is_chk_orph(c))
  855. return 0;
  856. ci.last_ino = 0;
  857. ci.tot_inos = 0;
  858. ci.missing = 0;
  859. ci.leaf_cnt = 0;
  860. ci.root = RB_ROOT;
  861. ci.node = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
  862. if (!ci.node) {
  863. ubifs_err("out of memory");
  864. return -ENOMEM;
  865. }
  866. err = dbg_scan_orphans(c, &ci);
  867. if (err)
  868. goto out;
  869. err = dbg_walk_index(c, &dbg_orphan_check, NULL, &ci);
  870. if (err) {
  871. ubifs_err("cannot scan TNC, error %d", err);
  872. goto out;
  873. }
  874. if (ci.missing) {
  875. ubifs_err("%lu missing orphan(s)", ci.missing);
  876. err = -EINVAL;
  877. goto out;
  878. }
  879. dbg_cmt("last inode number is %lu", ci.last_ino);
  880. dbg_cmt("total number of inodes is %lu", ci.tot_inos);
  881. dbg_cmt("total number of leaf nodes is %llu", ci.leaf_cnt);
  882. out:
  883. dbg_free_check_tree(&ci.root);
  884. kfree(ci.node);
  885. return err;
  886. }