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