1 // SPDX-License-Identifier: GPL-2.0+
3 * This file is part of UBIFS.
5 * Copyright (C) 2006-2008 Nokia Corporation.
7 * Author: Adrian Hunter
11 #include <dm/devres.h>
12 #include <linux/err.h>
16 * An orphan is an inode number whose inode node has been committed to the index
17 * with a link count of zero. That happens when an open file is deleted
18 * (unlinked) and then a commit is run. In the normal course of events the inode
19 * would be deleted when the file is closed. However in the case of an unclean
20 * unmount, orphans need to be accounted for. After an unclean unmount, the
21 * orphans' inodes must be deleted which means either scanning the entire index
22 * looking for them, or keeping a list on flash somewhere. This unit implements
23 * the latter approach.
25 * The orphan area is a fixed number of LEBs situated between the LPT area and
26 * the main area. The number of orphan area LEBs is specified when the file
27 * system is created. The minimum number is 1. The size of the orphan area
28 * should be so that it can hold the maximum number of orphans that are expected
29 * to ever exist at one time.
31 * The number of orphans that can fit in a LEB is:
33 * (c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64)
35 * For example: a 15872 byte LEB can fit 1980 orphans so 1 LEB may be enough.
37 * Orphans are accumulated in a rb-tree. When an inode's link count drops to
38 * zero, the inode number is added to the rb-tree. It is removed from the tree
39 * when the inode is deleted. Any new orphans that are in the orphan tree when
40 * the commit is run, are written to the orphan area in 1 or more orphan nodes.
41 * If the orphan area is full, it is consolidated to make space. There is
42 * always enough space because validation prevents the user from creating more
43 * than the maximum number of orphans allowed.
46 static int dbg_check_orphans(struct ubifs_info *c);
49 * ubifs_add_orphan - add an orphan.
50 * @c: UBIFS file-system description object
51 * @inum: orphan inode number
53 * Add an orphan. This function is called when an inodes link count drops to
56 int ubifs_add_orphan(struct ubifs_info *c, ino_t inum)
58 struct ubifs_orphan *orphan, *o;
59 struct rb_node **p, *parent = NULL;
61 orphan = kzalloc(sizeof(struct ubifs_orphan), GFP_NOFS);
67 spin_lock(&c->orphan_lock);
68 if (c->tot_orphans >= c->max_orphans) {
69 spin_unlock(&c->orphan_lock);
73 p = &c->orph_tree.rb_node;
76 o = rb_entry(parent, struct ubifs_orphan, rb);
79 else if (inum > o->inum)
82 ubifs_err(c, "orphaned twice");
83 spin_unlock(&c->orphan_lock);
90 rb_link_node(&orphan->rb, parent, p);
91 rb_insert_color(&orphan->rb, &c->orph_tree);
92 list_add_tail(&orphan->list, &c->orph_list);
93 list_add_tail(&orphan->new_list, &c->orph_new);
94 spin_unlock(&c->orphan_lock);
95 dbg_gen("ino %lu", (unsigned long)inum);
100 * ubifs_delete_orphan - delete an orphan.
101 * @c: UBIFS file-system description object
102 * @inum: orphan inode number
104 * Delete an orphan. This function is called when an inode is deleted.
106 void ubifs_delete_orphan(struct ubifs_info *c, ino_t inum)
108 struct ubifs_orphan *o;
111 spin_lock(&c->orphan_lock);
112 p = c->orph_tree.rb_node;
114 o = rb_entry(p, struct ubifs_orphan, rb);
117 else if (inum > o->inum)
121 spin_unlock(&c->orphan_lock);
122 dbg_gen("deleted twice ino %lu",
123 (unsigned long)inum);
128 o->dnext = c->orph_dnext;
130 spin_unlock(&c->orphan_lock);
131 dbg_gen("delete later ino %lu",
132 (unsigned long)inum);
135 rb_erase(p, &c->orph_tree);
139 list_del(&o->new_list);
142 spin_unlock(&c->orphan_lock);
144 dbg_gen("inum %lu", (unsigned long)inum);
148 spin_unlock(&c->orphan_lock);
149 ubifs_err(c, "missing orphan ino %lu", (unsigned long)inum);
154 * ubifs_orphan_start_commit - start commit of orphans.
155 * @c: UBIFS file-system description object
157 * Start commit of orphans.
159 int ubifs_orphan_start_commit(struct ubifs_info *c)
161 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(orphan->new);
167 ubifs_assert(!orphan->cmt);
171 last = &orphan->cnext;
174 c->cmt_orphans = c->new_orphans;
176 dbg_cmt("%d orphans to commit", c->cmt_orphans);
177 INIT_LIST_HEAD(&c->orph_new);
178 if (c->tot_orphans == 0)
182 spin_unlock(&c->orphan_lock);
187 * avail_orphs - calculate available space.
188 * @c: UBIFS file-system description object
190 * This function returns the number of orphans that can be written in the
193 static int avail_orphs(struct ubifs_info *c)
195 int avail_lebs, avail, gap;
197 avail_lebs = c->orph_lebs - (c->ohead_lnum - c->orph_first) - 1;
199 ((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
200 gap = c->leb_size - c->ohead_offs;
201 if (gap >= UBIFS_ORPH_NODE_SZ + sizeof(__le64))
202 avail += (gap - UBIFS_ORPH_NODE_SZ) / sizeof(__le64);
207 * tot_avail_orphs - calculate total space.
208 * @c: UBIFS file-system description object
210 * This function returns the number of orphans that can be written in half
211 * the total space. That leaves half the space for adding new orphans.
213 static int tot_avail_orphs(struct ubifs_info *c)
215 int avail_lebs, avail;
217 avail_lebs = c->orph_lebs;
219 ((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
224 * do_write_orph_node - write a node to the orphan head.
225 * @c: UBIFS file-system description object
226 * @len: length of node
227 * @atomic: write atomically
229 * This function writes a node to the orphan head from the orphan buffer. If
230 * %atomic is not zero, then the write is done atomically. On success, %0 is
231 * returned, otherwise a negative error code is returned.
233 static int do_write_orph_node(struct ubifs_info *c, int len, int atomic)
238 ubifs_assert(c->ohead_offs == 0);
239 ubifs_prepare_node(c, c->orph_buf, len, 1);
240 len = ALIGN(len, c->min_io_size);
241 err = ubifs_leb_change(c, c->ohead_lnum, c->orph_buf, len);
243 if (c->ohead_offs == 0) {
244 /* Ensure LEB has been unmapped */
245 err = ubifs_leb_unmap(c, c->ohead_lnum);
249 err = ubifs_write_node(c, c->orph_buf, len, c->ohead_lnum,
256 * write_orph_node - write an orphan node.
257 * @c: UBIFS file-system description object
258 * @atomic: write atomically
260 * This function builds an orphan node from the cnext list and writes it to the
261 * orphan head. On success, %0 is returned, otherwise a negative error code
264 static int write_orph_node(struct ubifs_info *c, int atomic)
266 struct ubifs_orphan *orphan, *cnext;
267 struct ubifs_orph_node *orph;
268 int gap, err, len, cnt, i;
270 ubifs_assert(c->cmt_orphans > 0);
271 gap = c->leb_size - c->ohead_offs;
272 if (gap < UBIFS_ORPH_NODE_SZ + sizeof(__le64)) {
276 if (c->ohead_lnum > c->orph_last) {
278 * We limit the number of orphans so that this should
281 ubifs_err(c, "out of space in orphan area");
285 cnt = (gap - UBIFS_ORPH_NODE_SZ) / sizeof(__le64);
286 if (cnt > c->cmt_orphans)
287 cnt = c->cmt_orphans;
288 len = UBIFS_ORPH_NODE_SZ + cnt * sizeof(__le64);
289 ubifs_assert(c->orph_buf);
291 orph->ch.node_type = UBIFS_ORPH_NODE;
292 spin_lock(&c->orphan_lock);
293 cnext = c->orph_cnext;
294 for (i = 0; i < cnt; i++) {
296 ubifs_assert(orphan->cmt);
297 orph->inos[i] = cpu_to_le64(orphan->inum);
299 cnext = orphan->cnext;
300 orphan->cnext = NULL;
302 c->orph_cnext = cnext;
303 c->cmt_orphans -= cnt;
304 spin_unlock(&c->orphan_lock);
306 orph->cmt_no = cpu_to_le64(c->cmt_no);
308 /* Mark the last node of the commit */
309 orph->cmt_no = cpu_to_le64((c->cmt_no) | (1ULL << 63));
310 ubifs_assert(c->ohead_offs + len <= c->leb_size);
311 ubifs_assert(c->ohead_lnum >= c->orph_first);
312 ubifs_assert(c->ohead_lnum <= c->orph_last);
313 err = do_write_orph_node(c, len, atomic);
314 c->ohead_offs += ALIGN(len, c->min_io_size);
315 c->ohead_offs = ALIGN(c->ohead_offs, 8);
320 * write_orph_nodes - write orphan nodes until there are no more to commit.
321 * @c: UBIFS file-system description object
322 * @atomic: write atomically
324 * This function writes orphan nodes for all the orphans to commit. On success,
325 * %0 is returned, otherwise a negative error code is returned.
327 static int write_orph_nodes(struct ubifs_info *c, int atomic)
331 while (c->cmt_orphans > 0) {
332 err = write_orph_node(c, atomic);
339 /* Unmap any unused LEBs after consolidation */
340 for (lnum = c->ohead_lnum + 1; lnum <= c->orph_last; lnum++) {
341 err = ubifs_leb_unmap(c, lnum);
350 * consolidate - consolidate the orphan area.
351 * @c: UBIFS file-system description object
353 * This function enables consolidation by putting all the orphans into the list
354 * to commit. The list is in the order that the orphans were added, and the
355 * LEBs are written atomically in order, so at no time can orphans be lost by
356 * an unclean unmount.
358 * This function returns %0 on success and a negative error code on failure.
360 static int consolidate(struct ubifs_info *c)
362 int tot_avail = tot_avail_orphs(c), err = 0;
364 spin_lock(&c->orphan_lock);
365 dbg_cmt("there is space for %d orphans and there are %d",
366 tot_avail, c->tot_orphans);
367 if (c->tot_orphans - c->new_orphans <= tot_avail) {
368 struct ubifs_orphan *orphan, **last;
371 /* Change the cnext list to include all non-new orphans */
372 last = &c->orph_cnext;
373 list_for_each_entry(orphan, &c->orph_list, list) {
378 last = &orphan->cnext;
382 ubifs_assert(cnt == c->tot_orphans - c->new_orphans);
383 c->cmt_orphans = cnt;
384 c->ohead_lnum = c->orph_first;
388 * We limit the number of orphans so that this should
391 ubifs_err(c, "out of space in orphan area");
394 spin_unlock(&c->orphan_lock);
399 * commit_orphans - commit orphans.
400 * @c: UBIFS file-system description object
402 * This function commits orphans to flash. On success, %0 is returned,
403 * otherwise a negative error code is returned.
405 static int commit_orphans(struct ubifs_info *c)
407 int avail, atomic = 0, err;
409 ubifs_assert(c->cmt_orphans > 0);
410 avail = avail_orphs(c);
411 if (avail < c->cmt_orphans) {
412 /* Not enough space to write new orphans, so consolidate */
413 err = consolidate(c);
418 err = write_orph_nodes(c, atomic);
423 * erase_deleted - erase the orphans marked for deletion.
424 * @c: UBIFS file-system description object
426 * During commit, the orphans being committed cannot be deleted, so they are
427 * marked for deletion and deleted by this function. Also, the recovery
428 * adds killed orphans to the deletion list, and therefore they are deleted
431 static void erase_deleted(struct ubifs_info *c)
433 struct ubifs_orphan *orphan, *dnext;
435 spin_lock(&c->orphan_lock);
436 dnext = c->orph_dnext;
439 dnext = orphan->dnext;
440 ubifs_assert(!orphan->new);
441 ubifs_assert(orphan->del);
442 rb_erase(&orphan->rb, &c->orph_tree);
443 list_del(&orphan->list);
445 dbg_gen("deleting orphan ino %lu", (unsigned long)orphan->inum);
448 c->orph_dnext = NULL;
449 spin_unlock(&c->orphan_lock);
453 * ubifs_orphan_end_commit - end commit of orphans.
454 * @c: UBIFS file-system description object
456 * End commit of orphans.
458 int ubifs_orphan_end_commit(struct ubifs_info *c)
462 if (c->cmt_orphans != 0) {
463 err = commit_orphans(c);
468 err = dbg_check_orphans(c);
473 * ubifs_clear_orphans - erase all LEBs used for orphans.
474 * @c: UBIFS file-system description object
476 * If recovery is not required, then the orphans from the previous session
477 * are not needed. This function locates the LEBs used to record
478 * orphans, and un-maps them.
480 int ubifs_clear_orphans(struct ubifs_info *c)
484 for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
485 err = ubifs_leb_unmap(c, lnum);
489 c->ohead_lnum = c->orph_first;
495 * insert_dead_orphan - insert an orphan.
496 * @c: UBIFS file-system description object
497 * @inum: orphan inode number
499 * This function is a helper to the 'do_kill_orphans()' function. The orphan
500 * must be kept until the next commit, so it is added to the rb-tree and the
503 static int insert_dead_orphan(struct ubifs_info *c, ino_t inum)
505 struct ubifs_orphan *orphan, *o;
506 struct rb_node **p, *parent = NULL;
508 orphan = kzalloc(sizeof(struct ubifs_orphan), GFP_KERNEL);
513 p = &c->orph_tree.rb_node;
516 o = rb_entry(parent, struct ubifs_orphan, rb);
519 else if (inum > o->inum)
522 /* Already added - no problem */
528 rb_link_node(&orphan->rb, parent, p);
529 rb_insert_color(&orphan->rb, &c->orph_tree);
530 list_add_tail(&orphan->list, &c->orph_list);
532 orphan->dnext = c->orph_dnext;
533 c->orph_dnext = orphan;
534 dbg_mnt("ino %lu, new %d, tot %d", (unsigned long)inum,
535 c->new_orphans, c->tot_orphans);
540 * do_kill_orphans - remove orphan inodes from the index.
541 * @c: UBIFS file-system description object
543 * @last_cmt_no: cmt_no of last orphan node read is passed and returned here
544 * @outofdate: whether the LEB is out of date is returned here
545 * @last_flagged: whether the end orphan node is encountered
547 * This function is a helper to the 'kill_orphans()' function. It goes through
548 * every orphan node in a LEB and for every inode number recorded, removes
549 * all keys for that inode from the TNC.
551 static int do_kill_orphans(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
552 unsigned long long *last_cmt_no, int *outofdate,
555 struct ubifs_scan_node *snod;
556 struct ubifs_orph_node *orph;
557 unsigned long long cmt_no;
559 int i, n, err, first = 1;
561 list_for_each_entry(snod, &sleb->nodes, list) {
562 if (snod->type != UBIFS_ORPH_NODE) {
563 ubifs_err(c, "invalid node type %d in orphan area at %d:%d",
564 snod->type, sleb->lnum, snod->offs);
565 ubifs_dump_node(c, snod->node);
571 /* Check commit number */
572 cmt_no = le64_to_cpu(orph->cmt_no) & LLONG_MAX;
574 * The commit number on the master node may be less, because
575 * of a failed commit. If there are several failed commits in a
576 * row, the commit number written on orphan nodes will continue
577 * to increase (because the commit number is adjusted here) even
578 * though the commit number on the master node stays the same
579 * because the master node has not been re-written.
581 if (cmt_no > c->cmt_no)
583 if (cmt_no < *last_cmt_no && *last_flagged) {
585 * The last orphan node had a higher commit number and
586 * was flagged as the last written for that commit
587 * number. That makes this orphan node, out of date.
590 ubifs_err(c, "out of order commit number %llu in orphan node at %d:%d",
591 cmt_no, sleb->lnum, snod->offs);
592 ubifs_dump_node(c, snod->node);
595 dbg_rcvry("out of date LEB %d", sleb->lnum);
603 n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
604 for (i = 0; i < n; i++) {
605 inum = le64_to_cpu(orph->inos[i]);
606 dbg_rcvry("deleting orphaned inode %lu",
607 (unsigned long)inum);
608 err = ubifs_tnc_remove_ino(c, inum);
611 err = insert_dead_orphan(c, inum);
616 *last_cmt_no = cmt_no;
617 if (le64_to_cpu(orph->cmt_no) & (1ULL << 63)) {
618 dbg_rcvry("last orph node for commit %llu at %d:%d",
619 cmt_no, sleb->lnum, snod->offs);
629 * kill_orphans - remove all orphan inodes from the index.
630 * @c: UBIFS file-system description object
632 * If recovery is required, then orphan inodes recorded during the previous
633 * session (which ended with an unclean unmount) must be deleted from the index.
634 * This is done by updating the TNC, but since the index is not updated until
635 * the next commit, the LEBs where the orphan information is recorded are not
636 * erased until the next commit.
638 static int kill_orphans(struct ubifs_info *c)
640 unsigned long long last_cmt_no = 0;
641 int lnum, err = 0, outofdate = 0, last_flagged = 0;
643 c->ohead_lnum = c->orph_first;
645 /* Check no-orphans flag and skip this if no orphans */
647 dbg_rcvry("no orphans");
651 * Orph nodes always start at c->orph_first and are written to each
652 * successive LEB in turn. Generally unused LEBs will have been unmapped
653 * but may contain out of date orphan nodes if the unmap didn't go
654 * through. In addition, the last orphan node written for each commit is
655 * marked (top bit of orph->cmt_no is set to 1). It is possible that
656 * there are orphan nodes from the next commit (i.e. the commit did not
657 * complete successfully). In that case, no orphans will have been lost
658 * due to the way that orphans are written, and any orphans added will
659 * be valid orphans anyway and so can be deleted.
661 for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
662 struct ubifs_scan_leb *sleb;
664 dbg_rcvry("LEB %d", lnum);
665 sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
667 if (PTR_ERR(sleb) == -EUCLEAN)
668 sleb = ubifs_recover_leb(c, lnum, 0,
675 err = do_kill_orphans(c, sleb, &last_cmt_no, &outofdate,
677 if (err || outofdate) {
678 ubifs_scan_destroy(sleb);
682 c->ohead_lnum = lnum;
683 c->ohead_offs = sleb->endpt;
685 ubifs_scan_destroy(sleb);
691 * ubifs_mount_orphans - delete orphan inodes and erase LEBs that recorded them.
692 * @c: UBIFS file-system description object
693 * @unclean: indicates recovery from unclean unmount
694 * @read_only: indicates read only mount
696 * This function is called when mounting to erase orphans from the previous
697 * session. If UBIFS was not unmounted cleanly, then the inodes recorded as
698 * orphans are deleted.
700 int ubifs_mount_orphans(struct ubifs_info *c, int unclean, int read_only)
704 c->max_orphans = tot_avail_orphs(c);
707 c->orph_buf = vmalloc(c->leb_size);
713 err = kill_orphans(c);
715 err = ubifs_clear_orphans(c);
721 * Everything below is related to debugging.
724 struct check_orphan {
730 unsigned long last_ino;
731 unsigned long tot_inos;
732 unsigned long missing;
733 unsigned long long leaf_cnt;
734 struct ubifs_ino_node *node;
738 static int dbg_find_orphan(struct ubifs_info *c, ino_t inum)
740 struct ubifs_orphan *o;
743 spin_lock(&c->orphan_lock);
744 p = c->orph_tree.rb_node;
746 o = rb_entry(p, struct ubifs_orphan, rb);
749 else if (inum > o->inum)
752 spin_unlock(&c->orphan_lock);
756 spin_unlock(&c->orphan_lock);
760 static int dbg_ins_check_orphan(struct rb_root *root, ino_t inum)
762 struct check_orphan *orphan, *o;
763 struct rb_node **p, *parent = NULL;
765 orphan = kzalloc(sizeof(struct check_orphan), GFP_NOFS);
773 o = rb_entry(parent, struct check_orphan, rb);
776 else if (inum > o->inum)
783 rb_link_node(&orphan->rb, parent, p);
784 rb_insert_color(&orphan->rb, root);
788 static int dbg_find_check_orphan(struct rb_root *root, ino_t inum)
790 struct check_orphan *o;
795 o = rb_entry(p, struct check_orphan, rb);
798 else if (inum > o->inum)
806 static void dbg_free_check_tree(struct rb_root *root)
808 struct check_orphan *o, *n;
810 rbtree_postorder_for_each_entry_safe(o, n, root, rb)
814 static int dbg_orphan_check(struct ubifs_info *c, struct ubifs_zbranch *zbr,
817 struct check_info *ci = priv;
821 inum = key_inum(c, &zbr->key);
822 if (inum != ci->last_ino) {
823 /* Lowest node type is the inode node, so it comes first */
824 if (key_type(c, &zbr->key) != UBIFS_INO_KEY)
825 ubifs_err(c, "found orphan node ino %lu, type %d",
826 (unsigned long)inum, key_type(c, &zbr->key));
829 err = ubifs_tnc_read_node(c, zbr, ci->node);
831 ubifs_err(c, "node read failed, error %d", err);
834 if (ci->node->nlink == 0)
835 /* Must be recorded as an orphan */
836 if (!dbg_find_check_orphan(&ci->root, inum) &&
837 !dbg_find_orphan(c, inum)) {
838 ubifs_err(c, "missing orphan, ino %lu",
839 (unsigned long)inum);
847 static int dbg_read_orphans(struct check_info *ci, struct ubifs_scan_leb *sleb)
849 struct ubifs_scan_node *snod;
850 struct ubifs_orph_node *orph;
854 list_for_each_entry(snod, &sleb->nodes, list) {
856 if (snod->type != UBIFS_ORPH_NODE)
859 n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
860 for (i = 0; i < n; i++) {
861 inum = le64_to_cpu(orph->inos[i]);
862 err = dbg_ins_check_orphan(&ci->root, inum);
870 static int dbg_scan_orphans(struct ubifs_info *c, struct check_info *ci)
875 /* Check no-orphans flag and skip this if no orphans */
879 buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
881 ubifs_err(c, "cannot allocate memory to check orphans");
885 for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
886 struct ubifs_scan_leb *sleb;
888 sleb = ubifs_scan(c, lnum, 0, buf, 0);
894 err = dbg_read_orphans(ci, sleb);
895 ubifs_scan_destroy(sleb);
904 static int dbg_check_orphans(struct ubifs_info *c)
906 struct check_info ci;
909 if (!dbg_is_chk_orph(c))
917 ci.node = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
919 ubifs_err(c, "out of memory");
923 err = dbg_scan_orphans(c, &ci);
927 err = dbg_walk_index(c, &dbg_orphan_check, NULL, &ci);
929 ubifs_err(c, "cannot scan TNC, error %d", err);
934 ubifs_err(c, "%lu missing orphan(s)", ci.missing);
939 dbg_cmt("last inode number is %lu", ci.last_ino);
940 dbg_cmt("total number of inodes is %lu", ci.tot_inos);
941 dbg_cmt("total number of leaf nodes is %llu", ci.leaf_cnt);
944 dbg_free_check_tree(&ci.root);