orphan.c 25 KB

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