LCOV - code coverage report
Current view: top level - libubifs - orphan.c (source / functions) Hit Total Coverage
Test: a simple test Lines: 103 251 41.0 %
Date: 2024-06-05 20:10:43 Functions: 7 12 58.3 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : 
      10             : #include "linux_err.h"
      11             : #include "bitops.h"
      12             : #include "kmem.h"
      13             : #include "ubifs.h"
      14             : #include "defs.h"
      15             : #include "debug.h"
      16             : #include "key.h"
      17             : #include "misc.h"
      18             : 
      19             : /*
      20             :  * An orphan is an inode number whose inode node has been committed to the index
      21             :  * with a link count of zero. That happens when an open file is deleted
      22             :  * (unlinked) and then a commit is run. In the normal course of events the inode
      23             :  * would be deleted when the file is closed. However in the case of an unclean
      24             :  * unmount, orphans need to be accounted for. After an unclean unmount, the
      25             :  * orphans' inodes must be deleted which means either scanning the entire index
      26             :  * looking for them, or keeping a list on flash somewhere. This unit implements
      27             :  * the latter approach.
      28             :  *
      29             :  * The orphan area is a fixed number of LEBs situated between the LPT area and
      30             :  * the main area. The number of orphan area LEBs is specified when the file
      31             :  * system is created. The minimum number is 1. The size of the orphan area
      32             :  * should be so that it can hold the maximum number of orphans that are expected
      33             :  * to ever exist at one time.
      34             :  *
      35             :  * The number of orphans that can fit in a LEB is:
      36             :  *
      37             :  *         (c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64)
      38             :  *
      39             :  * For example: a 15872 byte LEB can fit 1980 orphans so 1 LEB may be enough.
      40             :  *
      41             :  * Orphans are accumulated in a rb-tree. When an inode's link count drops to
      42             :  * zero, the inode number is added to the rb-tree. It is removed from the tree
      43             :  * when the inode is deleted.  Any new orphans that are in the orphan tree when
      44             :  * the commit is run, are written to the orphan area in 1 or more orphan nodes.
      45             :  * If the orphan area is full, it is consolidated to make space.  There is
      46             :  * always enough space because validation prevents the user from creating more
      47             :  * than the maximum number of orphans allowed.
      48             :  */
      49             : 
      50             : static int dbg_check_orphans(struct ubifs_info *c);
      51             : 
      52             : /**
      53             :  * ubifs_orphan_start_commit - start commit of orphans.
      54             :  * @c: UBIFS file-system description object
      55             :  *
      56             :  * Start commit of orphans.
      57             :  */
      58        3427 : int ubifs_orphan_start_commit(struct ubifs_info *c)
      59             : {
      60             :         struct ubifs_orphan *orphan, **last;
      61             : 
      62        3427 :         spin_lock(&c->orphan_lock);
      63        3427 :         last = &c->orph_cnext;
      64        3427 :         list_for_each_entry(orphan, &c->orph_new, new_list) {
      65           0 :                 ubifs_assert(c, orphan->new);
      66           0 :                 ubifs_assert(c, !orphan->cmt);
      67           0 :                 orphan->new = 0;
      68           0 :                 orphan->cmt = 1;
      69           0 :                 *last = orphan;
      70           0 :                 last = &orphan->cnext;
      71             :         }
      72        3427 :         *last = NULL;
      73        3427 :         c->cmt_orphans = c->new_orphans;
      74        3427 :         c->new_orphans = 0;
      75        3427 :         dbg_cmt("%d orphans to commit", c->cmt_orphans);
      76        6854 :         INIT_LIST_HEAD(&c->orph_new);
      77        3427 :         if (c->tot_orphans == 0)
      78        3427 :                 c->no_orphs = 1;
      79             :         else
      80           0 :                 c->no_orphs = 0;
      81        3427 :         spin_unlock(&c->orphan_lock);
      82        3427 :         return 0;
      83             : }
      84             : 
      85             : /**
      86             :  * avail_orphs - calculate available space.
      87             :  * @c: UBIFS file-system description object
      88             :  *
      89             :  * This function returns the number of orphans that can be written in the
      90             :  * available space.
      91             :  */
      92             : static int avail_orphs(struct ubifs_info *c)
      93             : {
      94             :         int avail_lebs, avail, gap;
      95             : 
      96           0 :         avail_lebs = c->orph_lebs - (c->ohead_lnum - c->orph_first) - 1;
      97           0 :         avail = avail_lebs *
      98           0 :                ((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
      99           0 :         gap = c->leb_size - c->ohead_offs;
     100           0 :         if (gap >= UBIFS_ORPH_NODE_SZ + sizeof(__le64))
     101           0 :                 avail += (gap - UBIFS_ORPH_NODE_SZ) / sizeof(__le64);
     102             :         return avail;
     103             : }
     104             : 
     105             : /**
     106             :  * tot_avail_orphs - calculate total space.
     107             :  * @c: UBIFS file-system description object
     108             :  *
     109             :  * This function returns the number of orphans that can be written in half
     110             :  * the total space. That leaves half the space for adding new orphans.
     111             :  */
     112             : static int tot_avail_orphs(struct ubifs_info *c)
     113             : {
     114             :         int avail_lebs, avail;
     115             : 
     116        2199 :         avail_lebs = c->orph_lebs;
     117        4398 :         avail = avail_lebs *
     118        2199 :                ((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
     119        2199 :         return avail / 2;
     120             : }
     121             : 
     122             : /**
     123             :  * do_write_orph_node - write a node to the orphan head.
     124             :  * @c: UBIFS file-system description object
     125             :  * @len: length of node
     126             :  * @atomic: write atomically
     127             :  *
     128             :  * This function writes a node to the orphan head from the orphan buffer. If
     129             :  * %atomic is not zero, then the write is done atomically. On success, %0 is
     130             :  * returned, otherwise a negative error code is returned.
     131             :  */
     132           0 : static int do_write_orph_node(struct ubifs_info *c, int len, int atomic)
     133             : {
     134           0 :         int err = 0;
     135             : 
     136           0 :         if (atomic) {
     137           0 :                 ubifs_assert(c, c->ohead_offs == 0);
     138           0 :                 ubifs_prepare_node(c, c->orph_buf, len, 1);
     139           0 :                 len = ALIGN(len, c->min_io_size);
     140           0 :                 err = ubifs_leb_change(c, c->ohead_lnum, c->orph_buf, len);
     141             :         } else {
     142           0 :                 if (c->ohead_offs == 0) {
     143             :                         /* Ensure LEB has been unmapped */
     144           0 :                         err = ubifs_leb_unmap(c, c->ohead_lnum);
     145           0 :                         if (err)
     146             :                                 return err;
     147             :                 }
     148           0 :                 err = ubifs_write_node(c, c->orph_buf, len, c->ohead_lnum,
     149             :                                        c->ohead_offs);
     150             :         }
     151             :         return err;
     152             : }
     153             : 
     154             : /**
     155             :  * write_orph_node - write an orphan node.
     156             :  * @c: UBIFS file-system description object
     157             :  * @atomic: write atomically
     158             :  *
     159             :  * This function builds an orphan node from the cnext list and writes it to the
     160             :  * orphan head. On success, %0 is returned, otherwise a negative error code
     161             :  * is returned.
     162             :  */
     163           0 : static int write_orph_node(struct ubifs_info *c, int atomic)
     164             : {
     165             :         struct ubifs_orphan *orphan, *cnext;
     166             :         struct ubifs_orph_node *orph;
     167             :         int gap, err, len, cnt, i;
     168             : 
     169           0 :         ubifs_assert(c, c->cmt_orphans > 0);
     170           0 :         gap = c->leb_size - c->ohead_offs;
     171           0 :         if (gap < UBIFS_ORPH_NODE_SZ + sizeof(__le64)) {
     172           0 :                 c->ohead_lnum += 1;
     173           0 :                 c->ohead_offs = 0;
     174           0 :                 gap = c->leb_size;
     175           0 :                 if (c->ohead_lnum > c->orph_last) {
     176             :                         /*
     177             :                          * We limit the number of orphans so that this should
     178             :                          * never happen.
     179             :                          */
     180           0 :                         ubifs_err(c, "out of space in orphan area");
     181             :                         return -EINVAL;
     182             :                 }
     183             :         }
     184           0 :         cnt = (gap - UBIFS_ORPH_NODE_SZ) / sizeof(__le64);
     185           0 :         if (cnt > c->cmt_orphans)
     186           0 :                 cnt = c->cmt_orphans;
     187           0 :         len = UBIFS_ORPH_NODE_SZ + cnt * sizeof(__le64);
     188           0 :         ubifs_assert(c, c->orph_buf);
     189           0 :         orph = c->orph_buf;
     190           0 :         orph->ch.node_type = UBIFS_ORPH_NODE;
     191           0 :         spin_lock(&c->orphan_lock);
     192           0 :         cnext = c->orph_cnext;
     193           0 :         for (i = 0; i < cnt; i++) {
     194           0 :                 orphan = cnext;
     195           0 :                 ubifs_assert(c, orphan->cmt);
     196           0 :                 orph->inos[i] = cpu_to_le64(orphan->inum);
     197           0 :                 orphan->cmt = 0;
     198           0 :                 cnext = orphan->cnext;
     199           0 :                 orphan->cnext = NULL;
     200             :         }
     201           0 :         c->orph_cnext = cnext;
     202           0 :         c->cmt_orphans -= cnt;
     203           0 :         spin_unlock(&c->orphan_lock);
     204           0 :         if (c->cmt_orphans)
     205           0 :                 orph->cmt_no = cpu_to_le64(c->cmt_no);
     206             :         else
     207             :                 /* Mark the last node of the commit */
     208           0 :                 orph->cmt_no = cpu_to_le64((c->cmt_no) | (1ULL << 63));
     209           0 :         ubifs_assert(c, c->ohead_offs + len <= c->leb_size);
     210           0 :         ubifs_assert(c, c->ohead_lnum >= c->orph_first);
     211           0 :         ubifs_assert(c, c->ohead_lnum <= c->orph_last);
     212           0 :         err = do_write_orph_node(c, len, atomic);
     213           0 :         c->ohead_offs += ALIGN(len, c->min_io_size);
     214           0 :         c->ohead_offs = ALIGN(c->ohead_offs, 8);
     215           0 :         return err;
     216             : }
     217             : 
     218             : /**
     219             :  * write_orph_nodes - write orphan nodes until there are no more to commit.
     220             :  * @c: UBIFS file-system description object
     221             :  * @atomic: write atomically
     222             :  *
     223             :  * This function writes orphan nodes for all the orphans to commit. On success,
     224             :  * %0 is returned, otherwise a negative error code is returned.
     225             :  */
     226           0 : static int write_orph_nodes(struct ubifs_info *c, int atomic)
     227             : {
     228             :         int err;
     229             : 
     230           0 :         while (c->cmt_orphans > 0) {
     231           0 :                 err = write_orph_node(c, atomic);
     232           0 :                 if (err)
     233             :                         return err;
     234             :         }
     235           0 :         if (atomic) {
     236             :                 int lnum;
     237             : 
     238             :                 /* Unmap any unused LEBs after consolidation */
     239           0 :                 for (lnum = c->ohead_lnum + 1; lnum <= c->orph_last; lnum++) {
     240           0 :                         err = ubifs_leb_unmap(c, lnum);
     241           0 :                         if (err)
     242             :                                 return err;
     243             :                 }
     244             :         }
     245             :         return 0;
     246             : }
     247             : 
     248             : /**
     249             :  * consolidate - consolidate the orphan area.
     250             :  * @c: UBIFS file-system description object
     251             :  *
     252             :  * This function enables consolidation by putting all the orphans into the list
     253             :  * to commit. The list is in the order that the orphans were added, and the
     254             :  * LEBs are written atomically in order, so at no time can orphans be lost by
     255             :  * an unclean unmount.
     256             :  *
     257             :  * This function returns %0 on success and a negative error code on failure.
     258             :  */
     259           0 : static int consolidate(struct ubifs_info *c)
     260             : {
     261           0 :         int tot_avail = tot_avail_orphs(c), err = 0;
     262             : 
     263           0 :         spin_lock(&c->orphan_lock);
     264           0 :         dbg_cmt("there is space for %d orphans and there are %d",
     265             :                 tot_avail, c->tot_orphans);
     266           0 :         if (c->tot_orphans - c->new_orphans <= tot_avail) {
     267             :                 struct ubifs_orphan *orphan, **last;
     268           0 :                 int cnt = 0;
     269             : 
     270             :                 /* Change the cnext list to include all non-new orphans */
     271           0 :                 last = &c->orph_cnext;
     272           0 :                 list_for_each_entry(orphan, &c->orph_list, list) {
     273           0 :                         if (orphan->new)
     274           0 :                                 continue;
     275           0 :                         orphan->cmt = 1;
     276           0 :                         *last = orphan;
     277           0 :                         last = &orphan->cnext;
     278           0 :                         cnt += 1;
     279             :                 }
     280           0 :                 *last = NULL;
     281           0 :                 ubifs_assert(c, cnt == c->tot_orphans - c->new_orphans);
     282           0 :                 c->cmt_orphans = cnt;
     283           0 :                 c->ohead_lnum = c->orph_first;
     284           0 :                 c->ohead_offs = 0;
     285             :         } else {
     286             :                 /*
     287             :                  * We limit the number of orphans so that this should
     288             :                  * never happen.
     289             :                  */
     290           0 :                 ubifs_err(c, "out of space in orphan area");
     291             :                 err = -EINVAL;
     292             :         }
     293           0 :         spin_unlock(&c->orphan_lock);
     294           0 :         return err;
     295             : }
     296             : 
     297             : /**
     298             :  * commit_orphans - commit orphans.
     299             :  * @c: UBIFS file-system description object
     300             :  *
     301             :  * This function commits orphans to flash. On success, %0 is returned,
     302             :  * otherwise a negative error code is returned.
     303             :  */
     304           0 : static int commit_orphans(struct ubifs_info *c)
     305             : {
     306           0 :         int avail, atomic = 0, err;
     307             : 
     308           0 :         ubifs_assert(c, c->cmt_orphans > 0);
     309           0 :         avail = avail_orphs(c);
     310           0 :         if (avail < c->cmt_orphans) {
     311             :                 /* Not enough space to write new orphans, so consolidate */
     312           0 :                 err = consolidate(c);
     313           0 :                 if (err)
     314             :                         return err;
     315             :                 atomic = 1;
     316             :         }
     317           0 :         err = write_orph_nodes(c, atomic);
     318           0 :         return err;
     319             : }
     320             : 
     321             : /**
     322             :  * erase_deleted - erase the orphans marked for deletion.
     323             :  * @c: UBIFS file-system description object
     324             :  *
     325             :  * During commit, the orphans being committed cannot be deleted, so they are
     326             :  * marked for deletion and deleted by this function. Also, the recovery
     327             :  * adds killed orphans to the deletion list, and therefore they are deleted
     328             :  * here too.
     329             :  */
     330        3398 : static void erase_deleted(struct ubifs_info *c)
     331             : {
     332             :         struct ubifs_orphan *orphan, *dnext;
     333             : 
     334        3398 :         spin_lock(&c->orphan_lock);
     335        3398 :         dnext = c->orph_dnext;
     336        6796 :         while (dnext) {
     337           0 :                 orphan = dnext;
     338           0 :                 dnext = orphan->dnext;
     339           0 :                 ubifs_assert(c, !orphan->new);
     340           0 :                 ubifs_assert(c, orphan->del);
     341           0 :                 list_del(&orphan->list);
     342           0 :                 c->tot_orphans -= 1;
     343           0 :                 dbg_gen("deleting orphan ino %lu", (unsigned long)orphan->inum);
     344             :                 kfree(orphan);
     345             :         }
     346        3398 :         c->orph_dnext = NULL;
     347        3398 :         spin_unlock(&c->orphan_lock);
     348        3398 : }
     349             : 
     350             : /**
     351             :  * ubifs_orphan_end_commit - end commit of orphans.
     352             :  * @c: UBIFS file-system description object
     353             :  *
     354             :  * End commit of orphans.
     355             :  */
     356        3398 : int ubifs_orphan_end_commit(struct ubifs_info *c)
     357             : {
     358             :         int err;
     359             : 
     360        3398 :         if (c->cmt_orphans != 0) {
     361           0 :                 err = commit_orphans(c);
     362           0 :                 if (err)
     363             :                         return err;
     364             :         }
     365        3398 :         erase_deleted(c);
     366        3398 :         err = dbg_check_orphans(c);
     367        3398 :         return err;
     368             : }
     369             : 
     370             : /**
     371             :  * ubifs_clear_orphans - erase all LEBs used for orphans.
     372             :  * @c: UBIFS file-system description object
     373             :  *
     374             :  * If recovery is not required, then the orphans from the previous session
     375             :  * are not needed. This function locates the LEBs used to record
     376             :  * orphans, and un-maps them.
     377             :  */
     378        1256 : int ubifs_clear_orphans(struct ubifs_info *c)
     379             : {
     380             :         int lnum, err;
     381             : 
     382        3421 :         for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
     383        2165 :                 err = ubifs_leb_unmap(c, lnum);
     384        2165 :                 if (err)
     385             :                         return err;
     386             :         }
     387        1256 :         c->ohead_lnum = c->orph_first;
     388        1256 :         c->ohead_offs = 0;
     389        1256 :         return 0;
     390             : }
     391             : 
     392             : /**
     393             :  * do_kill_orphans - remove orphan inodes from the index.
     394             :  * @c: UBIFS file-system description object
     395             :  * @sleb: scanned LEB
     396             :  * @last_cmt_no: cmt_no of last orphan node read is passed and returned here
     397             :  * @outofdate: whether the LEB is out of date is returned here
     398             :  * @last_flagged: whether the end orphan node is encountered
     399             :  *
     400             :  * This function is a helper to the 'kill_orphans()' function. It goes through
     401             :  * every orphan node in a LEB and for every inode number recorded, removes
     402             :  * all keys for that inode from the TNC.
     403             :  */
     404         122 : static int do_kill_orphans(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
     405             :                            unsigned long long *last_cmt_no, int *outofdate,
     406             :                            int *last_flagged)
     407             : {
     408             :         struct ubifs_scan_node *snod;
     409             :         struct ubifs_orph_node *orph;
     410         122 :         struct ubifs_ino_node *ino = NULL;
     411             :         unsigned long long cmt_no;
     412             :         ino_t inum;
     413         122 :         int i, n, err, first = 1;
     414             : 
     415         122 :         ino = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
     416         122 :         if (!ino)
     417             :                 return -ENOMEM;
     418             : 
     419        5789 :         list_for_each_entry(snod, &sleb->nodes, list) {
     420        5673 :                 if (snod->type != UBIFS_ORPH_NODE) {
     421           6 :                         ubifs_err(c, "invalid node type %d in orphan area at %d:%d",
     422             :                                   snod->type, sleb->lnum, snod->offs);
     423           6 :                         ubifs_dump_node(c, snod->node,
     424           6 :                                         c->leb_size - snod->offs);
     425           6 :                         err = -EINVAL;
     426             :                         set_failure_reason_callback(c, FR_DATA_CORRUPTED);
     427             :                         goto out_free;
     428             :                 }
     429             : 
     430        5667 :                 orph = snod->node;
     431             : 
     432             :                 /* Check commit number */
     433        5667 :                 cmt_no = le64_to_cpu(orph->cmt_no) & LLONG_MAX;
     434             :                 /*
     435             :                  * The commit number on the master node may be less, because
     436             :                  * of a failed commit. If there are several failed commits in a
     437             :                  * row, the commit number written on orphan nodes will continue
     438             :                  * to increase (because the commit number is adjusted here) even
     439             :                  * though the commit number on the master node stays the same
     440             :                  * because the master node has not been re-written.
     441             :                  */
     442        5667 :                 if (cmt_no > c->cmt_no)
     443           2 :                         c->cmt_no = cmt_no;
     444        5667 :                 if (cmt_no < *last_cmt_no && *last_flagged) {
     445             :                         /*
     446             :                          * The last orphan node had a higher commit number and
     447             :                          * was flagged as the last written for that commit
     448             :                          * number. That makes this orphan node, out of date.
     449             :                          */
     450           0 :                         if (!first) {
     451           0 :                                 ubifs_err(c, "out of order commit number %llu in orphan node at %d:%d",
     452             :                                           cmt_no, sleb->lnum, snod->offs);
     453           0 :                                 ubifs_dump_node(c, snod->node,
     454           0 :                                                 c->leb_size - snod->offs);
     455           0 :                                 err = -EINVAL;
     456             :                                 set_failure_reason_callback(c, FR_DATA_CORRUPTED);
     457             :                                 goto out_free;
     458             :                         }
     459           0 :                         dbg_rcvry("out of date LEB %d", sleb->lnum);
     460           0 :                         *outofdate = 1;
     461           0 :                         err = 0;
     462           0 :                         goto out_free;
     463             :                 }
     464             : 
     465        5667 :                 if (first)
     466          90 :                         first = 0;
     467             : 
     468        5667 :                 n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
     469       12277 :                 for (i = 0; i < n; i++) {
     470             :                         union ubifs_key key;
     471             : 
     472        6610 :                         inum = le64_to_cpu(orph->inos[i]);
     473             : 
     474       13220 :                         ino_key_init(c, &key, inum);
     475        6610 :                         err = ubifs_tnc_lookup(c, &key, ino);
     476        6610 :                         if (err && err != -ENOENT) {
     477             :                                 unsigned int reason;
     478             : 
     479           0 :                                 reason = get_failure_reason_callback(c);
     480           0 :                                 if (reason & FR_DATA_CORRUPTED) {
     481           0 :                                         test_and_clear_failure_reason_callback(c, FR_DATA_CORRUPTED);
     482           0 :                                         if (handle_failure_callback(c, FR_H_TNC_DATA_CORRUPTED, NULL)) {
     483             :                                                 /* Leave the inode to be deleted by subsequent steps */
     484           0 :                                                 continue;
     485             :                                         }
     486             :                                 }
     487           0 :                                 goto out_free;
     488             :                         }
     489             : 
     490             :                         /*
     491             :                          * Check whether an inode can really get deleted.
     492             :                          * linkat() with O_TMPFILE allows rebirth of an inode.
     493             :                          */
     494        6610 :                         if (err == 0 && ino->nlink == 0) {
     495           0 :                                 dbg_rcvry("deleting orphaned inode %lu",
     496             :                                           (unsigned long)inum);
     497             : 
     498           0 :                                 err = ubifs_tnc_remove_ino(c, inum);
     499           0 :                                 if (err) {
     500           0 :                                         if (c->program_type == FSCK_PROGRAM_TYPE)
     501             :                                                 goto out_free;
     502           0 :                                         goto out_ro;
     503             :                                 }
     504             :                         }
     505             :                 }
     506             : 
     507        5667 :                 *last_cmt_no = cmt_no;
     508        5667 :                 if (le64_to_cpu(orph->cmt_no) & (1ULL << 63)) {
     509        5667 :                         dbg_rcvry("last orph node for commit %llu at %d:%d",
     510             :                                   cmt_no, sleb->lnum, snod->offs);
     511        5667 :                         *last_flagged = 1;
     512             :                 } else
     513           0 :                         *last_flagged = 0;
     514             :         }
     515             : 
     516             :         err = 0;
     517         122 : out_free:
     518         122 :         kfree(ino);
     519         122 :         return err;
     520             : 
     521           0 : out_ro:
     522           0 :         ubifs_ro_mode(c, err);
     523           0 :         kfree(ino);
     524           0 :         return err;
     525             : }
     526             : 
     527             : /**
     528             :  * kill_orphans - remove all orphan inodes from the index.
     529             :  * @c: UBIFS file-system description object
     530             :  *
     531             :  * If recovery is required, then orphan inodes recorded during the previous
     532             :  * session (which ended with an unclean unmount) must be deleted from the index.
     533             :  * This is done by updating the TNC, but since the index is not updated until
     534             :  * the next commit, the LEBs where the orphan information is recorded are not
     535             :  * erased until the next commit.
     536             :  */
     537        1091 : static int kill_orphans(struct ubifs_info *c)
     538             : {
     539        1091 :         unsigned long long last_cmt_no = 0;
     540        1091 :         int lnum, err = 0, outofdate = 0, last_flagged = 0;
     541             : 
     542        1091 :         c->ohead_lnum = c->orph_first;
     543        1091 :         c->ohead_offs = 0;
     544             :         /* Check no-orphans flag and skip this if no orphans */
     545        1091 :         if (c->no_orphs) {
     546        1027 :                 dbg_rcvry("no orphans");
     547             :                 return 0;
     548             :         }
     549             :         /*
     550             :          * Orph nodes always start at c->orph_first and are written to each
     551             :          * successive LEB in turn. Generally unused LEBs will have been unmapped
     552             :          * but may contain out of date orphan nodes if the unmap didn't go
     553             :          * through. In addition, the last orphan node written for each commit is
     554             :          * marked (top bit of orph->cmt_no is set to 1). It is possible that
     555             :          * there are orphan nodes from the next commit (i.e. the commit did not
     556             :          * complete successfully). In that case, no orphans will have been lost
     557             :          * due to the way that orphans are written, and any orphans added will
     558             :          * be valid orphans anyway and so can be deleted.
     559             :          */
     560         183 :         for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
     561             :                 struct ubifs_scan_leb *sleb;
     562             : 
     563         122 :                 dbg_rcvry("LEB %d", lnum);
     564         122 :                 sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
     565         122 :                 if (IS_ERR(sleb)) {
     566           0 :                         if (PTR_ERR(sleb) == -EUCLEAN) {
     567           0 :                                 clear_failure_reason_callback(c);
     568           0 :                                 sleb = ubifs_recover_leb(c, lnum, 0,
     569             :                                                          c->sbuf, -1);
     570             :                         }
     571           0 :                         if (IS_ERR(sleb)) {
     572           0 :                                 if (test_and_clear_failure_reason_callback(c, FR_DATA_CORRUPTED) &&
     573           0 :                                     handle_failure_callback(c, FR_H_ORPHAN_CORRUPTED, &lnum)) {
     574             :                                         /* Skip the orphan LEB. */
     575           0 :                                         continue;
     576             :                                 }
     577           0 :                                 err = PTR_ERR(sleb);
     578           0 :                                 break;
     579             :                         }
     580             :                 }
     581         122 :                 err = do_kill_orphans(c, sleb, &last_cmt_no, &outofdate,
     582             :                                       &last_flagged);
     583         122 :                 if (err) {
     584           6 :                         unsigned int reason = get_failure_reason_callback(c);
     585             : 
     586           6 :                         if (reason & FR_DATA_CORRUPTED) {
     587           6 :                                 test_and_clear_failure_reason_callback(c, FR_DATA_CORRUPTED);
     588           3 :                                 if (handle_failure_callback(c, FR_H_ORPHAN_CORRUPTED, &lnum)) {
     589           3 :                                         err = 0;
     590             :                                         /* Skip the orphan LEB. */
     591           3 :                                         ubifs_scan_destroy(sleb);
     592           3 :                                         continue;
     593             :                                 }
     594             :                         }
     595           0 :                         ubifs_scan_destroy(sleb);
     596           0 :                         break;
     597             :                 }
     598         116 :                 if (outofdate) {
     599           0 :                         ubifs_scan_destroy(sleb);
     600           0 :                         break;
     601             :                 }
     602         116 :                 if (sleb->endpt) {
     603          90 :                         c->ohead_lnum = lnum;
     604          90 :                         c->ohead_offs = sleb->endpt;
     605             :                 }
     606         116 :                 ubifs_scan_destroy(sleb);
     607             :         }
     608             :         return err;
     609             : }
     610             : 
     611             : /**
     612             :  * ubifs_mount_orphans - delete orphan inodes and erase LEBs that recorded them.
     613             :  * @c: UBIFS file-system description object
     614             :  * @unclean: indicates recovery from unclean unmount
     615             :  * @read_only: indicates read only mount
     616             :  *
     617             :  * This function is called when mounting to erase orphans from the previous
     618             :  * session. If UBIFS was not unmounted cleanly, then the inodes recorded as
     619             :  * orphans are deleted.
     620             :  */
     621        2199 : int ubifs_mount_orphans(struct ubifs_info *c, int unclean, int read_only)
     622             : {
     623        2199 :         int err = 0;
     624             : 
     625        2199 :         c->max_orphans = tot_avail_orphs(c);
     626             : 
     627        2199 :         if (!read_only) {
     628        2095 :                 c->orph_buf = vmalloc(c->leb_size);
     629        2095 :                 if (!c->orph_buf)
     630             :                         return -ENOMEM;
     631             :         }
     632             : 
     633        2199 :         if (unclean)
     634        1091 :                 err = kill_orphans(c);
     635        1108 :         else if (!read_only)
     636        1098 :                 err = ubifs_clear_orphans(c);
     637             : 
     638             :         return err;
     639             : }
     640             : 
     641             : static int dbg_check_orphans(__unused struct ubifs_info *c)
     642             : {
     643             :         return 0;
     644             : }

Generated by: LCOV version 1.13