Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * This file is part of UBIFS.
0004  *
0005  * Copyright (C) 2006-2008 Nokia Corporation.
0006  *
0007  * Author: Adrian Hunter
0008  */
0009 
0010 #include "ubifs.h"
0011 
0012 /*
0013  * An orphan is an inode number whose inode node has been committed to the index
0014  * with a link count of zero. That happens when an open file is deleted
0015  * (unlinked) and then a commit is run. In the normal course of events the inode
0016  * would be deleted when the file is closed. However in the case of an unclean
0017  * unmount, orphans need to be accounted for. After an unclean unmount, the
0018  * orphans' inodes must be deleted which means either scanning the entire index
0019  * looking for them, or keeping a list on flash somewhere. This unit implements
0020  * the latter approach.
0021  *
0022  * The orphan area is a fixed number of LEBs situated between the LPT area and
0023  * the main area. The number of orphan area LEBs is specified when the file
0024  * system is created. The minimum number is 1. The size of the orphan area
0025  * should be so that it can hold the maximum number of orphans that are expected
0026  * to ever exist at one time.
0027  *
0028  * The number of orphans that can fit in a LEB is:
0029  *
0030  *         (c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64)
0031  *
0032  * For example: a 15872 byte LEB can fit 1980 orphans so 1 LEB may be enough.
0033  *
0034  * Orphans are accumulated in a rb-tree. When an inode's link count drops to
0035  * zero, the inode number is added to the rb-tree. It is removed from the tree
0036  * when the inode is deleted.  Any new orphans that are in the orphan tree when
0037  * the commit is run, are written to the orphan area in 1 or more orphan nodes.
0038  * If the orphan area is full, it is consolidated to make space.  There is
0039  * always enough space because validation prevents the user from creating more
0040  * than the maximum number of orphans allowed.
0041  */
0042 
0043 static int dbg_check_orphans(struct ubifs_info *c);
0044 
0045 static struct ubifs_orphan *orphan_add(struct ubifs_info *c, ino_t inum,
0046                        struct ubifs_orphan *parent_orphan)
0047 {
0048     struct ubifs_orphan *orphan, *o;
0049     struct rb_node **p, *parent = NULL;
0050 
0051     orphan = kzalloc(sizeof(struct ubifs_orphan), GFP_NOFS);
0052     if (!orphan)
0053         return ERR_PTR(-ENOMEM);
0054     orphan->inum = inum;
0055     orphan->new = 1;
0056     INIT_LIST_HEAD(&orphan->child_list);
0057 
0058     spin_lock(&c->orphan_lock);
0059     if (c->tot_orphans >= c->max_orphans) {
0060         spin_unlock(&c->orphan_lock);
0061         kfree(orphan);
0062         return ERR_PTR(-ENFILE);
0063     }
0064     p = &c->orph_tree.rb_node;
0065     while (*p) {
0066         parent = *p;
0067         o = rb_entry(parent, struct ubifs_orphan, rb);
0068         if (inum < o->inum)
0069             p = &(*p)->rb_left;
0070         else if (inum > o->inum)
0071             p = &(*p)->rb_right;
0072         else {
0073             ubifs_err(c, "orphaned twice");
0074             spin_unlock(&c->orphan_lock);
0075             kfree(orphan);
0076             return ERR_PTR(-EINVAL);
0077         }
0078     }
0079     c->tot_orphans += 1;
0080     c->new_orphans += 1;
0081     rb_link_node(&orphan->rb, parent, p);
0082     rb_insert_color(&orphan->rb, &c->orph_tree);
0083     list_add_tail(&orphan->list, &c->orph_list);
0084     list_add_tail(&orphan->new_list, &c->orph_new);
0085 
0086     if (parent_orphan) {
0087         list_add_tail(&orphan->child_list,
0088                   &parent_orphan->child_list);
0089     }
0090 
0091     spin_unlock(&c->orphan_lock);
0092     dbg_gen("ino %lu", (unsigned long)inum);
0093     return orphan;
0094 }
0095 
0096 static struct ubifs_orphan *lookup_orphan(struct ubifs_info *c, ino_t inum)
0097 {
0098     struct ubifs_orphan *o;
0099     struct rb_node *p;
0100 
0101     p = c->orph_tree.rb_node;
0102     while (p) {
0103         o = rb_entry(p, struct ubifs_orphan, rb);
0104         if (inum < o->inum)
0105             p = p->rb_left;
0106         else if (inum > o->inum)
0107             p = p->rb_right;
0108         else {
0109             return o;
0110         }
0111     }
0112     return NULL;
0113 }
0114 
0115 static void __orphan_drop(struct ubifs_info *c, struct ubifs_orphan *o)
0116 {
0117     rb_erase(&o->rb, &c->orph_tree);
0118     list_del(&o->list);
0119     c->tot_orphans -= 1;
0120 
0121     if (o->new) {
0122         list_del(&o->new_list);
0123         c->new_orphans -= 1;
0124     }
0125 
0126     kfree(o);
0127 }
0128 
0129 static void orphan_delete(struct ubifs_info *c, struct ubifs_orphan *orph)
0130 {
0131     if (orph->del) {
0132         dbg_gen("deleted twice ino %lu", (unsigned long)orph->inum);
0133         return;
0134     }
0135 
0136     if (orph->cmt) {
0137         orph->del = 1;
0138         orph->dnext = c->orph_dnext;
0139         c->orph_dnext = orph;
0140         dbg_gen("delete later ino %lu", (unsigned long)orph->inum);
0141         return;
0142     }
0143 
0144     __orphan_drop(c, orph);
0145 }
0146 
0147 /**
0148  * ubifs_add_orphan - add an orphan.
0149  * @c: UBIFS file-system description object
0150  * @inum: orphan inode number
0151  *
0152  * Add an orphan. This function is called when an inodes link count drops to
0153  * zero.
0154  */
0155 int ubifs_add_orphan(struct ubifs_info *c, ino_t inum)
0156 {
0157     int err = 0;
0158     ino_t xattr_inum;
0159     union ubifs_key key;
0160     struct ubifs_dent_node *xent, *pxent = NULL;
0161     struct fscrypt_name nm = {0};
0162     struct ubifs_orphan *xattr_orphan;
0163     struct ubifs_orphan *orphan;
0164 
0165     orphan = orphan_add(c, inum, NULL);
0166     if (IS_ERR(orphan))
0167         return PTR_ERR(orphan);
0168 
0169     lowest_xent_key(c, &key, inum);
0170     while (1) {
0171         xent = ubifs_tnc_next_ent(c, &key, &nm);
0172         if (IS_ERR(xent)) {
0173             err = PTR_ERR(xent);
0174             if (err == -ENOENT)
0175                 break;
0176             kfree(pxent);
0177             return err;
0178         }
0179 
0180         fname_name(&nm) = xent->name;
0181         fname_len(&nm) = le16_to_cpu(xent->nlen);
0182         xattr_inum = le64_to_cpu(xent->inum);
0183 
0184         xattr_orphan = orphan_add(c, xattr_inum, orphan);
0185         if (IS_ERR(xattr_orphan)) {
0186             kfree(pxent);
0187             kfree(xent);
0188             return PTR_ERR(xattr_orphan);
0189         }
0190 
0191         kfree(pxent);
0192         pxent = xent;
0193         key_read(c, &xent->key, &key);
0194     }
0195     kfree(pxent);
0196 
0197     return 0;
0198 }
0199 
0200 /**
0201  * ubifs_delete_orphan - delete an orphan.
0202  * @c: UBIFS file-system description object
0203  * @inum: orphan inode number
0204  *
0205  * Delete an orphan. This function is called when an inode is deleted.
0206  */
0207 void ubifs_delete_orphan(struct ubifs_info *c, ino_t inum)
0208 {
0209     struct ubifs_orphan *orph, *child_orph, *tmp_o;
0210 
0211     spin_lock(&c->orphan_lock);
0212 
0213     orph = lookup_orphan(c, inum);
0214     if (!orph) {
0215         spin_unlock(&c->orphan_lock);
0216         ubifs_err(c, "missing orphan ino %lu", (unsigned long)inum);
0217         dump_stack();
0218 
0219         return;
0220     }
0221 
0222     list_for_each_entry_safe(child_orph, tmp_o, &orph->child_list, child_list) {
0223         list_del(&child_orph->child_list);
0224         orphan_delete(c, child_orph);
0225     }
0226     
0227     orphan_delete(c, orph);
0228 
0229     spin_unlock(&c->orphan_lock);
0230 }
0231 
0232 /**
0233  * ubifs_orphan_start_commit - start commit of orphans.
0234  * @c: UBIFS file-system description object
0235  *
0236  * Start commit of orphans.
0237  */
0238 int ubifs_orphan_start_commit(struct ubifs_info *c)
0239 {
0240     struct ubifs_orphan *orphan, **last;
0241 
0242     spin_lock(&c->orphan_lock);
0243     last = &c->orph_cnext;
0244     list_for_each_entry(orphan, &c->orph_new, new_list) {
0245         ubifs_assert(c, orphan->new);
0246         ubifs_assert(c, !orphan->cmt);
0247         orphan->new = 0;
0248         orphan->cmt = 1;
0249         *last = orphan;
0250         last = &orphan->cnext;
0251     }
0252     *last = NULL;
0253     c->cmt_orphans = c->new_orphans;
0254     c->new_orphans = 0;
0255     dbg_cmt("%d orphans to commit", c->cmt_orphans);
0256     INIT_LIST_HEAD(&c->orph_new);
0257     if (c->tot_orphans == 0)
0258         c->no_orphs = 1;
0259     else
0260         c->no_orphs = 0;
0261     spin_unlock(&c->orphan_lock);
0262     return 0;
0263 }
0264 
0265 /**
0266  * avail_orphs - calculate available space.
0267  * @c: UBIFS file-system description object
0268  *
0269  * This function returns the number of orphans that can be written in the
0270  * available space.
0271  */
0272 static int avail_orphs(struct ubifs_info *c)
0273 {
0274     int avail_lebs, avail, gap;
0275 
0276     avail_lebs = c->orph_lebs - (c->ohead_lnum - c->orph_first) - 1;
0277     avail = avail_lebs *
0278            ((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
0279     gap = c->leb_size - c->ohead_offs;
0280     if (gap >= UBIFS_ORPH_NODE_SZ + sizeof(__le64))
0281         avail += (gap - UBIFS_ORPH_NODE_SZ) / sizeof(__le64);
0282     return avail;
0283 }
0284 
0285 /**
0286  * tot_avail_orphs - calculate total space.
0287  * @c: UBIFS file-system description object
0288  *
0289  * This function returns the number of orphans that can be written in half
0290  * the total space. That leaves half the space for adding new orphans.
0291  */
0292 static int tot_avail_orphs(struct ubifs_info *c)
0293 {
0294     int avail_lebs, avail;
0295 
0296     avail_lebs = c->orph_lebs;
0297     avail = avail_lebs *
0298            ((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
0299     return avail / 2;
0300 }
0301 
0302 /**
0303  * do_write_orph_node - write a node to the orphan head.
0304  * @c: UBIFS file-system description object
0305  * @len: length of node
0306  * @atomic: write atomically
0307  *
0308  * This function writes a node to the orphan head from the orphan buffer. If
0309  * %atomic is not zero, then the write is done atomically. On success, %0 is
0310  * returned, otherwise a negative error code is returned.
0311  */
0312 static int do_write_orph_node(struct ubifs_info *c, int len, int atomic)
0313 {
0314     int err = 0;
0315 
0316     if (atomic) {
0317         ubifs_assert(c, c->ohead_offs == 0);
0318         ubifs_prepare_node(c, c->orph_buf, len, 1);
0319         len = ALIGN(len, c->min_io_size);
0320         err = ubifs_leb_change(c, c->ohead_lnum, c->orph_buf, len);
0321     } else {
0322         if (c->ohead_offs == 0) {
0323             /* Ensure LEB has been unmapped */
0324             err = ubifs_leb_unmap(c, c->ohead_lnum);
0325             if (err)
0326                 return err;
0327         }
0328         err = ubifs_write_node(c, c->orph_buf, len, c->ohead_lnum,
0329                        c->ohead_offs);
0330     }
0331     return err;
0332 }
0333 
0334 /**
0335  * write_orph_node - write an orphan node.
0336  * @c: UBIFS file-system description object
0337  * @atomic: write atomically
0338  *
0339  * This function builds an orphan node from the cnext list and writes it to the
0340  * orphan head. On success, %0 is returned, otherwise a negative error code
0341  * is returned.
0342  */
0343 static int write_orph_node(struct ubifs_info *c, int atomic)
0344 {
0345     struct ubifs_orphan *orphan, *cnext;
0346     struct ubifs_orph_node *orph;
0347     int gap, err, len, cnt, i;
0348 
0349     ubifs_assert(c, c->cmt_orphans > 0);
0350     gap = c->leb_size - c->ohead_offs;
0351     if (gap < UBIFS_ORPH_NODE_SZ + sizeof(__le64)) {
0352         c->ohead_lnum += 1;
0353         c->ohead_offs = 0;
0354         gap = c->leb_size;
0355         if (c->ohead_lnum > c->orph_last) {
0356             /*
0357              * We limit the number of orphans so that this should
0358              * never happen.
0359              */
0360             ubifs_err(c, "out of space in orphan area");
0361             return -EINVAL;
0362         }
0363     }
0364     cnt = (gap - UBIFS_ORPH_NODE_SZ) / sizeof(__le64);
0365     if (cnt > c->cmt_orphans)
0366         cnt = c->cmt_orphans;
0367     len = UBIFS_ORPH_NODE_SZ + cnt * sizeof(__le64);
0368     ubifs_assert(c, c->orph_buf);
0369     orph = c->orph_buf;
0370     orph->ch.node_type = UBIFS_ORPH_NODE;
0371     spin_lock(&c->orphan_lock);
0372     cnext = c->orph_cnext;
0373     for (i = 0; i < cnt; i++) {
0374         orphan = cnext;
0375         ubifs_assert(c, orphan->cmt);
0376         orph->inos[i] = cpu_to_le64(orphan->inum);
0377         orphan->cmt = 0;
0378         cnext = orphan->cnext;
0379         orphan->cnext = NULL;
0380     }
0381     c->orph_cnext = cnext;
0382     c->cmt_orphans -= cnt;
0383     spin_unlock(&c->orphan_lock);
0384     if (c->cmt_orphans)
0385         orph->cmt_no = cpu_to_le64(c->cmt_no);
0386     else
0387         /* Mark the last node of the commit */
0388         orph->cmt_no = cpu_to_le64((c->cmt_no) | (1ULL << 63));
0389     ubifs_assert(c, c->ohead_offs + len <= c->leb_size);
0390     ubifs_assert(c, c->ohead_lnum >= c->orph_first);
0391     ubifs_assert(c, c->ohead_lnum <= c->orph_last);
0392     err = do_write_orph_node(c, len, atomic);
0393     c->ohead_offs += ALIGN(len, c->min_io_size);
0394     c->ohead_offs = ALIGN(c->ohead_offs, 8);
0395     return err;
0396 }
0397 
0398 /**
0399  * write_orph_nodes - write orphan nodes until there are no more to commit.
0400  * @c: UBIFS file-system description object
0401  * @atomic: write atomically
0402  *
0403  * This function writes orphan nodes for all the orphans to commit. On success,
0404  * %0 is returned, otherwise a negative error code is returned.
0405  */
0406 static int write_orph_nodes(struct ubifs_info *c, int atomic)
0407 {
0408     int err;
0409 
0410     while (c->cmt_orphans > 0) {
0411         err = write_orph_node(c, atomic);
0412         if (err)
0413             return err;
0414     }
0415     if (atomic) {
0416         int lnum;
0417 
0418         /* Unmap any unused LEBs after consolidation */
0419         for (lnum = c->ohead_lnum + 1; lnum <= c->orph_last; lnum++) {
0420             err = ubifs_leb_unmap(c, lnum);
0421             if (err)
0422                 return err;
0423         }
0424     }
0425     return 0;
0426 }
0427 
0428 /**
0429  * consolidate - consolidate the orphan area.
0430  * @c: UBIFS file-system description object
0431  *
0432  * This function enables consolidation by putting all the orphans into the list
0433  * to commit. The list is in the order that the orphans were added, and the
0434  * LEBs are written atomically in order, so at no time can orphans be lost by
0435  * an unclean unmount.
0436  *
0437  * This function returns %0 on success and a negative error code on failure.
0438  */
0439 static int consolidate(struct ubifs_info *c)
0440 {
0441     int tot_avail = tot_avail_orphs(c), err = 0;
0442 
0443     spin_lock(&c->orphan_lock);
0444     dbg_cmt("there is space for %d orphans and there are %d",
0445         tot_avail, c->tot_orphans);
0446     if (c->tot_orphans - c->new_orphans <= tot_avail) {
0447         struct ubifs_orphan *orphan, **last;
0448         int cnt = 0;
0449 
0450         /* Change the cnext list to include all non-new orphans */
0451         last = &c->orph_cnext;
0452         list_for_each_entry(orphan, &c->orph_list, list) {
0453             if (orphan->new)
0454                 continue;
0455             orphan->cmt = 1;
0456             *last = orphan;
0457             last = &orphan->cnext;
0458             cnt += 1;
0459         }
0460         *last = NULL;
0461         ubifs_assert(c, cnt == c->tot_orphans - c->new_orphans);
0462         c->cmt_orphans = cnt;
0463         c->ohead_lnum = c->orph_first;
0464         c->ohead_offs = 0;
0465     } else {
0466         /*
0467          * We limit the number of orphans so that this should
0468          * never happen.
0469          */
0470         ubifs_err(c, "out of space in orphan area");
0471         err = -EINVAL;
0472     }
0473     spin_unlock(&c->orphan_lock);
0474     return err;
0475 }
0476 
0477 /**
0478  * commit_orphans - commit orphans.
0479  * @c: UBIFS file-system description object
0480  *
0481  * This function commits orphans to flash. On success, %0 is returned,
0482  * otherwise a negative error code is returned.
0483  */
0484 static int commit_orphans(struct ubifs_info *c)
0485 {
0486     int avail, atomic = 0, err;
0487 
0488     ubifs_assert(c, c->cmt_orphans > 0);
0489     avail = avail_orphs(c);
0490     if (avail < c->cmt_orphans) {
0491         /* Not enough space to write new orphans, so consolidate */
0492         err = consolidate(c);
0493         if (err)
0494             return err;
0495         atomic = 1;
0496     }
0497     err = write_orph_nodes(c, atomic);
0498     return err;
0499 }
0500 
0501 /**
0502  * erase_deleted - erase the orphans marked for deletion.
0503  * @c: UBIFS file-system description object
0504  *
0505  * During commit, the orphans being committed cannot be deleted, so they are
0506  * marked for deletion and deleted by this function. Also, the recovery
0507  * adds killed orphans to the deletion list, and therefore they are deleted
0508  * here too.
0509  */
0510 static void erase_deleted(struct ubifs_info *c)
0511 {
0512     struct ubifs_orphan *orphan, *dnext;
0513 
0514     spin_lock(&c->orphan_lock);
0515     dnext = c->orph_dnext;
0516     while (dnext) {
0517         orphan = dnext;
0518         dnext = orphan->dnext;
0519         ubifs_assert(c, !orphan->new);
0520         ubifs_assert(c, orphan->del);
0521         rb_erase(&orphan->rb, &c->orph_tree);
0522         list_del(&orphan->list);
0523         c->tot_orphans -= 1;
0524         dbg_gen("deleting orphan ino %lu", (unsigned long)orphan->inum);
0525         kfree(orphan);
0526     }
0527     c->orph_dnext = NULL;
0528     spin_unlock(&c->orphan_lock);
0529 }
0530 
0531 /**
0532  * ubifs_orphan_end_commit - end commit of orphans.
0533  * @c: UBIFS file-system description object
0534  *
0535  * End commit of orphans.
0536  */
0537 int ubifs_orphan_end_commit(struct ubifs_info *c)
0538 {
0539     int err;
0540 
0541     if (c->cmt_orphans != 0) {
0542         err = commit_orphans(c);
0543         if (err)
0544             return err;
0545     }
0546     erase_deleted(c);
0547     err = dbg_check_orphans(c);
0548     return err;
0549 }
0550 
0551 /**
0552  * ubifs_clear_orphans - erase all LEBs used for orphans.
0553  * @c: UBIFS file-system description object
0554  *
0555  * If recovery is not required, then the orphans from the previous session
0556  * are not needed. This function locates the LEBs used to record
0557  * orphans, and un-maps them.
0558  */
0559 int ubifs_clear_orphans(struct ubifs_info *c)
0560 {
0561     int lnum, err;
0562 
0563     for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
0564         err = ubifs_leb_unmap(c, lnum);
0565         if (err)
0566             return err;
0567     }
0568     c->ohead_lnum = c->orph_first;
0569     c->ohead_offs = 0;
0570     return 0;
0571 }
0572 
0573 /**
0574  * insert_dead_orphan - insert an orphan.
0575  * @c: UBIFS file-system description object
0576  * @inum: orphan inode number
0577  *
0578  * This function is a helper to the 'do_kill_orphans()' function. The orphan
0579  * must be kept until the next commit, so it is added to the rb-tree and the
0580  * deletion list.
0581  */
0582 static int insert_dead_orphan(struct ubifs_info *c, ino_t inum)
0583 {
0584     struct ubifs_orphan *orphan, *o;
0585     struct rb_node **p, *parent = NULL;
0586 
0587     orphan = kzalloc(sizeof(struct ubifs_orphan), GFP_KERNEL);
0588     if (!orphan)
0589         return -ENOMEM;
0590     orphan->inum = inum;
0591 
0592     p = &c->orph_tree.rb_node;
0593     while (*p) {
0594         parent = *p;
0595         o = rb_entry(parent, struct ubifs_orphan, rb);
0596         if (inum < o->inum)
0597             p = &(*p)->rb_left;
0598         else if (inum > o->inum)
0599             p = &(*p)->rb_right;
0600         else {
0601             /* Already added - no problem */
0602             kfree(orphan);
0603             return 0;
0604         }
0605     }
0606     c->tot_orphans += 1;
0607     rb_link_node(&orphan->rb, parent, p);
0608     rb_insert_color(&orphan->rb, &c->orph_tree);
0609     list_add_tail(&orphan->list, &c->orph_list);
0610     orphan->del = 1;
0611     orphan->dnext = c->orph_dnext;
0612     c->orph_dnext = orphan;
0613     dbg_mnt("ino %lu, new %d, tot %d", (unsigned long)inum,
0614         c->new_orphans, c->tot_orphans);
0615     return 0;
0616 }
0617 
0618 /**
0619  * do_kill_orphans - remove orphan inodes from the index.
0620  * @c: UBIFS file-system description object
0621  * @sleb: scanned LEB
0622  * @last_cmt_no: cmt_no of last orphan node read is passed and returned here
0623  * @outofdate: whether the LEB is out of date is returned here
0624  * @last_flagged: whether the end orphan node is encountered
0625  *
0626  * This function is a helper to the 'kill_orphans()' function. It goes through
0627  * every orphan node in a LEB and for every inode number recorded, removes
0628  * all keys for that inode from the TNC.
0629  */
0630 static int do_kill_orphans(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
0631                unsigned long long *last_cmt_no, int *outofdate,
0632                int *last_flagged)
0633 {
0634     struct ubifs_scan_node *snod;
0635     struct ubifs_orph_node *orph;
0636     struct ubifs_ino_node *ino = NULL;
0637     unsigned long long cmt_no;
0638     ino_t inum;
0639     int i, n, err, first = 1;
0640 
0641     ino = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
0642     if (!ino)
0643         return -ENOMEM;
0644 
0645     list_for_each_entry(snod, &sleb->nodes, list) {
0646         if (snod->type != UBIFS_ORPH_NODE) {
0647             ubifs_err(c, "invalid node type %d in orphan area at %d:%d",
0648                   snod->type, sleb->lnum, snod->offs);
0649             ubifs_dump_node(c, snod->node,
0650                     c->leb_size - snod->offs);
0651             err = -EINVAL;
0652             goto out_free;
0653         }
0654 
0655         orph = snod->node;
0656 
0657         /* Check commit number */
0658         cmt_no = le64_to_cpu(orph->cmt_no) & LLONG_MAX;
0659         /*
0660          * The commit number on the master node may be less, because
0661          * of a failed commit. If there are several failed commits in a
0662          * row, the commit number written on orphan nodes will continue
0663          * to increase (because the commit number is adjusted here) even
0664          * though the commit number on the master node stays the same
0665          * because the master node has not been re-written.
0666          */
0667         if (cmt_no > c->cmt_no)
0668             c->cmt_no = cmt_no;
0669         if (cmt_no < *last_cmt_no && *last_flagged) {
0670             /*
0671              * The last orphan node had a higher commit number and
0672              * was flagged as the last written for that commit
0673              * number. That makes this orphan node, out of date.
0674              */
0675             if (!first) {
0676                 ubifs_err(c, "out of order commit number %llu in orphan node at %d:%d",
0677                       cmt_no, sleb->lnum, snod->offs);
0678                 ubifs_dump_node(c, snod->node,
0679                         c->leb_size - snod->offs);
0680                 err = -EINVAL;
0681                 goto out_free;
0682             }
0683             dbg_rcvry("out of date LEB %d", sleb->lnum);
0684             *outofdate = 1;
0685             err = 0;
0686             goto out_free;
0687         }
0688 
0689         if (first)
0690             first = 0;
0691 
0692         n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
0693         for (i = 0; i < n; i++) {
0694             union ubifs_key key1, key2;
0695 
0696             inum = le64_to_cpu(orph->inos[i]);
0697 
0698             ino_key_init(c, &key1, inum);
0699             err = ubifs_tnc_lookup(c, &key1, ino);
0700             if (err && err != -ENOENT)
0701                 goto out_free;
0702 
0703             /*
0704              * Check whether an inode can really get deleted.
0705              * linkat() with O_TMPFILE allows rebirth of an inode.
0706              */
0707             if (err == 0 && ino->nlink == 0) {
0708                 dbg_rcvry("deleting orphaned inode %lu",
0709                       (unsigned long)inum);
0710 
0711                 lowest_ino_key(c, &key1, inum);
0712                 highest_ino_key(c, &key2, inum);
0713 
0714                 err = ubifs_tnc_remove_range(c, &key1, &key2);
0715                 if (err)
0716                     goto out_ro;
0717             }
0718 
0719             err = insert_dead_orphan(c, inum);
0720             if (err)
0721                 goto out_free;
0722         }
0723 
0724         *last_cmt_no = cmt_no;
0725         if (le64_to_cpu(orph->cmt_no) & (1ULL << 63)) {
0726             dbg_rcvry("last orph node for commit %llu at %d:%d",
0727                   cmt_no, sleb->lnum, snod->offs);
0728             *last_flagged = 1;
0729         } else
0730             *last_flagged = 0;
0731     }
0732 
0733     err = 0;
0734 out_free:
0735     kfree(ino);
0736     return err;
0737 
0738 out_ro:
0739     ubifs_ro_mode(c, err);
0740     kfree(ino);
0741     return err;
0742 }
0743 
0744 /**
0745  * kill_orphans - remove all orphan inodes from the index.
0746  * @c: UBIFS file-system description object
0747  *
0748  * If recovery is required, then orphan inodes recorded during the previous
0749  * session (which ended with an unclean unmount) must be deleted from the index.
0750  * This is done by updating the TNC, but since the index is not updated until
0751  * the next commit, the LEBs where the orphan information is recorded are not
0752  * erased until the next commit.
0753  */
0754 static int kill_orphans(struct ubifs_info *c)
0755 {
0756     unsigned long long last_cmt_no = 0;
0757     int lnum, err = 0, outofdate = 0, last_flagged = 0;
0758 
0759     c->ohead_lnum = c->orph_first;
0760     c->ohead_offs = 0;
0761     /* Check no-orphans flag and skip this if no orphans */
0762     if (c->no_orphs) {
0763         dbg_rcvry("no orphans");
0764         return 0;
0765     }
0766     /*
0767      * Orph nodes always start at c->orph_first and are written to each
0768      * successive LEB in turn. Generally unused LEBs will have been unmapped
0769      * but may contain out of date orphan nodes if the unmap didn't go
0770      * through. In addition, the last orphan node written for each commit is
0771      * marked (top bit of orph->cmt_no is set to 1). It is possible that
0772      * there are orphan nodes from the next commit (i.e. the commit did not
0773      * complete successfully). In that case, no orphans will have been lost
0774      * due to the way that orphans are written, and any orphans added will
0775      * be valid orphans anyway and so can be deleted.
0776      */
0777     for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
0778         struct ubifs_scan_leb *sleb;
0779 
0780         dbg_rcvry("LEB %d", lnum);
0781         sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
0782         if (IS_ERR(sleb)) {
0783             if (PTR_ERR(sleb) == -EUCLEAN)
0784                 sleb = ubifs_recover_leb(c, lnum, 0,
0785                              c->sbuf, -1);
0786             if (IS_ERR(sleb)) {
0787                 err = PTR_ERR(sleb);
0788                 break;
0789             }
0790         }
0791         err = do_kill_orphans(c, sleb, &last_cmt_no, &outofdate,
0792                       &last_flagged);
0793         if (err || outofdate) {
0794             ubifs_scan_destroy(sleb);
0795             break;
0796         }
0797         if (sleb->endpt) {
0798             c->ohead_lnum = lnum;
0799             c->ohead_offs = sleb->endpt;
0800         }
0801         ubifs_scan_destroy(sleb);
0802     }
0803     return err;
0804 }
0805 
0806 /**
0807  * ubifs_mount_orphans - delete orphan inodes and erase LEBs that recorded them.
0808  * @c: UBIFS file-system description object
0809  * @unclean: indicates recovery from unclean unmount
0810  * @read_only: indicates read only mount
0811  *
0812  * This function is called when mounting to erase orphans from the previous
0813  * session. If UBIFS was not unmounted cleanly, then the inodes recorded as
0814  * orphans are deleted.
0815  */
0816 int ubifs_mount_orphans(struct ubifs_info *c, int unclean, int read_only)
0817 {
0818     int err = 0;
0819 
0820     c->max_orphans = tot_avail_orphs(c);
0821 
0822     if (!read_only) {
0823         c->orph_buf = vmalloc(c->leb_size);
0824         if (!c->orph_buf)
0825             return -ENOMEM;
0826     }
0827 
0828     if (unclean)
0829         err = kill_orphans(c);
0830     else if (!read_only)
0831         err = ubifs_clear_orphans(c);
0832 
0833     return err;
0834 }
0835 
0836 /*
0837  * Everything below is related to debugging.
0838  */
0839 
0840 struct check_orphan {
0841     struct rb_node rb;
0842     ino_t inum;
0843 };
0844 
0845 struct check_info {
0846     unsigned long last_ino;
0847     unsigned long tot_inos;
0848     unsigned long missing;
0849     unsigned long long leaf_cnt;
0850     struct ubifs_ino_node *node;
0851     struct rb_root root;
0852 };
0853 
0854 static bool dbg_find_orphan(struct ubifs_info *c, ino_t inum)
0855 {
0856     bool found = false;
0857 
0858     spin_lock(&c->orphan_lock);
0859     found = !!lookup_orphan(c, inum);
0860     spin_unlock(&c->orphan_lock);
0861 
0862     return found;
0863 }
0864 
0865 static int dbg_ins_check_orphan(struct rb_root *root, ino_t inum)
0866 {
0867     struct check_orphan *orphan, *o;
0868     struct rb_node **p, *parent = NULL;
0869 
0870     orphan = kzalloc(sizeof(struct check_orphan), GFP_NOFS);
0871     if (!orphan)
0872         return -ENOMEM;
0873     orphan->inum = inum;
0874 
0875     p = &root->rb_node;
0876     while (*p) {
0877         parent = *p;
0878         o = rb_entry(parent, struct check_orphan, rb);
0879         if (inum < o->inum)
0880             p = &(*p)->rb_left;
0881         else if (inum > o->inum)
0882             p = &(*p)->rb_right;
0883         else {
0884             kfree(orphan);
0885             return 0;
0886         }
0887     }
0888     rb_link_node(&orphan->rb, parent, p);
0889     rb_insert_color(&orphan->rb, root);
0890     return 0;
0891 }
0892 
0893 static int dbg_find_check_orphan(struct rb_root *root, ino_t inum)
0894 {
0895     struct check_orphan *o;
0896     struct rb_node *p;
0897 
0898     p = root->rb_node;
0899     while (p) {
0900         o = rb_entry(p, struct check_orphan, rb);
0901         if (inum < o->inum)
0902             p = p->rb_left;
0903         else if (inum > o->inum)
0904             p = p->rb_right;
0905         else
0906             return 1;
0907     }
0908     return 0;
0909 }
0910 
0911 static void dbg_free_check_tree(struct rb_root *root)
0912 {
0913     struct check_orphan *o, *n;
0914 
0915     rbtree_postorder_for_each_entry_safe(o, n, root, rb)
0916         kfree(o);
0917 }
0918 
0919 static int dbg_orphan_check(struct ubifs_info *c, struct ubifs_zbranch *zbr,
0920                 void *priv)
0921 {
0922     struct check_info *ci = priv;
0923     ino_t inum;
0924     int err;
0925 
0926     inum = key_inum(c, &zbr->key);
0927     if (inum != ci->last_ino) {
0928         /* Lowest node type is the inode node, so it comes first */
0929         if (key_type(c, &zbr->key) != UBIFS_INO_KEY)
0930             ubifs_err(c, "found orphan node ino %lu, type %d",
0931                   (unsigned long)inum, key_type(c, &zbr->key));
0932         ci->last_ino = inum;
0933         ci->tot_inos += 1;
0934         err = ubifs_tnc_read_node(c, zbr, ci->node);
0935         if (err) {
0936             ubifs_err(c, "node read failed, error %d", err);
0937             return err;
0938         }
0939         if (ci->node->nlink == 0)
0940             /* Must be recorded as an orphan */
0941             if (!dbg_find_check_orphan(&ci->root, inum) &&
0942                 !dbg_find_orphan(c, inum)) {
0943                 ubifs_err(c, "missing orphan, ino %lu",
0944                       (unsigned long)inum);
0945                 ci->missing += 1;
0946             }
0947     }
0948     ci->leaf_cnt += 1;
0949     return 0;
0950 }
0951 
0952 static int dbg_read_orphans(struct check_info *ci, struct ubifs_scan_leb *sleb)
0953 {
0954     struct ubifs_scan_node *snod;
0955     struct ubifs_orph_node *orph;
0956     ino_t inum;
0957     int i, n, err;
0958 
0959     list_for_each_entry(snod, &sleb->nodes, list) {
0960         cond_resched();
0961         if (snod->type != UBIFS_ORPH_NODE)
0962             continue;
0963         orph = snod->node;
0964         n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
0965         for (i = 0; i < n; i++) {
0966             inum = le64_to_cpu(orph->inos[i]);
0967             err = dbg_ins_check_orphan(&ci->root, inum);
0968             if (err)
0969                 return err;
0970         }
0971     }
0972     return 0;
0973 }
0974 
0975 static int dbg_scan_orphans(struct ubifs_info *c, struct check_info *ci)
0976 {
0977     int lnum, err = 0;
0978     void *buf;
0979 
0980     /* Check no-orphans flag and skip this if no orphans */
0981     if (c->no_orphs)
0982         return 0;
0983 
0984     buf = __vmalloc(c->leb_size, GFP_NOFS);
0985     if (!buf) {
0986         ubifs_err(c, "cannot allocate memory to check orphans");
0987         return 0;
0988     }
0989 
0990     for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
0991         struct ubifs_scan_leb *sleb;
0992 
0993         sleb = ubifs_scan(c, lnum, 0, buf, 0);
0994         if (IS_ERR(sleb)) {
0995             err = PTR_ERR(sleb);
0996             break;
0997         }
0998 
0999         err = dbg_read_orphans(ci, sleb);
1000         ubifs_scan_destroy(sleb);
1001         if (err)
1002             break;
1003     }
1004 
1005     vfree(buf);
1006     return err;
1007 }
1008 
1009 static int dbg_check_orphans(struct ubifs_info *c)
1010 {
1011     struct check_info ci;
1012     int err;
1013 
1014     if (!dbg_is_chk_orph(c))
1015         return 0;
1016 
1017     ci.last_ino = 0;
1018     ci.tot_inos = 0;
1019     ci.missing  = 0;
1020     ci.leaf_cnt = 0;
1021     ci.root = RB_ROOT;
1022     ci.node = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
1023     if (!ci.node) {
1024         ubifs_err(c, "out of memory");
1025         return -ENOMEM;
1026     }
1027 
1028     err = dbg_scan_orphans(c, &ci);
1029     if (err)
1030         goto out;
1031 
1032     err = dbg_walk_index(c, &dbg_orphan_check, NULL, &ci);
1033     if (err) {
1034         ubifs_err(c, "cannot scan TNC, error %d", err);
1035         goto out;
1036     }
1037 
1038     if (ci.missing) {
1039         ubifs_err(c, "%lu missing orphan(s)", ci.missing);
1040         err = -EINVAL;
1041         goto out;
1042     }
1043 
1044     dbg_cmt("last inode number is %lu", ci.last_ino);
1045     dbg_cmt("total number of inodes is %lu", ci.tot_inos);
1046     dbg_cmt("total number of leaf nodes is %llu", ci.leaf_cnt);
1047 
1048 out:
1049     dbg_free_check_tree(&ci.root);
1050     kfree(ci.node);
1051     return err;
1052 }