orphan.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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->del) {
  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->del = 1;
  133. o->dnext = c->orph_dnext;
  134. c->orph_dnext = o;
  135. spin_unlock(&c->orphan_lock);
  136. dbg_gen("delete later ino %lu",
  137. (unsigned long)inum);
  138. return;
  139. }
  140. rb_erase(p, &c->orph_tree);
  141. list_del(&o->list);
  142. c->tot_orphans -= 1;
  143. if (o->new) {
  144. list_del(&o->new_list);
  145. c->new_orphans -= 1;
  146. }
  147. spin_unlock(&c->orphan_lock);
  148. kfree(o);
  149. dbg_gen("inum %lu", (unsigned long)inum);
  150. return;
  151. }
  152. }
  153. spin_unlock(&c->orphan_lock);
  154. dbg_err("missing orphan ino %lu", (unsigned long)inum);
  155. dbg_dump_stack();
  156. }
  157. /**
  158. * ubifs_orphan_start_commit - start commit of orphans.
  159. * @c: UBIFS file-system description object
  160. *
  161. * Start commit of orphans.
  162. */
  163. int ubifs_orphan_start_commit(struct ubifs_info *c)
  164. {
  165. struct ubifs_orphan *orphan, **last;
  166. spin_lock(&c->orphan_lock);
  167. last = &c->orph_cnext;
  168. list_for_each_entry(orphan, &c->orph_new, new_list) {
  169. ubifs_assert(orphan->new);
  170. orphan->new = 0;
  171. *last = orphan;
  172. last = &orphan->cnext;
  173. }
  174. *last = orphan->cnext;
  175. c->cmt_orphans = c->new_orphans;
  176. c->new_orphans = 0;
  177. dbg_cmt("%d orphans to commit", c->cmt_orphans);
  178. INIT_LIST_HEAD(&c->orph_new);
  179. if (c->tot_orphans == 0)
  180. c->no_orphs = 1;
  181. else
  182. c->no_orphs = 0;
  183. spin_unlock(&c->orphan_lock);
  184. return 0;
  185. }
  186. /**
  187. * avail_orphs - calculate available space.
  188. * @c: UBIFS file-system description object
  189. *
  190. * This function returns the number of orphans that can be written in the
  191. * available space.
  192. */
  193. static int avail_orphs(struct ubifs_info *c)
  194. {
  195. int avail_lebs, avail, gap;
  196. avail_lebs = c->orph_lebs - (c->ohead_lnum - c->orph_first) - 1;
  197. avail = avail_lebs *
  198. ((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
  199. gap = c->leb_size - c->ohead_offs;
  200. if (gap >= UBIFS_ORPH_NODE_SZ + sizeof(__le64))
  201. avail += (gap - UBIFS_ORPH_NODE_SZ) / sizeof(__le64);
  202. return avail;
  203. }
  204. /**
  205. * tot_avail_orphs - calculate total space.
  206. * @c: UBIFS file-system description object
  207. *
  208. * This function returns the number of orphans that can be written in half
  209. * the total space. That leaves half the space for adding new orphans.
  210. */
  211. static int tot_avail_orphs(struct ubifs_info *c)
  212. {
  213. int avail_lebs, avail;
  214. avail_lebs = c->orph_lebs;
  215. avail = avail_lebs *
  216. ((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
  217. return avail / 2;
  218. }
  219. /**
  220. * do_write_orph_node - write a node to the orphan head.
  221. * @c: UBIFS file-system description object
  222. * @len: length of node
  223. * @atomic: write atomically
  224. *
  225. * This function writes a node to the orphan head from the orphan buffer. If
  226. * %atomic is not zero, then the write is done atomically. On success, %0 is
  227. * returned, otherwise a negative error code is returned.
  228. */
  229. static int do_write_orph_node(struct ubifs_info *c, int len, int atomic)
  230. {
  231. int err = 0;
  232. if (atomic) {
  233. ubifs_assert(c->ohead_offs == 0);
  234. ubifs_prepare_node(c, c->orph_buf, len, 1);
  235. len = ALIGN(len, c->min_io_size);
  236. err = ubifs_leb_change(c, c->ohead_lnum, c->orph_buf, len,
  237. UBI_SHORTTERM);
  238. } else {
  239. if (c->ohead_offs == 0) {
  240. /* Ensure LEB has been unmapped */
  241. err = ubifs_leb_unmap(c, c->ohead_lnum);
  242. if (err)
  243. return err;
  244. }
  245. err = ubifs_write_node(c, c->orph_buf, len, c->ohead_lnum,
  246. c->ohead_offs, UBI_SHORTTERM);
  247. }
  248. return err;
  249. }
  250. /**
  251. * write_orph_node - write an orphan node.
  252. * @c: UBIFS file-system description object
  253. * @atomic: write atomically
  254. *
  255. * This function builds an orphan node from the cnext list and writes it to the
  256. * orphan head. On success, %0 is returned, otherwise a negative error code
  257. * is returned.
  258. */
  259. static int write_orph_node(struct ubifs_info *c, int atomic)
  260. {
  261. struct ubifs_orphan *orphan, *cnext;
  262. struct ubifs_orph_node *orph;
  263. int gap, err, len, cnt, i;
  264. ubifs_assert(c->cmt_orphans > 0);
  265. gap = c->leb_size - c->ohead_offs;
  266. if (gap < UBIFS_ORPH_NODE_SZ + sizeof(__le64)) {
  267. c->ohead_lnum += 1;
  268. c->ohead_offs = 0;
  269. gap = c->leb_size;
  270. if (c->ohead_lnum > c->orph_last) {
  271. /*
  272. * We limit the number of orphans so that this should
  273. * never happen.
  274. */
  275. ubifs_err("out of space in orphan area");
  276. return -EINVAL;
  277. }
  278. }
  279. cnt = (gap - UBIFS_ORPH_NODE_SZ) / sizeof(__le64);
  280. if (cnt > c->cmt_orphans)
  281. cnt = c->cmt_orphans;
  282. len = UBIFS_ORPH_NODE_SZ + cnt * sizeof(__le64);
  283. ubifs_assert(c->orph_buf);
  284. orph = c->orph_buf;
  285. orph->ch.node_type = UBIFS_ORPH_NODE;
  286. spin_lock(&c->orphan_lock);
  287. cnext = c->orph_cnext;
  288. for (i = 0; i < cnt; i++) {
  289. orphan = cnext;
  290. orph->inos[i] = cpu_to_le64(orphan->inum);
  291. cnext = orphan->cnext;
  292. orphan->cnext = NULL;
  293. }
  294. c->orph_cnext = cnext;
  295. c->cmt_orphans -= cnt;
  296. spin_unlock(&c->orphan_lock);
  297. if (c->cmt_orphans)
  298. orph->cmt_no = cpu_to_le64(c->cmt_no);
  299. else
  300. /* Mark the last node of the commit */
  301. orph->cmt_no = cpu_to_le64((c->cmt_no) | (1ULL << 63));
  302. ubifs_assert(c->ohead_offs + len <= c->leb_size);
  303. ubifs_assert(c->ohead_lnum >= c->orph_first);
  304. ubifs_assert(c->ohead_lnum <= c->orph_last);
  305. err = do_write_orph_node(c, len, atomic);
  306. c->ohead_offs += ALIGN(len, c->min_io_size);
  307. c->ohead_offs = ALIGN(c->ohead_offs, 8);
  308. return err;
  309. }
  310. /**
  311. * write_orph_nodes - write orphan nodes until there are no more to commit.
  312. * @c: UBIFS file-system description object
  313. * @atomic: write atomically
  314. *
  315. * This function writes orphan nodes for all the orphans to commit. On success,
  316. * %0 is returned, otherwise a negative error code is returned.
  317. */
  318. static int write_orph_nodes(struct ubifs_info *c, int atomic)
  319. {
  320. int err;
  321. while (c->cmt_orphans > 0) {
  322. err = write_orph_node(c, atomic);
  323. if (err)
  324. return err;
  325. }
  326. if (atomic) {
  327. int lnum;
  328. /* Unmap any unused LEBs after consolidation */
  329. lnum = c->ohead_lnum + 1;
  330. for (lnum = c->ohead_lnum + 1; lnum <= c->orph_last; lnum++) {
  331. err = ubifs_leb_unmap(c, lnum);
  332. if (err)
  333. return err;
  334. }
  335. }
  336. return 0;
  337. }
  338. /**
  339. * consolidate - consolidate the orphan area.
  340. * @c: UBIFS file-system description object
  341. *
  342. * This function enables consolidation by putting all the orphans into the list
  343. * to commit. The list is in the order that the orphans were added, and the
  344. * LEBs are written atomically in order, so at no time can orphans be lost by
  345. * an unclean unmount.
  346. *
  347. * This function returns %0 on success and a negative error code on failure.
  348. */
  349. static int consolidate(struct ubifs_info *c)
  350. {
  351. int tot_avail = tot_avail_orphs(c), err = 0;
  352. spin_lock(&c->orphan_lock);
  353. dbg_cmt("there is space for %d orphans and there are %d",
  354. tot_avail, c->tot_orphans);
  355. if (c->tot_orphans - c->new_orphans <= tot_avail) {
  356. struct ubifs_orphan *orphan, **last;
  357. int cnt = 0;
  358. /* Change the cnext list to include all non-new orphans */
  359. last = &c->orph_cnext;
  360. list_for_each_entry(orphan, &c->orph_list, list) {
  361. if (orphan->new)
  362. continue;
  363. *last = orphan;
  364. last = &orphan->cnext;
  365. cnt += 1;
  366. }
  367. *last = orphan->cnext;
  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 "
  537. "%d:%d", snod->type, sleb->lnum, snod->offs);
  538. dbg_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 "
  562. "orphan node at %d:%d",
  563. cmt_no, sleb->lnum, snod->offs);
  564. dbg_dump_node(c, snod->node);
  565. return -EINVAL;
  566. }
  567. dbg_rcvry("out of date LEB %d", sleb->lnum);
  568. *outofdate = 1;
  569. return 0;
  570. }
  571. if (first)
  572. first = 0;
  573. n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
  574. for (i = 0; i < n; i++) {
  575. inum = le64_to_cpu(orph->inos[i]);
  576. dbg_rcvry("deleting orphaned inode %lu",
  577. (unsigned long)inum);
  578. err = ubifs_tnc_remove_ino(c, inum);
  579. if (err)
  580. return err;
  581. err = insert_dead_orphan(c, inum);
  582. if (err)
  583. return err;
  584. }
  585. *last_cmt_no = cmt_no;
  586. if (le64_to_cpu(orph->cmt_no) & (1ULL << 63)) {
  587. dbg_rcvry("last orph node for commit %llu at %d:%d",
  588. cmt_no, sleb->lnum, snod->offs);
  589. *last_flagged = 1;
  590. } else
  591. *last_flagged = 0;
  592. }
  593. return 0;
  594. }
  595. /**
  596. * kill_orphans - remove all orphan inodes from the index.
  597. * @c: UBIFS file-system description object
  598. *
  599. * If recovery is required, then orphan inodes recorded during the previous
  600. * session (which ended with an unclean unmount) must be deleted from the index.
  601. * This is done by updating the TNC, but since the index is not updated until
  602. * the next commit, the LEBs where the orphan information is recorded are not
  603. * erased until the next commit.
  604. */
  605. static int kill_orphans(struct ubifs_info *c)
  606. {
  607. unsigned long long last_cmt_no = 0;
  608. int lnum, err = 0, outofdate = 0, last_flagged = 0;
  609. c->ohead_lnum = c->orph_first;
  610. c->ohead_offs = 0;
  611. /* Check no-orphans flag and skip this if no orphans */
  612. if (c->no_orphs) {
  613. dbg_rcvry("no orphans");
  614. return 0;
  615. }
  616. /*
  617. * Orph nodes always start at c->orph_first and are written to each
  618. * successive LEB in turn. Generally unused LEBs will have been unmapped
  619. * but may contain out of date orphan nodes if the unmap didn't go
  620. * through. In addition, the last orphan node written for each commit is
  621. * marked (top bit of orph->cmt_no is set to 1). It is possible that
  622. * there are orphan nodes from the next commit (i.e. the commit did not
  623. * complete successfully). In that case, no orphans will have been lost
  624. * due to the way that orphans are written, and any orphans added will
  625. * be valid orphans anyway and so can be deleted.
  626. */
  627. for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
  628. struct ubifs_scan_leb *sleb;
  629. dbg_rcvry("LEB %d", lnum);
  630. sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
  631. if (IS_ERR(sleb)) {
  632. if (PTR_ERR(sleb) == -EUCLEAN)
  633. sleb = ubifs_recover_leb(c, lnum, 0,
  634. c->sbuf, -1);
  635. if (IS_ERR(sleb)) {
  636. err = PTR_ERR(sleb);
  637. break;
  638. }
  639. }
  640. err = do_kill_orphans(c, sleb, &last_cmt_no, &outofdate,
  641. &last_flagged);
  642. if (err || outofdate) {
  643. ubifs_scan_destroy(sleb);
  644. break;
  645. }
  646. if (sleb->endpt) {
  647. c->ohead_lnum = lnum;
  648. c->ohead_offs = sleb->endpt;
  649. }
  650. ubifs_scan_destroy(sleb);
  651. }
  652. return err;
  653. }
  654. /**
  655. * ubifs_mount_orphans - delete orphan inodes and erase LEBs that recorded them.
  656. * @c: UBIFS file-system description object
  657. * @unclean: indicates recovery from unclean unmount
  658. * @read_only: indicates read only mount
  659. *
  660. * This function is called when mounting to erase orphans from the previous
  661. * session. If UBIFS was not unmounted cleanly, then the inodes recorded as
  662. * orphans are deleted.
  663. */
  664. int ubifs_mount_orphans(struct ubifs_info *c, int unclean, int read_only)
  665. {
  666. int err = 0;
  667. c->max_orphans = tot_avail_orphs(c);
  668. if (!read_only) {
  669. c->orph_buf = vmalloc(c->leb_size);
  670. if (!c->orph_buf)
  671. return -ENOMEM;
  672. }
  673. if (unclean)
  674. err = kill_orphans(c);
  675. else if (!read_only)
  676. err = ubifs_clear_orphans(c);
  677. return err;
  678. }
  679. #ifdef CONFIG_UBIFS_FS_DEBUG
  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 rb_node *this = root->rb_node;
  756. struct check_orphan *o;
  757. while (this) {
  758. if (this->rb_left) {
  759. this = this->rb_left;
  760. continue;
  761. } else if (this->rb_right) {
  762. this = this->rb_right;
  763. continue;
  764. }
  765. o = rb_entry(this, struct check_orphan, rb);
  766. this = rb_parent(this);
  767. if (this) {
  768. if (this->rb_left == &o->rb)
  769. this->rb_left = NULL;
  770. else
  771. this->rb_right = NULL;
  772. }
  773. kfree(o);
  774. }
  775. }
  776. static int dbg_orphan_check(struct ubifs_info *c, struct ubifs_zbranch *zbr,
  777. void *priv)
  778. {
  779. struct check_info *ci = priv;
  780. ino_t inum;
  781. int err;
  782. inum = key_inum(c, &zbr->key);
  783. if (inum != ci->last_ino) {
  784. /* Lowest node type is the inode node, so it comes first */
  785. if (key_type(c, &zbr->key) != UBIFS_INO_KEY)
  786. ubifs_err("found orphan node ino %lu, type %d",
  787. (unsigned long)inum, key_type(c, &zbr->key));
  788. ci->last_ino = inum;
  789. ci->tot_inos += 1;
  790. err = ubifs_tnc_read_node(c, zbr, ci->node);
  791. if (err) {
  792. ubifs_err("node read failed, error %d", err);
  793. return err;
  794. }
  795. if (ci->node->nlink == 0)
  796. /* Must be recorded as an orphan */
  797. if (!dbg_find_check_orphan(&ci->root, inum) &&
  798. !dbg_find_orphan(c, inum)) {
  799. ubifs_err("missing orphan, ino %lu",
  800. (unsigned long)inum);
  801. ci->missing += 1;
  802. }
  803. }
  804. ci->leaf_cnt += 1;
  805. return 0;
  806. }
  807. static int dbg_read_orphans(struct check_info *ci, struct ubifs_scan_leb *sleb)
  808. {
  809. struct ubifs_scan_node *snod;
  810. struct ubifs_orph_node *orph;
  811. ino_t inum;
  812. int i, n, err;
  813. list_for_each_entry(snod, &sleb->nodes, list) {
  814. cond_resched();
  815. if (snod->type != UBIFS_ORPH_NODE)
  816. continue;
  817. orph = snod->node;
  818. n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
  819. for (i = 0; i < n; i++) {
  820. inum = le64_to_cpu(orph->inos[i]);
  821. err = dbg_ins_check_orphan(&ci->root, inum);
  822. if (err)
  823. return err;
  824. }
  825. }
  826. return 0;
  827. }
  828. static int dbg_scan_orphans(struct ubifs_info *c, struct check_info *ci)
  829. {
  830. int lnum, err = 0;
  831. void *buf;
  832. /* Check no-orphans flag and skip this if no orphans */
  833. if (c->no_orphs)
  834. return 0;
  835. buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
  836. if (!buf) {
  837. ubifs_err("cannot allocate memory to check orphans");
  838. return 0;
  839. }
  840. for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
  841. struct ubifs_scan_leb *sleb;
  842. sleb = ubifs_scan(c, lnum, 0, buf, 0);
  843. if (IS_ERR(sleb)) {
  844. err = PTR_ERR(sleb);
  845. break;
  846. }
  847. err = dbg_read_orphans(ci, sleb);
  848. ubifs_scan_destroy(sleb);
  849. if (err)
  850. break;
  851. }
  852. vfree(buf);
  853. return err;
  854. }
  855. static int dbg_check_orphans(struct ubifs_info *c)
  856. {
  857. struct check_info ci;
  858. int err;
  859. if (!dbg_is_chk_orph(c))
  860. return 0;
  861. ci.last_ino = 0;
  862. ci.tot_inos = 0;
  863. ci.missing = 0;
  864. ci.leaf_cnt = 0;
  865. ci.root = RB_ROOT;
  866. ci.node = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
  867. if (!ci.node) {
  868. ubifs_err("out of memory");
  869. return -ENOMEM;
  870. }
  871. err = dbg_scan_orphans(c, &ci);
  872. if (err)
  873. goto out;
  874. err = dbg_walk_index(c, &dbg_orphan_check, NULL, &ci);
  875. if (err) {
  876. ubifs_err("cannot scan TNC, error %d", err);
  877. goto out;
  878. }
  879. if (ci.missing) {
  880. ubifs_err("%lu missing orphan(s)", ci.missing);
  881. err = -EINVAL;
  882. goto out;
  883. }
  884. dbg_cmt("last inode number is %lu", ci.last_ino);
  885. dbg_cmt("total number of inodes is %lu", ci.tot_inos);
  886. dbg_cmt("total number of leaf nodes is %llu", ci.leaf_cnt);
  887. out:
  888. dbg_free_check_tree(&ci.root);
  889. kfree(ci.node);
  890. return err;
  891. }
  892. #endif /* CONFIG_UBIFS_FS_DEBUG */