orphan.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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("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("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("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. 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. orphan->cmt = 1;
  363. *last = orphan;
  364. last = &orphan->cnext;
  365. cnt += 1;
  366. }
  367. *last = NULL;
  368. ubifs_assert(cnt == c->tot_orphans - c->new_orphans);
  369. c->cmt_orphans = cnt;
  370. c->ohead_lnum = c->orph_first;
  371. c->ohead_offs = 0;
  372. } else {
  373. /*
  374. * We limit the number of orphans so that this should
  375. * never happen.
  376. */
  377. ubifs_err("out of space in orphan area");
  378. err = -EINVAL;
  379. }
  380. spin_unlock(&c->orphan_lock);
  381. return err;
  382. }
  383. /**
  384. * commit_orphans - commit orphans.
  385. * @c: UBIFS file-system description object
  386. *
  387. * This function commits orphans to flash. On success, %0 is returned,
  388. * otherwise a negative error code is returned.
  389. */
  390. static int commit_orphans(struct ubifs_info *c)
  391. {
  392. int avail, atomic = 0, err;
  393. ubifs_assert(c->cmt_orphans > 0);
  394. avail = avail_orphs(c);
  395. if (avail < c->cmt_orphans) {
  396. /* Not enough space to write new orphans, so consolidate */
  397. err = consolidate(c);
  398. if (err)
  399. return err;
  400. atomic = 1;
  401. }
  402. err = write_orph_nodes(c, atomic);
  403. return err;
  404. }
  405. /**
  406. * erase_deleted - erase the orphans marked for deletion.
  407. * @c: UBIFS file-system description object
  408. *
  409. * During commit, the orphans being committed cannot be deleted, so they are
  410. * marked for deletion and deleted by this function. Also, the recovery
  411. * adds killed orphans to the deletion list, and therefore they are deleted
  412. * here too.
  413. */
  414. static void erase_deleted(struct ubifs_info *c)
  415. {
  416. struct ubifs_orphan *orphan, *dnext;
  417. spin_lock(&c->orphan_lock);
  418. dnext = c->orph_dnext;
  419. while (dnext) {
  420. orphan = dnext;
  421. dnext = orphan->dnext;
  422. ubifs_assert(!orphan->new);
  423. ubifs_assert(orphan->del);
  424. rb_erase(&orphan->rb, &c->orph_tree);
  425. list_del(&orphan->list);
  426. c->tot_orphans -= 1;
  427. dbg_gen("deleting orphan ino %lu", (unsigned long)orphan->inum);
  428. kfree(orphan);
  429. }
  430. c->orph_dnext = NULL;
  431. spin_unlock(&c->orphan_lock);
  432. }
  433. /**
  434. * ubifs_orphan_end_commit - end commit of orphans.
  435. * @c: UBIFS file-system description object
  436. *
  437. * End commit of orphans.
  438. */
  439. int ubifs_orphan_end_commit(struct ubifs_info *c)
  440. {
  441. int err;
  442. if (c->cmt_orphans != 0) {
  443. err = commit_orphans(c);
  444. if (err)
  445. return err;
  446. }
  447. erase_deleted(c);
  448. err = dbg_check_orphans(c);
  449. return err;
  450. }
  451. /**
  452. * ubifs_clear_orphans - erase all LEBs used for orphans.
  453. * @c: UBIFS file-system description object
  454. *
  455. * If recovery is not required, then the orphans from the previous session
  456. * are not needed. This function locates the LEBs used to record
  457. * orphans, and un-maps them.
  458. */
  459. int ubifs_clear_orphans(struct ubifs_info *c)
  460. {
  461. int lnum, err;
  462. for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
  463. err = ubifs_leb_unmap(c, lnum);
  464. if (err)
  465. return err;
  466. }
  467. c->ohead_lnum = c->orph_first;
  468. c->ohead_offs = 0;
  469. return 0;
  470. }
  471. /**
  472. * insert_dead_orphan - insert an orphan.
  473. * @c: UBIFS file-system description object
  474. * @inum: orphan inode number
  475. *
  476. * This function is a helper to the 'do_kill_orphans()' function. The orphan
  477. * must be kept until the next commit, so it is added to the rb-tree and the
  478. * deletion list.
  479. */
  480. static int insert_dead_orphan(struct ubifs_info *c, ino_t inum)
  481. {
  482. struct ubifs_orphan *orphan, *o;
  483. struct rb_node **p, *parent = NULL;
  484. orphan = kzalloc(sizeof(struct ubifs_orphan), GFP_KERNEL);
  485. if (!orphan)
  486. return -ENOMEM;
  487. orphan->inum = inum;
  488. p = &c->orph_tree.rb_node;
  489. while (*p) {
  490. parent = *p;
  491. o = rb_entry(parent, struct ubifs_orphan, rb);
  492. if (inum < o->inum)
  493. p = &(*p)->rb_left;
  494. else if (inum > o->inum)
  495. p = &(*p)->rb_right;
  496. else {
  497. /* Already added - no problem */
  498. kfree(orphan);
  499. return 0;
  500. }
  501. }
  502. c->tot_orphans += 1;
  503. rb_link_node(&orphan->rb, parent, p);
  504. rb_insert_color(&orphan->rb, &c->orph_tree);
  505. list_add_tail(&orphan->list, &c->orph_list);
  506. orphan->del = 1;
  507. orphan->dnext = c->orph_dnext;
  508. c->orph_dnext = orphan;
  509. dbg_mnt("ino %lu, new %d, tot %d", (unsigned long)inum,
  510. c->new_orphans, c->tot_orphans);
  511. return 0;
  512. }
  513. /**
  514. * do_kill_orphans - remove orphan inodes from the index.
  515. * @c: UBIFS file-system description object
  516. * @sleb: scanned LEB
  517. * @last_cmt_no: cmt_no of last orphan node read is passed and returned here
  518. * @outofdate: whether the LEB is out of date is returned here
  519. * @last_flagged: whether the end orphan node is encountered
  520. *
  521. * This function is a helper to the 'kill_orphans()' function. It goes through
  522. * every orphan node in a LEB and for every inode number recorded, removes
  523. * all keys for that inode from the TNC.
  524. */
  525. static int do_kill_orphans(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
  526. unsigned long long *last_cmt_no, int *outofdate,
  527. int *last_flagged)
  528. {
  529. struct ubifs_scan_node *snod;
  530. struct ubifs_orph_node *orph;
  531. unsigned long long cmt_no;
  532. ino_t inum;
  533. int i, n, err, first = 1;
  534. list_for_each_entry(snod, &sleb->nodes, list) {
  535. if (snod->type != UBIFS_ORPH_NODE) {
  536. ubifs_err("invalid node type %d in orphan area at %d:%d",
  537. snod->type, sleb->lnum, snod->offs);
  538. ubifs_dump_node(c, snod->node);
  539. return -EINVAL;
  540. }
  541. orph = snod->node;
  542. /* Check commit number */
  543. cmt_no = le64_to_cpu(orph->cmt_no) & LLONG_MAX;
  544. /*
  545. * The commit number on the master node may be less, because
  546. * of a failed commit. If there are several failed commits in a
  547. * row, the commit number written on orphan nodes will continue
  548. * to increase (because the commit number is adjusted here) even
  549. * though the commit number on the master node stays the same
  550. * because the master node has not been re-written.
  551. */
  552. if (cmt_no > c->cmt_no)
  553. c->cmt_no = cmt_no;
  554. if (cmt_no < *last_cmt_no && *last_flagged) {
  555. /*
  556. * The last orphan node had a higher commit number and
  557. * was flagged as the last written for that commit
  558. * number. That makes this orphan node, out of date.
  559. */
  560. if (!first) {
  561. ubifs_err("out of order commit number %llu in orphan node at %d:%d",
  562. cmt_no, sleb->lnum, snod->offs);
  563. ubifs_dump_node(c, snod->node);
  564. return -EINVAL;
  565. }
  566. dbg_rcvry("out of date LEB %d", sleb->lnum);
  567. *outofdate = 1;
  568. return 0;
  569. }
  570. if (first)
  571. first = 0;
  572. n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
  573. for (i = 0; i < n; i++) {
  574. inum = le64_to_cpu(orph->inos[i]);
  575. dbg_rcvry("deleting orphaned inode %lu",
  576. (unsigned long)inum);
  577. err = ubifs_tnc_remove_ino(c, inum);
  578. if (err)
  579. return err;
  580. err = insert_dead_orphan(c, inum);
  581. if (err)
  582. return err;
  583. }
  584. *last_cmt_no = cmt_no;
  585. if (le64_to_cpu(orph->cmt_no) & (1ULL << 63)) {
  586. dbg_rcvry("last orph node for commit %llu at %d:%d",
  587. cmt_no, sleb->lnum, snod->offs);
  588. *last_flagged = 1;
  589. } else
  590. *last_flagged = 0;
  591. }
  592. return 0;
  593. }
  594. /**
  595. * kill_orphans - remove all orphan inodes from the index.
  596. * @c: UBIFS file-system description object
  597. *
  598. * If recovery is required, then orphan inodes recorded during the previous
  599. * session (which ended with an unclean unmount) must be deleted from the index.
  600. * This is done by updating the TNC, but since the index is not updated until
  601. * the next commit, the LEBs where the orphan information is recorded are not
  602. * erased until the next commit.
  603. */
  604. static int kill_orphans(struct ubifs_info *c)
  605. {
  606. unsigned long long last_cmt_no = 0;
  607. int lnum, err = 0, outofdate = 0, last_flagged = 0;
  608. c->ohead_lnum = c->orph_first;
  609. c->ohead_offs = 0;
  610. /* Check no-orphans flag and skip this if no orphans */
  611. if (c->no_orphs) {
  612. dbg_rcvry("no orphans");
  613. return 0;
  614. }
  615. /*
  616. * Orph nodes always start at c->orph_first and are written to each
  617. * successive LEB in turn. Generally unused LEBs will have been unmapped
  618. * but may contain out of date orphan nodes if the unmap didn't go
  619. * through. In addition, the last orphan node written for each commit is
  620. * marked (top bit of orph->cmt_no is set to 1). It is possible that
  621. * there are orphan nodes from the next commit (i.e. the commit did not
  622. * complete successfully). In that case, no orphans will have been lost
  623. * due to the way that orphans are written, and any orphans added will
  624. * be valid orphans anyway and so can be deleted.
  625. */
  626. for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
  627. struct ubifs_scan_leb *sleb;
  628. dbg_rcvry("LEB %d", lnum);
  629. sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
  630. if (IS_ERR(sleb)) {
  631. if (PTR_ERR(sleb) == -EUCLEAN)
  632. sleb = ubifs_recover_leb(c, lnum, 0,
  633. c->sbuf, -1);
  634. if (IS_ERR(sleb)) {
  635. err = PTR_ERR(sleb);
  636. break;
  637. }
  638. }
  639. err = do_kill_orphans(c, sleb, &last_cmt_no, &outofdate,
  640. &last_flagged);
  641. if (err || outofdate) {
  642. ubifs_scan_destroy(sleb);
  643. break;
  644. }
  645. if (sleb->endpt) {
  646. c->ohead_lnum = lnum;
  647. c->ohead_offs = sleb->endpt;
  648. }
  649. ubifs_scan_destroy(sleb);
  650. }
  651. return err;
  652. }
  653. /**
  654. * ubifs_mount_orphans - delete orphan inodes and erase LEBs that recorded them.
  655. * @c: UBIFS file-system description object
  656. * @unclean: indicates recovery from unclean unmount
  657. * @read_only: indicates read only mount
  658. *
  659. * This function is called when mounting to erase orphans from the previous
  660. * session. If UBIFS was not unmounted cleanly, then the inodes recorded as
  661. * orphans are deleted.
  662. */
  663. int ubifs_mount_orphans(struct ubifs_info *c, int unclean, int read_only)
  664. {
  665. int err = 0;
  666. c->max_orphans = tot_avail_orphs(c);
  667. if (!read_only) {
  668. c->orph_buf = vmalloc(c->leb_size);
  669. if (!c->orph_buf)
  670. return -ENOMEM;
  671. }
  672. if (unclean)
  673. err = kill_orphans(c);
  674. else if (!read_only)
  675. err = ubifs_clear_orphans(c);
  676. return err;
  677. }
  678. /*
  679. * Everything below is related to debugging.
  680. */
  681. struct check_orphan {
  682. struct rb_node rb;
  683. ino_t inum;
  684. };
  685. struct check_info {
  686. unsigned long last_ino;
  687. unsigned long tot_inos;
  688. unsigned long missing;
  689. unsigned long long leaf_cnt;
  690. struct ubifs_ino_node *node;
  691. struct rb_root root;
  692. };
  693. static int dbg_find_orphan(struct ubifs_info *c, ino_t inum)
  694. {
  695. struct ubifs_orphan *o;
  696. struct rb_node *p;
  697. spin_lock(&c->orphan_lock);
  698. p = c->orph_tree.rb_node;
  699. while (p) {
  700. o = rb_entry(p, struct ubifs_orphan, rb);
  701. if (inum < o->inum)
  702. p = p->rb_left;
  703. else if (inum > o->inum)
  704. p = p->rb_right;
  705. else {
  706. spin_unlock(&c->orphan_lock);
  707. return 1;
  708. }
  709. }
  710. spin_unlock(&c->orphan_lock);
  711. return 0;
  712. }
  713. static int dbg_ins_check_orphan(struct rb_root *root, ino_t inum)
  714. {
  715. struct check_orphan *orphan, *o;
  716. struct rb_node **p, *parent = NULL;
  717. orphan = kzalloc(sizeof(struct check_orphan), GFP_NOFS);
  718. if (!orphan)
  719. return -ENOMEM;
  720. orphan->inum = inum;
  721. p = &root->rb_node;
  722. while (*p) {
  723. parent = *p;
  724. o = rb_entry(parent, struct check_orphan, rb);
  725. if (inum < o->inum)
  726. p = &(*p)->rb_left;
  727. else if (inum > o->inum)
  728. p = &(*p)->rb_right;
  729. else {
  730. kfree(orphan);
  731. return 0;
  732. }
  733. }
  734. rb_link_node(&orphan->rb, parent, p);
  735. rb_insert_color(&orphan->rb, root);
  736. return 0;
  737. }
  738. static int dbg_find_check_orphan(struct rb_root *root, ino_t inum)
  739. {
  740. struct check_orphan *o;
  741. struct rb_node *p;
  742. p = root->rb_node;
  743. while (p) {
  744. o = rb_entry(p, struct check_orphan, rb);
  745. if (inum < o->inum)
  746. p = p->rb_left;
  747. else if (inum > o->inum)
  748. p = p->rb_right;
  749. else
  750. return 1;
  751. }
  752. return 0;
  753. }
  754. static void dbg_free_check_tree(struct rb_root *root)
  755. {
  756. struct rb_node *this = root->rb_node;
  757. struct check_orphan *o;
  758. while (this) {
  759. if (this->rb_left) {
  760. this = this->rb_left;
  761. continue;
  762. } else if (this->rb_right) {
  763. this = this->rb_right;
  764. continue;
  765. }
  766. o = rb_entry(this, struct check_orphan, rb);
  767. this = rb_parent(this);
  768. if (this) {
  769. if (this->rb_left == &o->rb)
  770. this->rb_left = NULL;
  771. else
  772. this->rb_right = NULL;
  773. }
  774. kfree(o);
  775. }
  776. }
  777. static int dbg_orphan_check(struct ubifs_info *c, struct ubifs_zbranch *zbr,
  778. void *priv)
  779. {
  780. struct check_info *ci = priv;
  781. ino_t inum;
  782. int err;
  783. inum = key_inum(c, &zbr->key);
  784. if (inum != ci->last_ino) {
  785. /* Lowest node type is the inode node, so it comes first */
  786. if (key_type(c, &zbr->key) != UBIFS_INO_KEY)
  787. ubifs_err("found orphan node ino %lu, type %d",
  788. (unsigned long)inum, key_type(c, &zbr->key));
  789. ci->last_ino = inum;
  790. ci->tot_inos += 1;
  791. err = ubifs_tnc_read_node(c, zbr, ci->node);
  792. if (err) {
  793. ubifs_err("node read failed, error %d", err);
  794. return err;
  795. }
  796. if (ci->node->nlink == 0)
  797. /* Must be recorded as an orphan */
  798. if (!dbg_find_check_orphan(&ci->root, inum) &&
  799. !dbg_find_orphan(c, inum)) {
  800. ubifs_err("missing orphan, ino %lu",
  801. (unsigned long)inum);
  802. ci->missing += 1;
  803. }
  804. }
  805. ci->leaf_cnt += 1;
  806. return 0;
  807. }
  808. static int dbg_read_orphans(struct check_info *ci, struct ubifs_scan_leb *sleb)
  809. {
  810. struct ubifs_scan_node *snod;
  811. struct ubifs_orph_node *orph;
  812. ino_t inum;
  813. int i, n, err;
  814. list_for_each_entry(snod, &sleb->nodes, list) {
  815. cond_resched();
  816. if (snod->type != UBIFS_ORPH_NODE)
  817. continue;
  818. orph = snod->node;
  819. n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
  820. for (i = 0; i < n; i++) {
  821. inum = le64_to_cpu(orph->inos[i]);
  822. err = dbg_ins_check_orphan(&ci->root, inum);
  823. if (err)
  824. return err;
  825. }
  826. }
  827. return 0;
  828. }
  829. static int dbg_scan_orphans(struct ubifs_info *c, struct check_info *ci)
  830. {
  831. int lnum, err = 0;
  832. void *buf;
  833. /* Check no-orphans flag and skip this if no orphans */
  834. if (c->no_orphs)
  835. return 0;
  836. buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
  837. if (!buf) {
  838. ubifs_err("cannot allocate memory to check orphans");
  839. return 0;
  840. }
  841. for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
  842. struct ubifs_scan_leb *sleb;
  843. sleb = ubifs_scan(c, lnum, 0, buf, 0);
  844. if (IS_ERR(sleb)) {
  845. err = PTR_ERR(sleb);
  846. break;
  847. }
  848. err = dbg_read_orphans(ci, sleb);
  849. ubifs_scan_destroy(sleb);
  850. if (err)
  851. break;
  852. }
  853. vfree(buf);
  854. return err;
  855. }
  856. static int dbg_check_orphans(struct ubifs_info *c)
  857. {
  858. struct check_info ci;
  859. int err;
  860. if (!dbg_is_chk_orph(c))
  861. return 0;
  862. ci.last_ino = 0;
  863. ci.tot_inos = 0;
  864. ci.missing = 0;
  865. ci.leaf_cnt = 0;
  866. ci.root = RB_ROOT;
  867. ci.node = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
  868. if (!ci.node) {
  869. ubifs_err("out of memory");
  870. return -ENOMEM;
  871. }
  872. err = dbg_scan_orphans(c, &ci);
  873. if (err)
  874. goto out;
  875. err = dbg_walk_index(c, &dbg_orphan_check, NULL, &ci);
  876. if (err) {
  877. ubifs_err("cannot scan TNC, error %d", err);
  878. goto out;
  879. }
  880. if (ci.missing) {
  881. ubifs_err("%lu missing orphan(s)", ci.missing);
  882. err = -EINVAL;
  883. goto out;
  884. }
  885. dbg_cmt("last inode number is %lu", ci.last_ino);
  886. dbg_cmt("total number of inodes is %lu", ci.tot_inos);
  887. dbg_cmt("total number of leaf nodes is %llu", ci.leaf_cnt);
  888. out:
  889. dbg_free_check_tree(&ci.root);
  890. kfree(ci.node);
  891. return err;
  892. }