2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * SPDX-License-Identifier: GPL-2.0+
8 * Author: Adrian Hunter
11 #include <linux/err.h>
15 * An orphan is an inode number whose inode node has been committed to the index
16 * with a link count of zero. That happens when an open file is deleted
17 * (unlinked) and then a commit is run. In the normal course of events the inode
18 * would be deleted when the file is closed. However in the case of an unclean
19 * unmount, orphans need to be accounted for. After an unclean unmount, the
20 * orphans' inodes must be deleted which means either scanning the entire index
21 * looking for them, or keeping a list on flash somewhere. This unit implements
22 * the latter approach.
24 * The orphan area is a fixed number of LEBs situated between the LPT area and
25 * the main area. The number of orphan area LEBs is specified when the file
26 * system is created. The minimum number is 1. The size of the orphan area
27 * should be so that it can hold the maximum number of orphans that are expected
28 * to ever exist at one time.
30 * The number of orphans that can fit in a LEB is:
32 * (c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64)
34 * For example: a 15872 byte LEB can fit 1980 orphans so 1 LEB may be enough.
36 * Orphans are accumulated in a rb-tree. When an inode's link count drops to
37 * zero, the inode number is added to the rb-tree. It is removed from the tree
38 * when the inode is deleted. Any new orphans that are in the orphan tree when
39 * the commit is run, are written to the orphan area in 1 or more orphan nodes.
40 * If the orphan area is full, it is consolidated to make space. There is
41 * always enough space because validation prevents the user from creating more
42 * than the maximum number of orphans allowed.
45 static int dbg_check_orphans(struct ubifs_info *c);
48 * ubifs_add_orphan - add an orphan.
49 * @c: UBIFS file-system description object
50 * @inum: orphan inode number
52 * Add an orphan. This function is called when an inodes link count drops to
55 int ubifs_add_orphan(struct ubifs_info *c, ino_t inum)
57 struct ubifs_orphan *orphan, *o;
58 struct rb_node **p, *parent = NULL;
60 orphan = kzalloc(sizeof(struct ubifs_orphan), GFP_NOFS);
66 spin_lock(&c->orphan_lock);
67 if (c->tot_orphans >= c->max_orphans) {
68 spin_unlock(&c->orphan_lock);
72 p = &c->orph_tree.rb_node;
75 o = rb_entry(parent, struct ubifs_orphan, rb);
78 else if (inum > o->inum)
81 ubifs_err(c, "orphaned twice");
82 spin_unlock(&c->orphan_lock);
89 rb_link_node(&orphan->rb, parent, p);
90 rb_insert_color(&orphan->rb, &c->orph_tree);
91 list_add_tail(&orphan->list, &c->orph_list);
92 list_add_tail(&orphan->new_list, &c->orph_new);
93 spin_unlock(&c->orphan_lock);
94 dbg_gen("ino %lu", (unsigned long)inum);
99 * ubifs_delete_orphan - delete an orphan.
100 * @c: UBIFS file-system description object
101 * @inum: orphan inode number
103 * Delete an orphan. This function is called when an inode is deleted.
105 void ubifs_delete_orphan(struct ubifs_info *c, ino_t inum)
107 struct ubifs_orphan *o;
110 spin_lock(&c->orphan_lock);
111 p = c->orph_tree.rb_node;
113 o = rb_entry(p, struct ubifs_orphan, rb);
116 else if (inum > o->inum)
120 spin_unlock(&c->orphan_lock);
121 dbg_gen("deleted twice ino %lu",
122 (unsigned long)inum);
127 o->dnext = c->orph_dnext;
129 spin_unlock(&c->orphan_lock);
130 dbg_gen("delete later ino %lu",
131 (unsigned long)inum);
134 rb_erase(p, &c->orph_tree);
138 list_del(&o->new_list);
141 spin_unlock(&c->orphan_lock);
143 dbg_gen("inum %lu", (unsigned long)inum);
147 spin_unlock(&c->orphan_lock);
148 ubifs_err(c, "missing orphan ino %lu", (unsigned long)inum);
153 * ubifs_orphan_start_commit - start commit of orphans.
154 * @c: UBIFS file-system description object
156 * Start commit of orphans.
158 int ubifs_orphan_start_commit(struct ubifs_info *c)
160 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);
170 last = &orphan->cnext;
173 c->cmt_orphans = c->new_orphans;
175 dbg_cmt("%d orphans to commit", c->cmt_orphans);
176 INIT_LIST_HEAD(&c->orph_new);
177 if (c->tot_orphans == 0)
181 spin_unlock(&c->orphan_lock);
186 * avail_orphs - calculate available space.
187 * @c: UBIFS file-system description object
189 * This function returns the number of orphans that can be written in the
192 static int avail_orphs(struct ubifs_info *c)
194 int avail_lebs, avail, gap;
196 avail_lebs = c->orph_lebs - (c->ohead_lnum - c->orph_first) - 1;
198 ((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
199 gap = c->leb_size - c->ohead_offs;
200 if (gap >= UBIFS_ORPH_NODE_SZ + sizeof(__le64))
201 avail += (gap - UBIFS_ORPH_NODE_SZ) / sizeof(__le64);
206 * tot_avail_orphs - calculate total space.
207 * @c: UBIFS file-system description object
209 * This function returns the number of orphans that can be written in half
210 * the total space. That leaves half the space for adding new orphans.
212 static int tot_avail_orphs(struct ubifs_info *c)
214 int avail_lebs, avail;
216 avail_lebs = c->orph_lebs;
218 ((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
223 * do_write_orph_node - write a node to the orphan head.
224 * @c: UBIFS file-system description object
225 * @len: length of node
226 * @atomic: write atomically
228 * This function writes a node to the orphan head from the orphan buffer. If
229 * %atomic is not zero, then the write is done atomically. On success, %0 is
230 * returned, otherwise a negative error code is returned.
232 static int do_write_orph_node(struct ubifs_info *c, int len, int atomic)
237 ubifs_assert(c->ohead_offs == 0);
238 ubifs_prepare_node(c, c->orph_buf, len, 1);
239 len = ALIGN(len, c->min_io_size);
240 err = ubifs_leb_change(c, c->ohead_lnum, c->orph_buf, len);
242 if (c->ohead_offs == 0) {
243 /* Ensure LEB has been unmapped */
244 err = ubifs_leb_unmap(c, c->ohead_lnum);
248 err = ubifs_write_node(c, c->orph_buf, len, c->ohead_lnum,
255 * write_orph_node - write an orphan node.
256 * @c: UBIFS file-system description object
257 * @atomic: write atomically
259 * This function builds an orphan node from the cnext list and writes it to the
260 * orphan head. On success, %0 is returned, otherwise a negative error code
263 static int write_orph_node(struct ubifs_info *c, int atomic)
265 struct ubifs_orphan *orphan, *cnext;
266 struct ubifs_orph_node *orph;
267 int gap, err, len, cnt, i;
269 ubifs_assert(c->cmt_orphans > 0);
270 gap = c->leb_size - c->ohead_offs;
271 if (gap < UBIFS_ORPH_NODE_SZ + sizeof(__le64)) {
275 if (c->ohead_lnum > c->orph_last) {
277 * We limit the number of orphans so that this should
280 ubifs_err(c, "out of space in orphan area");
284 cnt = (gap - UBIFS_ORPH_NODE_SZ) / sizeof(__le64);
285 if (cnt > c->cmt_orphans)
286 cnt = c->cmt_orphans;
287 len = UBIFS_ORPH_NODE_SZ + cnt * sizeof(__le64);
288 ubifs_assert(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++) {
295 ubifs_assert(orphan->cmt);
296 orph->inos[i] = cpu_to_le64(orphan->inum);
298 cnext = orphan->cnext;
299 orphan->cnext = NULL;
301 c->orph_cnext = cnext;
302 c->cmt_orphans -= cnt;
303 spin_unlock(&c->orphan_lock);
305 orph->cmt_no = cpu_to_le64(c->cmt_no);
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 c->ohead_offs += ALIGN(len, c->min_io_size);
314 c->ohead_offs = ALIGN(c->ohead_offs, 8);
319 * write_orph_nodes - write orphan nodes until there are no more to commit.
320 * @c: UBIFS file-system description object
321 * @atomic: write atomically
323 * This function writes orphan nodes for all the orphans to commit. On success,
324 * %0 is returned, otherwise a negative error code is returned.
326 static int write_orph_nodes(struct ubifs_info *c, int atomic)
330 while (c->cmt_orphans > 0) {
331 err = write_orph_node(c, atomic);
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);
349 * consolidate - consolidate the orphan area.
350 * @c: UBIFS file-system description object
352 * This function enables consolidation by putting all the orphans into the list
353 * to commit. The list is in the order that the orphans were added, and the
354 * LEBs are written atomically in order, so at no time can orphans be lost by
355 * an unclean unmount.
357 * This function returns %0 on success and a negative error code on failure.
359 static int consolidate(struct ubifs_info *c)
361 int tot_avail = tot_avail_orphs(c), err = 0;
363 spin_lock(&c->orphan_lock);
364 dbg_cmt("there is space for %d orphans and there are %d",
365 tot_avail, c->tot_orphans);
366 if (c->tot_orphans - c->new_orphans <= tot_avail) {
367 struct ubifs_orphan *orphan, **last;
370 /* Change the cnext list to include all non-new orphans */
371 last = &c->orph_cnext;
372 list_for_each_entry(orphan, &c->orph_list, list) {
377 last = &orphan->cnext;
381 ubifs_assert(cnt == c->tot_orphans - c->new_orphans);
382 c->cmt_orphans = cnt;
383 c->ohead_lnum = c->orph_first;
387 * We limit the number of orphans so that this should
390 ubifs_err(c, "out of space in orphan area");
393 spin_unlock(&c->orphan_lock);
398 * commit_orphans - commit orphans.
399 * @c: UBIFS file-system description object
401 * This function commits orphans to flash. On success, %0 is returned,
402 * otherwise a negative error code is returned.
404 static int commit_orphans(struct ubifs_info *c)
406 int avail, atomic = 0, err;
408 ubifs_assert(c->cmt_orphans > 0);
409 avail = avail_orphs(c);
410 if (avail < c->cmt_orphans) {
411 /* Not enough space to write new orphans, so consolidate */
412 err = consolidate(c);
417 err = write_orph_nodes(c, atomic);
422 * erase_deleted - erase the orphans marked for deletion.
423 * @c: UBIFS file-system description object
425 * During commit, the orphans being committed cannot be deleted, so they are
426 * marked for deletion and deleted by this function. Also, the recovery
427 * adds killed orphans to the deletion list, and therefore they are deleted
430 static void erase_deleted(struct ubifs_info *c)
432 struct ubifs_orphan *orphan, *dnext;
434 spin_lock(&c->orphan_lock);
435 dnext = c->orph_dnext;
438 dnext = orphan->dnext;
439 ubifs_assert(!orphan->new);
440 ubifs_assert(orphan->del);
441 rb_erase(&orphan->rb, &c->orph_tree);
442 list_del(&orphan->list);
444 dbg_gen("deleting orphan ino %lu", (unsigned long)orphan->inum);
447 c->orph_dnext = NULL;
448 spin_unlock(&c->orphan_lock);
452 * ubifs_orphan_end_commit - end commit of orphans.
453 * @c: UBIFS file-system description object
455 * End commit of orphans.
457 int ubifs_orphan_end_commit(struct ubifs_info *c)
461 if (c->cmt_orphans != 0) {
462 err = commit_orphans(c);
467 err = dbg_check_orphans(c);
472 * ubifs_clear_orphans - erase all LEBs used for orphans.
473 * @c: UBIFS file-system description object
475 * If recovery is not required, then the orphans from the previous session
476 * are not needed. This function locates the LEBs used to record
477 * orphans, and un-maps them.
479 int ubifs_clear_orphans(struct ubifs_info *c)
483 for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
484 err = ubifs_leb_unmap(c, lnum);
488 c->ohead_lnum = c->orph_first;
494 * insert_dead_orphan - insert an orphan.
495 * @c: UBIFS file-system description object
496 * @inum: orphan inode number
498 * This function is a helper to the 'do_kill_orphans()' function. The orphan
499 * must be kept until the next commit, so it is added to the rb-tree and the
502 static int insert_dead_orphan(struct ubifs_info *c, ino_t inum)
504 struct ubifs_orphan *orphan, *o;
505 struct rb_node **p, *parent = NULL;
507 orphan = kzalloc(sizeof(struct ubifs_orphan), GFP_KERNEL);
512 p = &c->orph_tree.rb_node;
515 o = rb_entry(parent, struct ubifs_orphan, rb);
518 else if (inum > o->inum)
521 /* Already added - no problem */
527 rb_link_node(&orphan->rb, parent, p);
528 rb_insert_color(&orphan->rb, &c->orph_tree);
529 list_add_tail(&orphan->list, &c->orph_list);
531 orphan->dnext = c->orph_dnext;
532 c->orph_dnext = orphan;
533 dbg_mnt("ino %lu, new %d, tot %d", (unsigned long)inum,
534 c->new_orphans, c->tot_orphans);
539 * do_kill_orphans - remove orphan inodes from the index.
540 * @c: UBIFS file-system description object
542 * @last_cmt_no: cmt_no of last orphan node read is passed and returned here
543 * @outofdate: whether the LEB is out of date is returned here
544 * @last_flagged: whether the end orphan node is encountered
546 * This function is a helper to the 'kill_orphans()' function. It goes through
547 * every orphan node in a LEB and for every inode number recorded, removes
548 * all keys for that inode from the TNC.
550 static int do_kill_orphans(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
551 unsigned long long *last_cmt_no, int *outofdate,
554 struct ubifs_scan_node *snod;
555 struct ubifs_orph_node *orph;
556 unsigned long long cmt_no;
558 int i, n, err, first = 1;
560 list_for_each_entry(snod, &sleb->nodes, list) {
561 if (snod->type != UBIFS_ORPH_NODE) {
562 ubifs_err(c, "invalid node type %d in orphan area at %d:%d",
563 snod->type, sleb->lnum, snod->offs);
564 ubifs_dump_node(c, snod->node);
570 /* Check commit number */
571 cmt_no = le64_to_cpu(orph->cmt_no) & LLONG_MAX;
573 * The commit number on the master node may be less, because
574 * of a failed commit. If there are several failed commits in a
575 * row, the commit number written on orphan nodes will continue
576 * to increase (because the commit number is adjusted here) even
577 * though the commit number on the master node stays the same
578 * because the master node has not been re-written.
580 if (cmt_no > c->cmt_no)
582 if (cmt_no < *last_cmt_no && *last_flagged) {
584 * The last orphan node had a higher commit number and
585 * was flagged as the last written for that commit
586 * number. That makes this orphan node, out of date.
589 ubifs_err(c, "out of order commit number %llu in orphan node at %d:%d",
590 cmt_no, sleb->lnum, snod->offs);
591 ubifs_dump_node(c, snod->node);
594 dbg_rcvry("out of date LEB %d", sleb->lnum);
602 n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
603 for (i = 0; i < n; i++) {
604 inum = le64_to_cpu(orph->inos[i]);
605 dbg_rcvry("deleting orphaned inode %lu",
606 (unsigned long)inum);
607 err = ubifs_tnc_remove_ino(c, inum);
610 err = insert_dead_orphan(c, inum);
615 *last_cmt_no = cmt_no;
616 if (le64_to_cpu(orph->cmt_no) & (1ULL << 63)) {
617 dbg_rcvry("last orph node for commit %llu at %d:%d",
618 cmt_no, sleb->lnum, snod->offs);
628 * kill_orphans - remove all orphan inodes from the index.
629 * @c: UBIFS file-system description object
631 * If recovery is required, then orphan inodes recorded during the previous
632 * session (which ended with an unclean unmount) must be deleted from the index.
633 * This is done by updating the TNC, but since the index is not updated until
634 * the next commit, the LEBs where the orphan information is recorded are not
635 * erased until the next commit.
637 static int kill_orphans(struct ubifs_info *c)
639 unsigned long long last_cmt_no = 0;
640 int lnum, err = 0, outofdate = 0, last_flagged = 0;
642 c->ohead_lnum = c->orph_first;
644 /* Check no-orphans flag and skip this if no orphans */
646 dbg_rcvry("no orphans");
650 * Orph nodes always start at c->orph_first and are written to each
651 * successive LEB in turn. Generally unused LEBs will have been unmapped
652 * but may contain out of date orphan nodes if the unmap didn't go
653 * through. In addition, the last orphan node written for each commit is
654 * marked (top bit of orph->cmt_no is set to 1). It is possible that
655 * there are orphan nodes from the next commit (i.e. the commit did not
656 * complete successfully). In that case, no orphans will have been lost
657 * due to the way that orphans are written, and any orphans added will
658 * be valid orphans anyway and so can be deleted.
660 for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
661 struct ubifs_scan_leb *sleb;
663 dbg_rcvry("LEB %d", lnum);
664 sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
666 if (PTR_ERR(sleb) == -EUCLEAN)
667 sleb = ubifs_recover_leb(c, lnum, 0,
674 err = do_kill_orphans(c, sleb, &last_cmt_no, &outofdate,
676 if (err || outofdate) {
677 ubifs_scan_destroy(sleb);
681 c->ohead_lnum = lnum;
682 c->ohead_offs = sleb->endpt;
684 ubifs_scan_destroy(sleb);
690 * ubifs_mount_orphans - delete orphan inodes and erase LEBs that recorded them.
691 * @c: UBIFS file-system description object
692 * @unclean: indicates recovery from unclean unmount
693 * @read_only: indicates read only mount
695 * This function is called when mounting to erase orphans from the previous
696 * session. If UBIFS was not unmounted cleanly, then the inodes recorded as
697 * orphans are deleted.
699 int ubifs_mount_orphans(struct ubifs_info *c, int unclean, int read_only)
703 c->max_orphans = tot_avail_orphs(c);
706 c->orph_buf = vmalloc(c->leb_size);
712 err = kill_orphans(c);
714 err = ubifs_clear_orphans(c);
720 * Everything below is related to debugging.
723 struct check_orphan {
729 unsigned long last_ino;
730 unsigned long tot_inos;
731 unsigned long missing;
732 unsigned long long leaf_cnt;
733 struct ubifs_ino_node *node;
737 static int dbg_find_orphan(struct ubifs_info *c, ino_t inum)
739 struct ubifs_orphan *o;
742 spin_lock(&c->orphan_lock);
743 p = c->orph_tree.rb_node;
745 o = rb_entry(p, struct ubifs_orphan, rb);
748 else if (inum > o->inum)
751 spin_unlock(&c->orphan_lock);
755 spin_unlock(&c->orphan_lock);
759 static int dbg_ins_check_orphan(struct rb_root *root, ino_t inum)
761 struct check_orphan *orphan, *o;
762 struct rb_node **p, *parent = NULL;
764 orphan = kzalloc(sizeof(struct check_orphan), GFP_NOFS);
772 o = rb_entry(parent, struct check_orphan, rb);
775 else if (inum > o->inum)
782 rb_link_node(&orphan->rb, parent, p);
783 rb_insert_color(&orphan->rb, root);
787 static int dbg_find_check_orphan(struct rb_root *root, ino_t inum)
789 struct check_orphan *o;
794 o = rb_entry(p, struct check_orphan, rb);
797 else if (inum > o->inum)
805 static void dbg_free_check_tree(struct rb_root *root)
807 struct check_orphan *o, *n;
809 rbtree_postorder_for_each_entry_safe(o, n, root, rb)
813 static int dbg_orphan_check(struct ubifs_info *c, struct ubifs_zbranch *zbr,
816 struct check_info *ci = priv;
820 inum = key_inum(c, &zbr->key);
821 if (inum != ci->last_ino) {
822 /* Lowest node type is the inode node, so it comes first */
823 if (key_type(c, &zbr->key) != UBIFS_INO_KEY)
824 ubifs_err(c, "found orphan node ino %lu, type %d",
825 (unsigned long)inum, key_type(c, &zbr->key));
828 err = ubifs_tnc_read_node(c, zbr, ci->node);
830 ubifs_err(c, "node read failed, error %d", err);
833 if (ci->node->nlink == 0)
834 /* Must be recorded as an orphan */
835 if (!dbg_find_check_orphan(&ci->root, inum) &&
836 !dbg_find_orphan(c, inum)) {
837 ubifs_err(c, "missing orphan, ino %lu",
838 (unsigned long)inum);
846 static int dbg_read_orphans(struct check_info *ci, struct ubifs_scan_leb *sleb)
848 struct ubifs_scan_node *snod;
849 struct ubifs_orph_node *orph;
853 list_for_each_entry(snod, &sleb->nodes, list) {
855 if (snod->type != UBIFS_ORPH_NODE)
858 n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
859 for (i = 0; i < n; i++) {
860 inum = le64_to_cpu(orph->inos[i]);
861 err = dbg_ins_check_orphan(&ci->root, inum);
869 static int dbg_scan_orphans(struct ubifs_info *c, struct check_info *ci)
874 /* Check no-orphans flag and skip this if no orphans */
878 buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
880 ubifs_err(c, "cannot allocate memory to check orphans");
884 for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
885 struct ubifs_scan_leb *sleb;
887 sleb = ubifs_scan(c, lnum, 0, buf, 0);
893 err = dbg_read_orphans(ci, sleb);
894 ubifs_scan_destroy(sleb);
903 static int dbg_check_orphans(struct ubifs_info *c)
905 struct check_info ci;
908 if (!dbg_is_chk_orph(c))
916 ci.node = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
918 ubifs_err(c, "out of memory");
922 err = dbg_scan_orphans(c, &ci);
926 err = dbg_walk_index(c, &dbg_orphan_check, NULL, &ci);
928 ubifs_err(c, "cannot scan TNC, error %d", err);
933 ubifs_err(c, "%lu missing orphan(s)", ci.missing);
938 dbg_cmt("last inode number is %lu", ci.last_ino);
939 dbg_cmt("total number of inodes is %lu", ci.tot_inos);
940 dbg_cmt("total number of leaf nodes is %llu", ci.leaf_cnt);
943 dbg_free_check_tree(&ci.root);