orphan.c 25 KB

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