orphan.c 26 KB

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