orphan.c 25 KB

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