orphan.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This file is part of UBIFS.
  4. *
  5. * Copyright (C) 2006-2008 Nokia Corporation.
  6. *
  7. * Author: Adrian Hunter
  8. */
  9. #include "ubifs.h"
  10. /*
  11. * An orphan is an inode number whose inode node has been committed to the index
  12. * with a link count of zero. That happens when an open file is deleted
  13. * (unlinked) and then a commit is run. In the normal course of events the inode
  14. * would be deleted when the file is closed. However in the case of an unclean
  15. * unmount, orphans need to be accounted for. After an unclean unmount, the
  16. * orphans' inodes must be deleted which means either scanning the entire index
  17. * looking for them, or keeping a list on flash somewhere. This unit implements
  18. * the latter approach.
  19. *
  20. * The orphan area is a fixed number of LEBs situated between the LPT area and
  21. * the main area. The number of orphan area LEBs is specified when the file
  22. * system is created. The minimum number is 1. The size of the orphan area
  23. * should be so that it can hold the maximum number of orphans that are expected
  24. * to ever exist at one time.
  25. *
  26. * The number of orphans that can fit in a LEB is:
  27. *
  28. * (c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64)
  29. *
  30. * For example: a 15872 byte LEB can fit 1980 orphans so 1 LEB may be enough.
  31. *
  32. * Orphans are accumulated in a rb-tree. When an inode's link count drops to
  33. * zero, the inode number is added to the rb-tree. It is removed from the tree
  34. * when the inode is deleted. Any new orphans that are in the orphan tree when
  35. * the commit is run, are written to the orphan area in 1 or more orphan nodes.
  36. * If the orphan area is full, it is consolidated to make space. There is
  37. * always enough space because validation prevents the user from creating more
  38. * than the maximum number of orphans allowed.
  39. */
  40. static int dbg_check_orphans(struct ubifs_info *c);
  41. /**
  42. * ubifs_add_orphan - add an orphan.
  43. * @c: UBIFS file-system description object
  44. * @inum: orphan inode number
  45. *
  46. * Add an orphan. This function is called when an inodes link count drops to
  47. * zero.
  48. */
  49. int ubifs_add_orphan(struct ubifs_info *c, ino_t inum)
  50. {
  51. struct ubifs_orphan *orphan, *o;
  52. struct rb_node **p, *parent = NULL;
  53. orphan = kzalloc_obj(struct ubifs_orphan, GFP_NOFS);
  54. if (!orphan)
  55. return -ENOMEM;
  56. orphan->inum = inum;
  57. orphan->new = 1;
  58. spin_lock(&c->orphan_lock);
  59. if (c->tot_orphans >= c->max_orphans) {
  60. spin_unlock(&c->orphan_lock);
  61. kfree(orphan);
  62. return -ENFILE;
  63. }
  64. p = &c->orph_tree.rb_node;
  65. while (*p) {
  66. parent = *p;
  67. o = rb_entry(parent, struct ubifs_orphan, rb);
  68. if (inum < o->inum)
  69. p = &(*p)->rb_left;
  70. else if (inum > o->inum)
  71. p = &(*p)->rb_right;
  72. else {
  73. ubifs_err(c, "ino %lu orphaned twice", (unsigned long)inum);
  74. spin_unlock(&c->orphan_lock);
  75. kfree(orphan);
  76. return -EINVAL;
  77. }
  78. }
  79. c->tot_orphans += 1;
  80. c->new_orphans += 1;
  81. rb_link_node(&orphan->rb, parent, p);
  82. rb_insert_color(&orphan->rb, &c->orph_tree);
  83. list_add_tail(&orphan->list, &c->orph_list);
  84. list_add_tail(&orphan->new_list, &c->orph_new);
  85. spin_unlock(&c->orphan_lock);
  86. dbg_gen("ino %lu", (unsigned long)inum);
  87. return 0;
  88. }
  89. static struct ubifs_orphan *lookup_orphan(struct ubifs_info *c, ino_t inum)
  90. {
  91. struct ubifs_orphan *o;
  92. struct rb_node *p;
  93. p = c->orph_tree.rb_node;
  94. while (p) {
  95. o = rb_entry(p, struct ubifs_orphan, rb);
  96. if (inum < o->inum)
  97. p = p->rb_left;
  98. else if (inum > o->inum)
  99. p = p->rb_right;
  100. else {
  101. return o;
  102. }
  103. }
  104. return NULL;
  105. }
  106. static void __orphan_drop(struct ubifs_info *c, struct ubifs_orphan *o)
  107. {
  108. rb_erase(&o->rb, &c->orph_tree);
  109. list_del(&o->list);
  110. c->tot_orphans -= 1;
  111. if (o->new) {
  112. list_del(&o->new_list);
  113. c->new_orphans -= 1;
  114. }
  115. kfree(o);
  116. }
  117. static void orphan_delete(struct ubifs_info *c, struct ubifs_orphan *orph)
  118. {
  119. if (orph->del) {
  120. dbg_gen("deleted twice ino %lu", (unsigned long)orph->inum);
  121. return;
  122. }
  123. if (orph->cmt) {
  124. orph->del = 1;
  125. rb_erase(&orph->rb, &c->orph_tree);
  126. orph->dnext = c->orph_dnext;
  127. c->orph_dnext = orph;
  128. dbg_gen("delete later ino %lu", (unsigned long)orph->inum);
  129. return;
  130. }
  131. __orphan_drop(c, orph);
  132. }
  133. /**
  134. * ubifs_delete_orphan - delete an orphan.
  135. * @c: UBIFS file-system description object
  136. * @inum: orphan inode number
  137. *
  138. * Delete an orphan. This function is called when an inode is deleted.
  139. */
  140. void ubifs_delete_orphan(struct ubifs_info *c, ino_t inum)
  141. {
  142. struct ubifs_orphan *orph;
  143. spin_lock(&c->orphan_lock);
  144. orph = lookup_orphan(c, inum);
  145. if (!orph) {
  146. spin_unlock(&c->orphan_lock);
  147. ubifs_err(c, "missing orphan ino %lu", (unsigned long)inum);
  148. dump_stack();
  149. return;
  150. }
  151. orphan_delete(c, orph);
  152. spin_unlock(&c->orphan_lock);
  153. }
  154. /**
  155. * ubifs_orphan_start_commit - start commit of orphans.
  156. * @c: UBIFS file-system description object
  157. *
  158. * Start commit of orphans.
  159. */
  160. int ubifs_orphan_start_commit(struct ubifs_info *c)
  161. {
  162. struct ubifs_orphan *orphan, **last;
  163. spin_lock(&c->orphan_lock);
  164. last = &c->orph_cnext;
  165. list_for_each_entry(orphan, &c->orph_new, new_list) {
  166. ubifs_assert(c, orphan->new);
  167. ubifs_assert(c, !orphan->cmt);
  168. orphan->new = 0;
  169. orphan->cmt = 1;
  170. *last = orphan;
  171. last = &orphan->cnext;
  172. }
  173. *last = NULL;
  174. c->cmt_orphans = c->new_orphans;
  175. c->new_orphans = 0;
  176. dbg_cmt("%d orphans to commit", c->cmt_orphans);
  177. INIT_LIST_HEAD(&c->orph_new);
  178. if (c->tot_orphans == 0)
  179. c->no_orphs = 1;
  180. else
  181. c->no_orphs = 0;
  182. spin_unlock(&c->orphan_lock);
  183. return 0;
  184. }
  185. /**
  186. * avail_orphs - calculate available space.
  187. * @c: UBIFS file-system description object
  188. *
  189. * This function returns the number of orphans that can be written in the
  190. * available space.
  191. */
  192. static int avail_orphs(struct ubifs_info *c)
  193. {
  194. int avail_lebs, avail, gap;
  195. avail_lebs = c->orph_lebs - (c->ohead_lnum - c->orph_first) - 1;
  196. avail = avail_lebs *
  197. ((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
  198. gap = c->leb_size - c->ohead_offs;
  199. if (gap >= UBIFS_ORPH_NODE_SZ + sizeof(__le64))
  200. avail += (gap - UBIFS_ORPH_NODE_SZ) / sizeof(__le64);
  201. return avail;
  202. }
  203. /**
  204. * tot_avail_orphs - calculate total space.
  205. * @c: UBIFS file-system description object
  206. *
  207. * This function returns the number of orphans that can be written in half
  208. * the total space. That leaves half the space for adding new orphans.
  209. */
  210. static int tot_avail_orphs(struct ubifs_info *c)
  211. {
  212. int avail_lebs, avail;
  213. avail_lebs = c->orph_lebs;
  214. avail = avail_lebs *
  215. ((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
  216. return avail / 2;
  217. }
  218. /**
  219. * do_write_orph_node - write a node to the orphan head.
  220. * @c: UBIFS file-system description object
  221. * @len: length of node
  222. * @atomic: write atomically
  223. *
  224. * This function writes a node to the orphan head from the orphan buffer. If
  225. * %atomic is not zero, then the write is done atomically. On success, %0 is
  226. * returned, otherwise a negative error code is returned.
  227. */
  228. static int do_write_orph_node(struct ubifs_info *c, int len, int atomic)
  229. {
  230. int err = 0;
  231. if (atomic) {
  232. ubifs_assert(c, c->ohead_offs == 0);
  233. ubifs_prepare_node(c, c->orph_buf, len, 1);
  234. len = ALIGN(len, c->min_io_size);
  235. err = ubifs_leb_change(c, c->ohead_lnum, c->orph_buf, len);
  236. } else {
  237. if (c->ohead_offs == 0) {
  238. /* Ensure LEB has been unmapped */
  239. err = ubifs_leb_unmap(c, c->ohead_lnum);
  240. if (err)
  241. return err;
  242. }
  243. err = ubifs_write_node(c, c->orph_buf, len, c->ohead_lnum,
  244. c->ohead_offs);
  245. }
  246. return err;
  247. }
  248. /**
  249. * write_orph_node - write an orphan node.
  250. * @c: UBIFS file-system description object
  251. * @atomic: write atomically
  252. *
  253. * This function builds an orphan node from the cnext list and writes it to the
  254. * orphan head. On success, %0 is returned, otherwise a negative error code
  255. * is returned.
  256. */
  257. static int write_orph_node(struct ubifs_info *c, int atomic)
  258. {
  259. struct ubifs_orphan *orphan, *cnext;
  260. struct ubifs_orph_node *orph;
  261. int gap, err, len, cnt, i;
  262. ubifs_assert(c, c->cmt_orphans > 0);
  263. gap = c->leb_size - c->ohead_offs;
  264. if (gap < UBIFS_ORPH_NODE_SZ + sizeof(__le64)) {
  265. c->ohead_lnum += 1;
  266. c->ohead_offs = 0;
  267. gap = c->leb_size;
  268. if (c->ohead_lnum > c->orph_last) {
  269. /*
  270. * We limit the number of orphans so that this should
  271. * never happen.
  272. */
  273. ubifs_err(c, "out of space in orphan area");
  274. return -EINVAL;
  275. }
  276. }
  277. cnt = (gap - UBIFS_ORPH_NODE_SZ) / sizeof(__le64);
  278. if (cnt > c->cmt_orphans)
  279. cnt = c->cmt_orphans;
  280. len = UBIFS_ORPH_NODE_SZ + cnt * sizeof(__le64);
  281. ubifs_assert(c, c->orph_buf);
  282. orph = c->orph_buf;
  283. orph->ch.node_type = UBIFS_ORPH_NODE;
  284. spin_lock(&c->orphan_lock);
  285. cnext = c->orph_cnext;
  286. for (i = 0; i < cnt; i++) {
  287. orphan = cnext;
  288. ubifs_assert(c, orphan->cmt);
  289. orph->inos[i] = cpu_to_le64(orphan->inum);
  290. orphan->cmt = 0;
  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, c->ohead_offs + len <= c->leb_size);
  303. ubifs_assert(c, c->ohead_lnum >= c->orph_first);
  304. ubifs_assert(c, 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. 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(c, 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(c, "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, 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(c, !orphan->new);
  423. ubifs_assert(c, orphan->del);
  424. list_del(&orphan->list);
  425. c->tot_orphans -= 1;
  426. dbg_gen("deleting orphan ino %lu", (unsigned long)orphan->inum);
  427. kfree(orphan);
  428. }
  429. c->orph_dnext = NULL;
  430. spin_unlock(&c->orphan_lock);
  431. }
  432. /**
  433. * ubifs_orphan_end_commit - end commit of orphans.
  434. * @c: UBIFS file-system description object
  435. *
  436. * End commit of orphans.
  437. */
  438. int ubifs_orphan_end_commit(struct ubifs_info *c)
  439. {
  440. int err;
  441. if (c->cmt_orphans != 0) {
  442. err = commit_orphans(c);
  443. if (err)
  444. return err;
  445. }
  446. erase_deleted(c);
  447. err = dbg_check_orphans(c);
  448. return err;
  449. }
  450. /**
  451. * ubifs_clear_orphans - erase all LEBs used for orphans.
  452. * @c: UBIFS file-system description object
  453. *
  454. * If recovery is not required, then the orphans from the previous session
  455. * are not needed. This function locates the LEBs used to record
  456. * orphans, and un-maps them.
  457. */
  458. int ubifs_clear_orphans(struct ubifs_info *c)
  459. {
  460. int lnum, err;
  461. for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
  462. err = ubifs_leb_unmap(c, lnum);
  463. if (err)
  464. return err;
  465. }
  466. c->ohead_lnum = c->orph_first;
  467. c->ohead_offs = 0;
  468. return 0;
  469. }
  470. /**
  471. * do_kill_orphans - remove orphan inodes from the index.
  472. * @c: UBIFS file-system description object
  473. * @sleb: scanned LEB
  474. * @last_cmt_no: cmt_no of last orphan node read is passed and returned here
  475. * @outofdate: whether the LEB is out of date is returned here
  476. * @last_flagged: whether the end orphan node is encountered
  477. *
  478. * This function is a helper to the 'kill_orphans()' function. It goes through
  479. * every orphan node in a LEB and for every inode number recorded, removes
  480. * all keys for that inode from the TNC.
  481. */
  482. static int do_kill_orphans(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
  483. unsigned long long *last_cmt_no, int *outofdate,
  484. int *last_flagged)
  485. {
  486. struct ubifs_scan_node *snod;
  487. struct ubifs_orph_node *orph;
  488. struct ubifs_ino_node *ino = NULL;
  489. unsigned long long cmt_no;
  490. ino_t inum;
  491. int i, n, err, first = 1;
  492. ino = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
  493. if (!ino)
  494. return -ENOMEM;
  495. list_for_each_entry(snod, &sleb->nodes, list) {
  496. if (snod->type != UBIFS_ORPH_NODE) {
  497. ubifs_err(c, "invalid node type %d in orphan area at %d:%d",
  498. snod->type, sleb->lnum, snod->offs);
  499. ubifs_dump_node(c, snod->node,
  500. c->leb_size - snod->offs);
  501. err = -EINVAL;
  502. goto out_free;
  503. }
  504. orph = snod->node;
  505. /* Check commit number */
  506. cmt_no = le64_to_cpu(orph->cmt_no) & LLONG_MAX;
  507. /*
  508. * The commit number on the master node may be less, because
  509. * of a failed commit. If there are several failed commits in a
  510. * row, the commit number written on orphan nodes will continue
  511. * to increase (because the commit number is adjusted here) even
  512. * though the commit number on the master node stays the same
  513. * because the master node has not been re-written.
  514. */
  515. if (cmt_no > c->cmt_no)
  516. c->cmt_no = cmt_no;
  517. if (cmt_no < *last_cmt_no && *last_flagged) {
  518. /*
  519. * The last orphan node had a higher commit number and
  520. * was flagged as the last written for that commit
  521. * number. That makes this orphan node, out of date.
  522. */
  523. if (!first) {
  524. ubifs_err(c, "out of order commit number %llu in orphan node at %d:%d",
  525. cmt_no, sleb->lnum, snod->offs);
  526. ubifs_dump_node(c, snod->node,
  527. c->leb_size - snod->offs);
  528. err = -EINVAL;
  529. goto out_free;
  530. }
  531. dbg_rcvry("out of date LEB %d", sleb->lnum);
  532. *outofdate = 1;
  533. err = 0;
  534. goto out_free;
  535. }
  536. if (first)
  537. first = 0;
  538. n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
  539. for (i = 0; i < n; i++) {
  540. union ubifs_key key;
  541. inum = le64_to_cpu(orph->inos[i]);
  542. ino_key_init(c, &key, inum);
  543. err = ubifs_tnc_lookup(c, &key, ino);
  544. if (err && err != -ENOENT)
  545. goto out_free;
  546. /*
  547. * Check whether an inode can really get deleted.
  548. * linkat() with O_TMPFILE allows rebirth of an inode.
  549. */
  550. if (err == 0 && ino->nlink == 0) {
  551. dbg_rcvry("deleting orphaned inode %lu",
  552. (unsigned long)inum);
  553. err = ubifs_tnc_remove_ino(c, inum);
  554. if (err)
  555. goto out_ro;
  556. }
  557. }
  558. *last_cmt_no = cmt_no;
  559. if (le64_to_cpu(orph->cmt_no) & (1ULL << 63)) {
  560. dbg_rcvry("last orph node for commit %llu at %d:%d",
  561. cmt_no, sleb->lnum, snod->offs);
  562. *last_flagged = 1;
  563. } else
  564. *last_flagged = 0;
  565. }
  566. err = 0;
  567. out_free:
  568. kfree(ino);
  569. return err;
  570. out_ro:
  571. ubifs_ro_mode(c, err);
  572. kfree(ino);
  573. return err;
  574. }
  575. /**
  576. * kill_orphans - remove all orphan inodes from the index.
  577. * @c: UBIFS file-system description object
  578. *
  579. * If recovery is required, then orphan inodes recorded during the previous
  580. * session (which ended with an unclean unmount) must be deleted from the index.
  581. * This is done by updating the TNC, but since the index is not updated until
  582. * the next commit, the LEBs where the orphan information is recorded are not
  583. * erased until the next commit.
  584. */
  585. static int kill_orphans(struct ubifs_info *c)
  586. {
  587. unsigned long long last_cmt_no = 0;
  588. int lnum, err = 0, outofdate = 0, last_flagged = 0;
  589. c->ohead_lnum = c->orph_first;
  590. c->ohead_offs = 0;
  591. /* Check no-orphans flag and skip this if no orphans */
  592. if (c->no_orphs) {
  593. dbg_rcvry("no orphans");
  594. return 0;
  595. }
  596. /*
  597. * Orph nodes always start at c->orph_first and are written to each
  598. * successive LEB in turn. Generally unused LEBs will have been unmapped
  599. * but may contain out of date orphan nodes if the unmap didn't go
  600. * through. In addition, the last orphan node written for each commit is
  601. * marked (top bit of orph->cmt_no is set to 1). It is possible that
  602. * there are orphan nodes from the next commit (i.e. the commit did not
  603. * complete successfully). In that case, no orphans will have been lost
  604. * due to the way that orphans are written, and any orphans added will
  605. * be valid orphans anyway and so can be deleted.
  606. */
  607. for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
  608. struct ubifs_scan_leb *sleb;
  609. dbg_rcvry("LEB %d", lnum);
  610. sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
  611. if (IS_ERR(sleb)) {
  612. if (PTR_ERR(sleb) == -EUCLEAN)
  613. sleb = ubifs_recover_leb(c, lnum, 0,
  614. c->sbuf, -1);
  615. if (IS_ERR(sleb)) {
  616. err = PTR_ERR(sleb);
  617. break;
  618. }
  619. }
  620. err = do_kill_orphans(c, sleb, &last_cmt_no, &outofdate,
  621. &last_flagged);
  622. if (err || outofdate) {
  623. ubifs_scan_destroy(sleb);
  624. break;
  625. }
  626. if (sleb->endpt) {
  627. c->ohead_lnum = lnum;
  628. c->ohead_offs = sleb->endpt;
  629. }
  630. ubifs_scan_destroy(sleb);
  631. }
  632. return err;
  633. }
  634. /**
  635. * ubifs_mount_orphans - delete orphan inodes and erase LEBs that recorded them.
  636. * @c: UBIFS file-system description object
  637. * @unclean: indicates recovery from unclean unmount
  638. * @read_only: indicates read only mount
  639. *
  640. * This function is called when mounting to erase orphans from the previous
  641. * session. If UBIFS was not unmounted cleanly, then the inodes recorded as
  642. * orphans are deleted.
  643. */
  644. int ubifs_mount_orphans(struct ubifs_info *c, int unclean, int read_only)
  645. {
  646. int err = 0;
  647. c->max_orphans = tot_avail_orphs(c);
  648. if (!read_only) {
  649. c->orph_buf = vmalloc(c->leb_size);
  650. if (!c->orph_buf)
  651. return -ENOMEM;
  652. }
  653. if (unclean)
  654. err = kill_orphans(c);
  655. else if (!read_only)
  656. err = ubifs_clear_orphans(c);
  657. return err;
  658. }
  659. /*
  660. * Everything below is related to debugging.
  661. */
  662. struct check_orphan {
  663. struct rb_node rb;
  664. ino_t inum;
  665. };
  666. struct check_info {
  667. unsigned long last_ino;
  668. unsigned long tot_inos;
  669. unsigned long missing;
  670. unsigned long long leaf_cnt;
  671. struct ubifs_ino_node *node;
  672. struct rb_root root;
  673. };
  674. static bool dbg_find_orphan(struct ubifs_info *c, ino_t inum)
  675. {
  676. bool found = false;
  677. spin_lock(&c->orphan_lock);
  678. found = !!lookup_orphan(c, inum);
  679. spin_unlock(&c->orphan_lock);
  680. return found;
  681. }
  682. static int dbg_ins_check_orphan(struct rb_root *root, ino_t inum)
  683. {
  684. struct check_orphan *orphan, *o;
  685. struct rb_node **p, *parent = NULL;
  686. orphan = kzalloc_obj(struct check_orphan, GFP_NOFS);
  687. if (!orphan)
  688. return -ENOMEM;
  689. orphan->inum = inum;
  690. p = &root->rb_node;
  691. while (*p) {
  692. parent = *p;
  693. o = rb_entry(parent, struct check_orphan, rb);
  694. if (inum < o->inum)
  695. p = &(*p)->rb_left;
  696. else if (inum > o->inum)
  697. p = &(*p)->rb_right;
  698. else {
  699. kfree(orphan);
  700. return 0;
  701. }
  702. }
  703. rb_link_node(&orphan->rb, parent, p);
  704. rb_insert_color(&orphan->rb, root);
  705. return 0;
  706. }
  707. static int dbg_find_check_orphan(struct rb_root *root, ino_t inum)
  708. {
  709. struct check_orphan *o;
  710. struct rb_node *p;
  711. p = root->rb_node;
  712. while (p) {
  713. o = rb_entry(p, struct check_orphan, rb);
  714. if (inum < o->inum)
  715. p = p->rb_left;
  716. else if (inum > o->inum)
  717. p = p->rb_right;
  718. else
  719. return 1;
  720. }
  721. return 0;
  722. }
  723. static void dbg_free_check_tree(struct rb_root *root)
  724. {
  725. struct check_orphan *o, *n;
  726. rbtree_postorder_for_each_entry_safe(o, n, root, rb)
  727. kfree(o);
  728. }
  729. static int dbg_orphan_check(struct ubifs_info *c, struct ubifs_zbranch *zbr,
  730. void *priv)
  731. {
  732. struct check_info *ci = priv;
  733. ino_t inum;
  734. int err;
  735. inum = key_inum(c, &zbr->key);
  736. if (inum != ci->last_ino) {
  737. /*
  738. * Lowest node type is the inode node or xattr entry(when
  739. * selinux/encryption is enabled), so it comes first
  740. */
  741. if (key_type(c, &zbr->key) != UBIFS_INO_KEY &&
  742. key_type(c, &zbr->key) != UBIFS_XENT_KEY)
  743. ubifs_err(c, "found orphan node ino %lu, type %d",
  744. (unsigned long)inum, key_type(c, &zbr->key));
  745. ci->last_ino = inum;
  746. ci->tot_inos += 1;
  747. err = ubifs_tnc_read_node(c, zbr, ci->node);
  748. if (err) {
  749. ubifs_err(c, "node read failed, error %d", err);
  750. return err;
  751. }
  752. if (ci->node->nlink == 0)
  753. /* Must be recorded as an orphan */
  754. if (!dbg_find_check_orphan(&ci->root, inum) &&
  755. !dbg_find_orphan(c, inum)) {
  756. ubifs_err(c, "missing orphan, ino %lu",
  757. (unsigned long)inum);
  758. ci->missing += 1;
  759. }
  760. }
  761. ci->leaf_cnt += 1;
  762. return 0;
  763. }
  764. static int dbg_read_orphans(struct check_info *ci, struct ubifs_scan_leb *sleb)
  765. {
  766. struct ubifs_scan_node *snod;
  767. struct ubifs_orph_node *orph;
  768. ino_t inum;
  769. int i, n, err;
  770. list_for_each_entry(snod, &sleb->nodes, list) {
  771. cond_resched();
  772. if (snod->type != UBIFS_ORPH_NODE)
  773. continue;
  774. orph = snod->node;
  775. n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
  776. for (i = 0; i < n; i++) {
  777. inum = le64_to_cpu(orph->inos[i]);
  778. err = dbg_ins_check_orphan(&ci->root, inum);
  779. if (err)
  780. return err;
  781. }
  782. }
  783. return 0;
  784. }
  785. static int dbg_scan_orphans(struct ubifs_info *c, struct check_info *ci)
  786. {
  787. int lnum, err = 0;
  788. void *buf;
  789. /* Check no-orphans flag and skip this if no orphans */
  790. if (c->no_orphs)
  791. return 0;
  792. buf = __vmalloc(c->leb_size, GFP_NOFS);
  793. if (!buf) {
  794. ubifs_err(c, "cannot allocate memory to check orphans");
  795. return 0;
  796. }
  797. for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
  798. struct ubifs_scan_leb *sleb;
  799. sleb = ubifs_scan(c, lnum, 0, buf, 0);
  800. if (IS_ERR(sleb)) {
  801. err = PTR_ERR(sleb);
  802. break;
  803. }
  804. err = dbg_read_orphans(ci, sleb);
  805. ubifs_scan_destroy(sleb);
  806. if (err)
  807. break;
  808. }
  809. vfree(buf);
  810. return err;
  811. }
  812. static int dbg_check_orphans(struct ubifs_info *c)
  813. {
  814. struct check_info ci;
  815. int err;
  816. if (!dbg_is_chk_orph(c))
  817. return 0;
  818. ci.last_ino = 0;
  819. ci.tot_inos = 0;
  820. ci.missing = 0;
  821. ci.leaf_cnt = 0;
  822. ci.root = RB_ROOT;
  823. ci.node = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
  824. if (!ci.node) {
  825. ubifs_err(c, "out of memory");
  826. return -ENOMEM;
  827. }
  828. err = dbg_scan_orphans(c, &ci);
  829. if (err)
  830. goto out;
  831. err = dbg_walk_index(c, &dbg_orphan_check, NULL, &ci);
  832. if (err) {
  833. ubifs_err(c, "cannot scan TNC, error %d", err);
  834. goto out;
  835. }
  836. if (ci.missing) {
  837. ubifs_err(c, "%lu missing orphan(s)", ci.missing);
  838. err = -EINVAL;
  839. goto out;
  840. }
  841. dbg_cmt("last inode number is %lu", ci.last_ino);
  842. dbg_cmt("total number of inodes is %lu", ci.tot_inos);
  843. dbg_cmt("total number of leaf nodes is %llu", ci.leaf_cnt);
  844. out:
  845. dbg_free_check_tree(&ci.root);
  846. kfree(ci.node);
  847. return err;
  848. }