xref: /openbmc/linux/fs/ubifs/orphan.c (revision cdd38c5f1ce4398ec58fec95904b75824daab7b5)
12b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21e51764aSArtem Bityutskiy /*
31e51764aSArtem Bityutskiy  * This file is part of UBIFS.
41e51764aSArtem Bityutskiy  *
51e51764aSArtem Bityutskiy  * Copyright (C) 2006-2008 Nokia Corporation.
61e51764aSArtem Bityutskiy  *
71e51764aSArtem Bityutskiy  * Author: Adrian Hunter
81e51764aSArtem Bityutskiy  */
91e51764aSArtem Bityutskiy 
101e51764aSArtem Bityutskiy #include "ubifs.h"
111e51764aSArtem Bityutskiy 
121e51764aSArtem Bityutskiy /*
131e51764aSArtem Bityutskiy  * An orphan is an inode number whose inode node has been committed to the index
141e51764aSArtem Bityutskiy  * with a link count of zero. That happens when an open file is deleted
151e51764aSArtem Bityutskiy  * (unlinked) and then a commit is run. In the normal course of events the inode
161e51764aSArtem Bityutskiy  * would be deleted when the file is closed. However in the case of an unclean
171e51764aSArtem Bityutskiy  * unmount, orphans need to be accounted for. After an unclean unmount, the
181e51764aSArtem Bityutskiy  * orphans' inodes must be deleted which means either scanning the entire index
191e51764aSArtem Bityutskiy  * looking for them, or keeping a list on flash somewhere. This unit implements
201e51764aSArtem Bityutskiy  * the latter approach.
211e51764aSArtem Bityutskiy  *
221e51764aSArtem Bityutskiy  * The orphan area is a fixed number of LEBs situated between the LPT area and
231e51764aSArtem Bityutskiy  * the main area. The number of orphan area LEBs is specified when the file
241e51764aSArtem Bityutskiy  * system is created. The minimum number is 1. The size of the orphan area
251e51764aSArtem Bityutskiy  * should be so that it can hold the maximum number of orphans that are expected
261e51764aSArtem Bityutskiy  * to ever exist at one time.
271e51764aSArtem Bityutskiy  *
281e51764aSArtem Bityutskiy  * The number of orphans that can fit in a LEB is:
291e51764aSArtem Bityutskiy  *
301e51764aSArtem Bityutskiy  *         (c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64)
311e51764aSArtem Bityutskiy  *
321e51764aSArtem Bityutskiy  * For example: a 15872 byte LEB can fit 1980 orphans so 1 LEB may be enough.
331e51764aSArtem Bityutskiy  *
341e51764aSArtem Bityutskiy  * Orphans are accumulated in a rb-tree. When an inode's link count drops to
351e51764aSArtem Bityutskiy  * zero, the inode number is added to the rb-tree. It is removed from the tree
361e51764aSArtem Bityutskiy  * when the inode is deleted.  Any new orphans that are in the orphan tree when
3749d128aaSAdrian Hunter  * the commit is run, are written to the orphan area in 1 or more orphan nodes.
381e51764aSArtem Bityutskiy  * If the orphan area is full, it is consolidated to make space.  There is
391e51764aSArtem Bityutskiy  * always enough space because validation prevents the user from creating more
401e51764aSArtem Bityutskiy  * than the maximum number of orphans allowed.
411e51764aSArtem Bityutskiy  */
421e51764aSArtem Bityutskiy 
431e51764aSArtem Bityutskiy static int dbg_check_orphans(struct ubifs_info *c);
441e51764aSArtem Bityutskiy 
orphan_add(struct ubifs_info * c,ino_t inum,struct ubifs_orphan * parent_orphan)45988bec41SRichard Weinberger static struct ubifs_orphan *orphan_add(struct ubifs_info *c, ino_t inum,
46988bec41SRichard Weinberger 				       struct ubifs_orphan *parent_orphan)
471e51764aSArtem Bityutskiy {
481e51764aSArtem Bityutskiy 	struct ubifs_orphan *orphan, *o;
491e51764aSArtem Bityutskiy 	struct rb_node **p, *parent = NULL;
501e51764aSArtem Bityutskiy 
511e51764aSArtem Bityutskiy 	orphan = kzalloc(sizeof(struct ubifs_orphan), GFP_NOFS);
521e51764aSArtem Bityutskiy 	if (!orphan)
53988bec41SRichard Weinberger 		return ERR_PTR(-ENOMEM);
541e51764aSArtem Bityutskiy 	orphan->inum = inum;
551e51764aSArtem Bityutskiy 	orphan->new = 1;
56988bec41SRichard Weinberger 	INIT_LIST_HEAD(&orphan->child_list);
571e51764aSArtem Bityutskiy 
581e51764aSArtem Bityutskiy 	spin_lock(&c->orphan_lock);
591e51764aSArtem Bityutskiy 	if (c->tot_orphans >= c->max_orphans) {
601e51764aSArtem Bityutskiy 		spin_unlock(&c->orphan_lock);
611e51764aSArtem Bityutskiy 		kfree(orphan);
62988bec41SRichard Weinberger 		return ERR_PTR(-ENFILE);
631e51764aSArtem Bityutskiy 	}
641e51764aSArtem Bityutskiy 	p = &c->orph_tree.rb_node;
651e51764aSArtem Bityutskiy 	while (*p) {
661e51764aSArtem Bityutskiy 		parent = *p;
671e51764aSArtem Bityutskiy 		o = rb_entry(parent, struct ubifs_orphan, rb);
681e51764aSArtem Bityutskiy 		if (inum < o->inum)
691e51764aSArtem Bityutskiy 			p = &(*p)->rb_left;
701e51764aSArtem Bityutskiy 		else if (inum > o->inum)
711e51764aSArtem Bityutskiy 			p = &(*p)->rb_right;
721e51764aSArtem Bityutskiy 		else {
73235c362bSSheng Yong 			ubifs_err(c, "orphaned twice");
741e51764aSArtem Bityutskiy 			spin_unlock(&c->orphan_lock);
751e51764aSArtem Bityutskiy 			kfree(orphan);
76988bec41SRichard Weinberger 			return ERR_PTR(-EINVAL);
771e51764aSArtem Bityutskiy 		}
781e51764aSArtem Bityutskiy 	}
791e51764aSArtem Bityutskiy 	c->tot_orphans += 1;
801e51764aSArtem Bityutskiy 	c->new_orphans += 1;
811e51764aSArtem Bityutskiy 	rb_link_node(&orphan->rb, parent, p);
821e51764aSArtem Bityutskiy 	rb_insert_color(&orphan->rb, &c->orph_tree);
831e51764aSArtem Bityutskiy 	list_add_tail(&orphan->list, &c->orph_list);
841e51764aSArtem Bityutskiy 	list_add_tail(&orphan->new_list, &c->orph_new);
85988bec41SRichard Weinberger 
86988bec41SRichard Weinberger 	if (parent_orphan) {
87988bec41SRichard Weinberger 		list_add_tail(&orphan->child_list,
88988bec41SRichard Weinberger 			      &parent_orphan->child_list);
89988bec41SRichard Weinberger 	}
90988bec41SRichard Weinberger 
911e51764aSArtem Bityutskiy 	spin_unlock(&c->orphan_lock);
92e84461adSArtem Bityutskiy 	dbg_gen("ino %lu", (unsigned long)inum);
93988bec41SRichard Weinberger 	return orphan;
94988bec41SRichard Weinberger }
95988bec41SRichard Weinberger 
lookup_orphan(struct ubifs_info * c,ino_t inum)96988bec41SRichard Weinberger static struct ubifs_orphan *lookup_orphan(struct ubifs_info *c, ino_t inum)
97988bec41SRichard Weinberger {
98988bec41SRichard Weinberger 	struct ubifs_orphan *o;
99988bec41SRichard Weinberger 	struct rb_node *p;
100988bec41SRichard Weinberger 
101988bec41SRichard Weinberger 	p = c->orph_tree.rb_node;
102988bec41SRichard Weinberger 	while (p) {
103988bec41SRichard Weinberger 		o = rb_entry(p, struct ubifs_orphan, rb);
104988bec41SRichard Weinberger 		if (inum < o->inum)
105988bec41SRichard Weinberger 			p = p->rb_left;
106988bec41SRichard Weinberger 		else if (inum > o->inum)
107988bec41SRichard Weinberger 			p = p->rb_right;
108988bec41SRichard Weinberger 		else {
109988bec41SRichard Weinberger 			return o;
110988bec41SRichard Weinberger 		}
111988bec41SRichard Weinberger 	}
112988bec41SRichard Weinberger 	return NULL;
113988bec41SRichard Weinberger }
114988bec41SRichard Weinberger 
__orphan_drop(struct ubifs_info * c,struct ubifs_orphan * o)115988bec41SRichard Weinberger static void __orphan_drop(struct ubifs_info *c, struct ubifs_orphan *o)
116988bec41SRichard Weinberger {
117988bec41SRichard Weinberger 	rb_erase(&o->rb, &c->orph_tree);
118988bec41SRichard Weinberger 	list_del(&o->list);
119988bec41SRichard Weinberger 	c->tot_orphans -= 1;
120988bec41SRichard Weinberger 
121988bec41SRichard Weinberger 	if (o->new) {
122988bec41SRichard Weinberger 		list_del(&o->new_list);
123988bec41SRichard Weinberger 		c->new_orphans -= 1;
124988bec41SRichard Weinberger 	}
125988bec41SRichard Weinberger 
126988bec41SRichard Weinberger 	kfree(o);
127988bec41SRichard Weinberger }
128988bec41SRichard Weinberger 
orphan_delete(struct ubifs_info * c,struct ubifs_orphan * orph)1298009ce95SRichard Weinberger static void orphan_delete(struct ubifs_info *c, struct ubifs_orphan *orph)
130988bec41SRichard Weinberger {
131988bec41SRichard Weinberger 	if (orph->del) {
132155fc6baSGeert Uytterhoeven 		dbg_gen("deleted twice ino %lu", (unsigned long)orph->inum);
133988bec41SRichard Weinberger 		return;
134988bec41SRichard Weinberger 	}
135988bec41SRichard Weinberger 
136988bec41SRichard Weinberger 	if (orph->cmt) {
137988bec41SRichard Weinberger 		orph->del = 1;
138988bec41SRichard Weinberger 		orph->dnext = c->orph_dnext;
139988bec41SRichard Weinberger 		c->orph_dnext = orph;
140155fc6baSGeert Uytterhoeven 		dbg_gen("delete later ino %lu", (unsigned long)orph->inum);
141988bec41SRichard Weinberger 		return;
142988bec41SRichard Weinberger 	}
143988bec41SRichard Weinberger 
144988bec41SRichard Weinberger 	__orphan_drop(c, orph);
145988bec41SRichard Weinberger }
146988bec41SRichard Weinberger 
147988bec41SRichard Weinberger /**
148988bec41SRichard Weinberger  * ubifs_add_orphan - add an orphan.
149988bec41SRichard Weinberger  * @c: UBIFS file-system description object
150988bec41SRichard Weinberger  * @inum: orphan inode number
151988bec41SRichard Weinberger  *
152988bec41SRichard Weinberger  * Add an orphan. This function is called when an inodes link count drops to
153988bec41SRichard Weinberger  * zero.
154988bec41SRichard Weinberger  */
ubifs_add_orphan(struct ubifs_info * c,ino_t inum)155988bec41SRichard Weinberger int ubifs_add_orphan(struct ubifs_info *c, ino_t inum)
156988bec41SRichard Weinberger {
157988bec41SRichard Weinberger 	int err = 0;
158988bec41SRichard Weinberger 	ino_t xattr_inum;
159988bec41SRichard Weinberger 	union ubifs_key key;
160927cc5ceSZhihao Cheng 	struct ubifs_dent_node *xent, *pxent = NULL;
161988bec41SRichard Weinberger 	struct fscrypt_name nm = {0};
162988bec41SRichard Weinberger 	struct ubifs_orphan *xattr_orphan;
163988bec41SRichard Weinberger 	struct ubifs_orphan *orphan;
164988bec41SRichard Weinberger 
165988bec41SRichard Weinberger 	orphan = orphan_add(c, inum, NULL);
166988bec41SRichard Weinberger 	if (IS_ERR(orphan))
167988bec41SRichard Weinberger 		return PTR_ERR(orphan);
168988bec41SRichard Weinberger 
169988bec41SRichard Weinberger 	lowest_xent_key(c, &key, inum);
170988bec41SRichard Weinberger 	while (1) {
171988bec41SRichard Weinberger 		xent = ubifs_tnc_next_ent(c, &key, &nm);
172988bec41SRichard Weinberger 		if (IS_ERR(xent)) {
173988bec41SRichard Weinberger 			err = PTR_ERR(xent);
174988bec41SRichard Weinberger 			if (err == -ENOENT)
175988bec41SRichard Weinberger 				break;
176f2aae745SZhihao Cheng 			kfree(pxent);
177988bec41SRichard Weinberger 			return err;
178988bec41SRichard Weinberger 		}
179988bec41SRichard Weinberger 
180988bec41SRichard Weinberger 		fname_name(&nm) = xent->name;
181988bec41SRichard Weinberger 		fname_len(&nm) = le16_to_cpu(xent->nlen);
182988bec41SRichard Weinberger 		xattr_inum = le64_to_cpu(xent->inum);
183988bec41SRichard Weinberger 
184988bec41SRichard Weinberger 		xattr_orphan = orphan_add(c, xattr_inum, orphan);
185927cc5ceSZhihao Cheng 		if (IS_ERR(xattr_orphan)) {
186f2aae745SZhihao Cheng 			kfree(pxent);
187927cc5ceSZhihao Cheng 			kfree(xent);
188988bec41SRichard Weinberger 			return PTR_ERR(xattr_orphan);
189927cc5ceSZhihao Cheng 		}
190988bec41SRichard Weinberger 
191927cc5ceSZhihao Cheng 		kfree(pxent);
192927cc5ceSZhihao Cheng 		pxent = xent;
193988bec41SRichard Weinberger 		key_read(c, &xent->key, &key);
194988bec41SRichard Weinberger 	}
195927cc5ceSZhihao Cheng 	kfree(pxent);
196988bec41SRichard Weinberger 
1971e51764aSArtem Bityutskiy 	return 0;
1981e51764aSArtem Bityutskiy }
1991e51764aSArtem Bityutskiy 
2001e51764aSArtem Bityutskiy /**
2011e51764aSArtem Bityutskiy  * ubifs_delete_orphan - delete an orphan.
2021e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
2031e51764aSArtem Bityutskiy  * @inum: orphan inode number
2041e51764aSArtem Bityutskiy  *
2051e51764aSArtem Bityutskiy  * Delete an orphan. This function is called when an inode is deleted.
2061e51764aSArtem Bityutskiy  */
ubifs_delete_orphan(struct ubifs_info * c,ino_t inum)2071e51764aSArtem Bityutskiy void ubifs_delete_orphan(struct ubifs_info *c, ino_t inum)
2081e51764aSArtem Bityutskiy {
2098009ce95SRichard Weinberger 	struct ubifs_orphan *orph, *child_orph, *tmp_o;
2108009ce95SRichard Weinberger 
2118009ce95SRichard Weinberger 	spin_lock(&c->orphan_lock);
2128009ce95SRichard Weinberger 
2138009ce95SRichard Weinberger 	orph = lookup_orphan(c, inum);
2148009ce95SRichard Weinberger 	if (!orph) {
2158009ce95SRichard Weinberger 		spin_unlock(&c->orphan_lock);
2168009ce95SRichard Weinberger 		ubifs_err(c, "missing orphan ino %lu", (unsigned long)inum);
2178009ce95SRichard Weinberger 		dump_stack();
2188009ce95SRichard Weinberger 
2198009ce95SRichard Weinberger 		return;
2208009ce95SRichard Weinberger 	}
2218009ce95SRichard Weinberger 
2228009ce95SRichard Weinberger 	list_for_each_entry_safe(child_orph, tmp_o, &orph->child_list, child_list) {
2238009ce95SRichard Weinberger 		list_del(&child_orph->child_list);
2248009ce95SRichard Weinberger 		orphan_delete(c, child_orph);
2258009ce95SRichard Weinberger 	}
2268009ce95SRichard Weinberger 
2278009ce95SRichard Weinberger 	orphan_delete(c, orph);
2288009ce95SRichard Weinberger 
2298009ce95SRichard Weinberger 	spin_unlock(&c->orphan_lock);
2301e51764aSArtem Bityutskiy }
2311e51764aSArtem Bityutskiy 
2321e51764aSArtem Bityutskiy /**
2331e51764aSArtem Bityutskiy  * ubifs_orphan_start_commit - start commit of orphans.
2341e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
2351e51764aSArtem Bityutskiy  *
2361e51764aSArtem Bityutskiy  * Start commit of orphans.
2371e51764aSArtem Bityutskiy  */
ubifs_orphan_start_commit(struct ubifs_info * c)2381e51764aSArtem Bityutskiy int ubifs_orphan_start_commit(struct ubifs_info *c)
2391e51764aSArtem Bityutskiy {
2401e51764aSArtem Bityutskiy 	struct ubifs_orphan *orphan, **last;
2411e51764aSArtem Bityutskiy 
2421e51764aSArtem Bityutskiy 	spin_lock(&c->orphan_lock);
2431e51764aSArtem Bityutskiy 	last = &c->orph_cnext;
2441e51764aSArtem Bityutskiy 	list_for_each_entry(orphan, &c->orph_new, new_list) {
2456eb61d58SRichard Weinberger 		ubifs_assert(c, orphan->new);
2466eb61d58SRichard Weinberger 		ubifs_assert(c, !orphan->cmt);
2471e51764aSArtem Bityutskiy 		orphan->new = 0;
2482928f0d0SAdam Thomas 		orphan->cmt = 1;
2491e51764aSArtem Bityutskiy 		*last = orphan;
2501e51764aSArtem Bityutskiy 		last = &orphan->cnext;
2511e51764aSArtem Bityutskiy 	}
2527074e5ebSJulia Lawall 	*last = NULL;
2531e51764aSArtem Bityutskiy 	c->cmt_orphans = c->new_orphans;
2541e51764aSArtem Bityutskiy 	c->new_orphans = 0;
2551e51764aSArtem Bityutskiy 	dbg_cmt("%d orphans to commit", c->cmt_orphans);
2561e51764aSArtem Bityutskiy 	INIT_LIST_HEAD(&c->orph_new);
2571e51764aSArtem Bityutskiy 	if (c->tot_orphans == 0)
2581e51764aSArtem Bityutskiy 		c->no_orphs = 1;
2591e51764aSArtem Bityutskiy 	else
2601e51764aSArtem Bityutskiy 		c->no_orphs = 0;
2611e51764aSArtem Bityutskiy 	spin_unlock(&c->orphan_lock);
2621e51764aSArtem Bityutskiy 	return 0;
2631e51764aSArtem Bityutskiy }
2641e51764aSArtem Bityutskiy 
2651e51764aSArtem Bityutskiy /**
2661e51764aSArtem Bityutskiy  * avail_orphs - calculate available space.
2671e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
2681e51764aSArtem Bityutskiy  *
2691e51764aSArtem Bityutskiy  * This function returns the number of orphans that can be written in the
2701e51764aSArtem Bityutskiy  * available space.
2711e51764aSArtem Bityutskiy  */
avail_orphs(struct ubifs_info * c)2721e51764aSArtem Bityutskiy static int avail_orphs(struct ubifs_info *c)
2731e51764aSArtem Bityutskiy {
2741e51764aSArtem Bityutskiy 	int avail_lebs, avail, gap;
2751e51764aSArtem Bityutskiy 
2761e51764aSArtem Bityutskiy 	avail_lebs = c->orph_lebs - (c->ohead_lnum - c->orph_first) - 1;
2771e51764aSArtem Bityutskiy 	avail = avail_lebs *
2781e51764aSArtem Bityutskiy 	       ((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
2791e51764aSArtem Bityutskiy 	gap = c->leb_size - c->ohead_offs;
2801e51764aSArtem Bityutskiy 	if (gap >= UBIFS_ORPH_NODE_SZ + sizeof(__le64))
2811e51764aSArtem Bityutskiy 		avail += (gap - UBIFS_ORPH_NODE_SZ) / sizeof(__le64);
2821e51764aSArtem Bityutskiy 	return avail;
2831e51764aSArtem Bityutskiy }
2841e51764aSArtem Bityutskiy 
2851e51764aSArtem Bityutskiy /**
2861e51764aSArtem Bityutskiy  * tot_avail_orphs - calculate total space.
2871e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
2881e51764aSArtem Bityutskiy  *
2891e51764aSArtem Bityutskiy  * This function returns the number of orphans that can be written in half
2901e51764aSArtem Bityutskiy  * the total space. That leaves half the space for adding new orphans.
2911e51764aSArtem Bityutskiy  */
tot_avail_orphs(struct ubifs_info * c)2921e51764aSArtem Bityutskiy static int tot_avail_orphs(struct ubifs_info *c)
2931e51764aSArtem Bityutskiy {
2941e51764aSArtem Bityutskiy 	int avail_lebs, avail;
2951e51764aSArtem Bityutskiy 
2961e51764aSArtem Bityutskiy 	avail_lebs = c->orph_lebs;
2971e51764aSArtem Bityutskiy 	avail = avail_lebs *
2981e51764aSArtem Bityutskiy 	       ((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
2991e51764aSArtem Bityutskiy 	return avail / 2;
3001e51764aSArtem Bityutskiy }
3011e51764aSArtem Bityutskiy 
3021e51764aSArtem Bityutskiy /**
30349d128aaSAdrian Hunter  * do_write_orph_node - write a node to the orphan head.
3041e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
3051e51764aSArtem Bityutskiy  * @len: length of node
3061e51764aSArtem Bityutskiy  * @atomic: write atomically
3071e51764aSArtem Bityutskiy  *
3081e51764aSArtem Bityutskiy  * This function writes a node to the orphan head from the orphan buffer. If
3091e51764aSArtem Bityutskiy  * %atomic is not zero, then the write is done atomically. On success, %0 is
3101e51764aSArtem Bityutskiy  * returned, otherwise a negative error code is returned.
3111e51764aSArtem Bityutskiy  */
do_write_orph_node(struct ubifs_info * c,int len,int atomic)3121e51764aSArtem Bityutskiy static int do_write_orph_node(struct ubifs_info *c, int len, int atomic)
3131e51764aSArtem Bityutskiy {
3141e51764aSArtem Bityutskiy 	int err = 0;
3151e51764aSArtem Bityutskiy 
3161e51764aSArtem Bityutskiy 	if (atomic) {
3176eb61d58SRichard Weinberger 		ubifs_assert(c, c->ohead_offs == 0);
3181e51764aSArtem Bityutskiy 		ubifs_prepare_node(c, c->orph_buf, len, 1);
3191e51764aSArtem Bityutskiy 		len = ALIGN(len, c->min_io_size);
320b36a261eSRichard Weinberger 		err = ubifs_leb_change(c, c->ohead_lnum, c->orph_buf, len);
3211e51764aSArtem Bityutskiy 	} else {
3221e51764aSArtem Bityutskiy 		if (c->ohead_offs == 0) {
3231e51764aSArtem Bityutskiy 			/* Ensure LEB has been unmapped */
3241e51764aSArtem Bityutskiy 			err = ubifs_leb_unmap(c, c->ohead_lnum);
3251e51764aSArtem Bityutskiy 			if (err)
3261e51764aSArtem Bityutskiy 				return err;
3271e51764aSArtem Bityutskiy 		}
3281e51764aSArtem Bityutskiy 		err = ubifs_write_node(c, c->orph_buf, len, c->ohead_lnum,
329b36a261eSRichard Weinberger 				       c->ohead_offs);
3301e51764aSArtem Bityutskiy 	}
3311e51764aSArtem Bityutskiy 	return err;
3321e51764aSArtem Bityutskiy }
3331e51764aSArtem Bityutskiy 
3341e51764aSArtem Bityutskiy /**
33549d128aaSAdrian Hunter  * write_orph_node - write an orphan node.
3361e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
3371e51764aSArtem Bityutskiy  * @atomic: write atomically
3381e51764aSArtem Bityutskiy  *
33949d128aaSAdrian Hunter  * This function builds an orphan node from the cnext list and writes it to the
3401e51764aSArtem Bityutskiy  * orphan head. On success, %0 is returned, otherwise a negative error code
3411e51764aSArtem Bityutskiy  * is returned.
3421e51764aSArtem Bityutskiy  */
write_orph_node(struct ubifs_info * c,int atomic)3431e51764aSArtem Bityutskiy static int write_orph_node(struct ubifs_info *c, int atomic)
3441e51764aSArtem Bityutskiy {
3451e51764aSArtem Bityutskiy 	struct ubifs_orphan *orphan, *cnext;
3461e51764aSArtem Bityutskiy 	struct ubifs_orph_node *orph;
3471e51764aSArtem Bityutskiy 	int gap, err, len, cnt, i;
3481e51764aSArtem Bityutskiy 
3496eb61d58SRichard Weinberger 	ubifs_assert(c, c->cmt_orphans > 0);
3501e51764aSArtem Bityutskiy 	gap = c->leb_size - c->ohead_offs;
3511e51764aSArtem Bityutskiy 	if (gap < UBIFS_ORPH_NODE_SZ + sizeof(__le64)) {
3521e51764aSArtem Bityutskiy 		c->ohead_lnum += 1;
3531e51764aSArtem Bityutskiy 		c->ohead_offs = 0;
3541e51764aSArtem Bityutskiy 		gap = c->leb_size;
3551e51764aSArtem Bityutskiy 		if (c->ohead_lnum > c->orph_last) {
3561e51764aSArtem Bityutskiy 			/*
3571e51764aSArtem Bityutskiy 			 * We limit the number of orphans so that this should
3581e51764aSArtem Bityutskiy 			 * never happen.
3591e51764aSArtem Bityutskiy 			 */
360235c362bSSheng Yong 			ubifs_err(c, "out of space in orphan area");
3611e51764aSArtem Bityutskiy 			return -EINVAL;
3621e51764aSArtem Bityutskiy 		}
3631e51764aSArtem Bityutskiy 	}
3641e51764aSArtem Bityutskiy 	cnt = (gap - UBIFS_ORPH_NODE_SZ) / sizeof(__le64);
3651e51764aSArtem Bityutskiy 	if (cnt > c->cmt_orphans)
3661e51764aSArtem Bityutskiy 		cnt = c->cmt_orphans;
3671e51764aSArtem Bityutskiy 	len = UBIFS_ORPH_NODE_SZ + cnt * sizeof(__le64);
3686eb61d58SRichard Weinberger 	ubifs_assert(c, c->orph_buf);
3691e51764aSArtem Bityutskiy 	orph = c->orph_buf;
3701e51764aSArtem Bityutskiy 	orph->ch.node_type = UBIFS_ORPH_NODE;
3711e51764aSArtem Bityutskiy 	spin_lock(&c->orphan_lock);
3721e51764aSArtem Bityutskiy 	cnext = c->orph_cnext;
3731e51764aSArtem Bityutskiy 	for (i = 0; i < cnt; i++) {
3741e51764aSArtem Bityutskiy 		orphan = cnext;
3756eb61d58SRichard Weinberger 		ubifs_assert(c, orphan->cmt);
3761e51764aSArtem Bityutskiy 		orph->inos[i] = cpu_to_le64(orphan->inum);
3772928f0d0SAdam Thomas 		orphan->cmt = 0;
3781e51764aSArtem Bityutskiy 		cnext = orphan->cnext;
3791e51764aSArtem Bityutskiy 		orphan->cnext = NULL;
3801e51764aSArtem Bityutskiy 	}
3811e51764aSArtem Bityutskiy 	c->orph_cnext = cnext;
3821e51764aSArtem Bityutskiy 	c->cmt_orphans -= cnt;
3831e51764aSArtem Bityutskiy 	spin_unlock(&c->orphan_lock);
3841e51764aSArtem Bityutskiy 	if (c->cmt_orphans)
385014eb04bSArtem Bityutskiy 		orph->cmt_no = cpu_to_le64(c->cmt_no);
3861e51764aSArtem Bityutskiy 	else
3871e51764aSArtem Bityutskiy 		/* Mark the last node of the commit */
388014eb04bSArtem Bityutskiy 		orph->cmt_no = cpu_to_le64((c->cmt_no) | (1ULL << 63));
3896eb61d58SRichard Weinberger 	ubifs_assert(c, c->ohead_offs + len <= c->leb_size);
3906eb61d58SRichard Weinberger 	ubifs_assert(c, c->ohead_lnum >= c->orph_first);
3916eb61d58SRichard Weinberger 	ubifs_assert(c, c->ohead_lnum <= c->orph_last);
3921e51764aSArtem Bityutskiy 	err = do_write_orph_node(c, len, atomic);
3931e51764aSArtem Bityutskiy 	c->ohead_offs += ALIGN(len, c->min_io_size);
3941e51764aSArtem Bityutskiy 	c->ohead_offs = ALIGN(c->ohead_offs, 8);
3951e51764aSArtem Bityutskiy 	return err;
3961e51764aSArtem Bityutskiy }
3971e51764aSArtem Bityutskiy 
3981e51764aSArtem Bityutskiy /**
39949d128aaSAdrian Hunter  * write_orph_nodes - write orphan nodes until there are no more to commit.
4001e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
4011e51764aSArtem Bityutskiy  * @atomic: write atomically
4021e51764aSArtem Bityutskiy  *
40349d128aaSAdrian Hunter  * This function writes orphan nodes for all the orphans to commit. On success,
4041e51764aSArtem Bityutskiy  * %0 is returned, otherwise a negative error code is returned.
4051e51764aSArtem Bityutskiy  */
write_orph_nodes(struct ubifs_info * c,int atomic)4061e51764aSArtem Bityutskiy static int write_orph_nodes(struct ubifs_info *c, int atomic)
4071e51764aSArtem Bityutskiy {
4081e51764aSArtem Bityutskiy 	int err;
4091e51764aSArtem Bityutskiy 
4101e51764aSArtem Bityutskiy 	while (c->cmt_orphans > 0) {
4111e51764aSArtem Bityutskiy 		err = write_orph_node(c, atomic);
4121e51764aSArtem Bityutskiy 		if (err)
4131e51764aSArtem Bityutskiy 			return err;
4141e51764aSArtem Bityutskiy 	}
4151e51764aSArtem Bityutskiy 	if (atomic) {
4161e51764aSArtem Bityutskiy 		int lnum;
4171e51764aSArtem Bityutskiy 
4181e51764aSArtem Bityutskiy 		/* Unmap any unused LEBs after consolidation */
4191e51764aSArtem Bityutskiy 		for (lnum = c->ohead_lnum + 1; lnum <= c->orph_last; lnum++) {
4201e51764aSArtem Bityutskiy 			err = ubifs_leb_unmap(c, lnum);
4211e51764aSArtem Bityutskiy 			if (err)
4221e51764aSArtem Bityutskiy 				return err;
4231e51764aSArtem Bityutskiy 		}
4241e51764aSArtem Bityutskiy 	}
4251e51764aSArtem Bityutskiy 	return 0;
4261e51764aSArtem Bityutskiy }
4271e51764aSArtem Bityutskiy 
4281e51764aSArtem Bityutskiy /**
4291e51764aSArtem Bityutskiy  * consolidate - consolidate the orphan area.
4301e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
4311e51764aSArtem Bityutskiy  *
4321e51764aSArtem Bityutskiy  * This function enables consolidation by putting all the orphans into the list
4331e51764aSArtem Bityutskiy  * to commit. The list is in the order that the orphans were added, and the
4341e51764aSArtem Bityutskiy  * LEBs are written atomically in order, so at no time can orphans be lost by
4351e51764aSArtem Bityutskiy  * an unclean unmount.
4361e51764aSArtem Bityutskiy  *
4371e51764aSArtem Bityutskiy  * This function returns %0 on success and a negative error code on failure.
4381e51764aSArtem Bityutskiy  */
consolidate(struct ubifs_info * c)4391e51764aSArtem Bityutskiy static int consolidate(struct ubifs_info *c)
4401e51764aSArtem Bityutskiy {
4411e51764aSArtem Bityutskiy 	int tot_avail = tot_avail_orphs(c), err = 0;
4421e51764aSArtem Bityutskiy 
4431e51764aSArtem Bityutskiy 	spin_lock(&c->orphan_lock);
4441e51764aSArtem Bityutskiy 	dbg_cmt("there is space for %d orphans and there are %d",
4451e51764aSArtem Bityutskiy 		tot_avail, c->tot_orphans);
4461e51764aSArtem Bityutskiy 	if (c->tot_orphans - c->new_orphans <= tot_avail) {
4471e51764aSArtem Bityutskiy 		struct ubifs_orphan *orphan, **last;
4481e51764aSArtem Bityutskiy 		int cnt = 0;
4491e51764aSArtem Bityutskiy 
4501e51764aSArtem Bityutskiy 		/* Change the cnext list to include all non-new orphans */
4511e51764aSArtem Bityutskiy 		last = &c->orph_cnext;
4521e51764aSArtem Bityutskiy 		list_for_each_entry(orphan, &c->orph_list, list) {
4531e51764aSArtem Bityutskiy 			if (orphan->new)
4541e51764aSArtem Bityutskiy 				continue;
4552928f0d0SAdam Thomas 			orphan->cmt = 1;
4561e51764aSArtem Bityutskiy 			*last = orphan;
4571e51764aSArtem Bityutskiy 			last = &orphan->cnext;
4581e51764aSArtem Bityutskiy 			cnt += 1;
4591e51764aSArtem Bityutskiy 		}
4607074e5ebSJulia Lawall 		*last = NULL;
4616eb61d58SRichard Weinberger 		ubifs_assert(c, cnt == c->tot_orphans - c->new_orphans);
4621e51764aSArtem Bityutskiy 		c->cmt_orphans = cnt;
4631e51764aSArtem Bityutskiy 		c->ohead_lnum = c->orph_first;
4641e51764aSArtem Bityutskiy 		c->ohead_offs = 0;
4651e51764aSArtem Bityutskiy 	} else {
4661e51764aSArtem Bityutskiy 		/*
4671e51764aSArtem Bityutskiy 		 * We limit the number of orphans so that this should
4681e51764aSArtem Bityutskiy 		 * never happen.
4691e51764aSArtem Bityutskiy 		 */
470235c362bSSheng Yong 		ubifs_err(c, "out of space in orphan area");
4711e51764aSArtem Bityutskiy 		err = -EINVAL;
4721e51764aSArtem Bityutskiy 	}
4731e51764aSArtem Bityutskiy 	spin_unlock(&c->orphan_lock);
4741e51764aSArtem Bityutskiy 	return err;
4751e51764aSArtem Bityutskiy }
4761e51764aSArtem Bityutskiy 
4771e51764aSArtem Bityutskiy /**
4781e51764aSArtem Bityutskiy  * commit_orphans - commit orphans.
4791e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
4801e51764aSArtem Bityutskiy  *
4811e51764aSArtem Bityutskiy  * This function commits orphans to flash. On success, %0 is returned,
4821e51764aSArtem Bityutskiy  * otherwise a negative error code is returned.
4831e51764aSArtem Bityutskiy  */
commit_orphans(struct ubifs_info * c)4841e51764aSArtem Bityutskiy static int commit_orphans(struct ubifs_info *c)
4851e51764aSArtem Bityutskiy {
4861e51764aSArtem Bityutskiy 	int avail, atomic = 0, err;
4871e51764aSArtem Bityutskiy 
4886eb61d58SRichard Weinberger 	ubifs_assert(c, c->cmt_orphans > 0);
4891e51764aSArtem Bityutskiy 	avail = avail_orphs(c);
4901e51764aSArtem Bityutskiy 	if (avail < c->cmt_orphans) {
4911e51764aSArtem Bityutskiy 		/* Not enough space to write new orphans, so consolidate */
4921e51764aSArtem Bityutskiy 		err = consolidate(c);
4931e51764aSArtem Bityutskiy 		if (err)
4941e51764aSArtem Bityutskiy 			return err;
4951e51764aSArtem Bityutskiy 		atomic = 1;
4961e51764aSArtem Bityutskiy 	}
4971e51764aSArtem Bityutskiy 	err = write_orph_nodes(c, atomic);
4981e51764aSArtem Bityutskiy 	return err;
4991e51764aSArtem Bityutskiy }
5001e51764aSArtem Bityutskiy 
5011e51764aSArtem Bityutskiy /**
5021e51764aSArtem Bityutskiy  * erase_deleted - erase the orphans marked for deletion.
5031e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
5041e51764aSArtem Bityutskiy  *
5051e51764aSArtem Bityutskiy  * During commit, the orphans being committed cannot be deleted, so they are
5061e51764aSArtem Bityutskiy  * marked for deletion and deleted by this function. Also, the recovery
5071e51764aSArtem Bityutskiy  * adds killed orphans to the deletion list, and therefore they are deleted
5081e51764aSArtem Bityutskiy  * here too.
5091e51764aSArtem Bityutskiy  */
erase_deleted(struct ubifs_info * c)5101e51764aSArtem Bityutskiy static void erase_deleted(struct ubifs_info *c)
5111e51764aSArtem Bityutskiy {
5121e51764aSArtem Bityutskiy 	struct ubifs_orphan *orphan, *dnext;
5131e51764aSArtem Bityutskiy 
5141e51764aSArtem Bityutskiy 	spin_lock(&c->orphan_lock);
5151e51764aSArtem Bityutskiy 	dnext = c->orph_dnext;
5161e51764aSArtem Bityutskiy 	while (dnext) {
5171e51764aSArtem Bityutskiy 		orphan = dnext;
5181e51764aSArtem Bityutskiy 		dnext = orphan->dnext;
5196eb61d58SRichard Weinberger 		ubifs_assert(c, !orphan->new);
5206eb61d58SRichard Weinberger 		ubifs_assert(c, orphan->del);
5211e51764aSArtem Bityutskiy 		rb_erase(&orphan->rb, &c->orph_tree);
5221e51764aSArtem Bityutskiy 		list_del(&orphan->list);
5231e51764aSArtem Bityutskiy 		c->tot_orphans -= 1;
524e84461adSArtem Bityutskiy 		dbg_gen("deleting orphan ino %lu", (unsigned long)orphan->inum);
5251e51764aSArtem Bityutskiy 		kfree(orphan);
5261e51764aSArtem Bityutskiy 	}
5271e51764aSArtem Bityutskiy 	c->orph_dnext = NULL;
5281e51764aSArtem Bityutskiy 	spin_unlock(&c->orphan_lock);
5291e51764aSArtem Bityutskiy }
5301e51764aSArtem Bityutskiy 
5311e51764aSArtem Bityutskiy /**
5321e51764aSArtem Bityutskiy  * ubifs_orphan_end_commit - end commit of orphans.
5331e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
5341e51764aSArtem Bityutskiy  *
5351e51764aSArtem Bityutskiy  * End commit of orphans.
5361e51764aSArtem Bityutskiy  */
ubifs_orphan_end_commit(struct ubifs_info * c)5371e51764aSArtem Bityutskiy int ubifs_orphan_end_commit(struct ubifs_info *c)
5381e51764aSArtem Bityutskiy {
5391e51764aSArtem Bityutskiy 	int err;
5401e51764aSArtem Bityutskiy 
5411e51764aSArtem Bityutskiy 	if (c->cmt_orphans != 0) {
5421e51764aSArtem Bityutskiy 		err = commit_orphans(c);
5431e51764aSArtem Bityutskiy 		if (err)
5441e51764aSArtem Bityutskiy 			return err;
5451e51764aSArtem Bityutskiy 	}
5461e51764aSArtem Bityutskiy 	erase_deleted(c);
5471e51764aSArtem Bityutskiy 	err = dbg_check_orphans(c);
5481e51764aSArtem Bityutskiy 	return err;
5491e51764aSArtem Bityutskiy }
5501e51764aSArtem Bityutskiy 
5511e51764aSArtem Bityutskiy /**
55249d128aaSAdrian Hunter  * ubifs_clear_orphans - erase all LEBs used for orphans.
5531e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
5541e51764aSArtem Bityutskiy  *
5551e51764aSArtem Bityutskiy  * If recovery is not required, then the orphans from the previous session
5561e51764aSArtem Bityutskiy  * are not needed. This function locates the LEBs used to record
5571e51764aSArtem Bityutskiy  * orphans, and un-maps them.
5581e51764aSArtem Bityutskiy  */
ubifs_clear_orphans(struct ubifs_info * c)55949d128aaSAdrian Hunter int ubifs_clear_orphans(struct ubifs_info *c)
5601e51764aSArtem Bityutskiy {
5611e51764aSArtem Bityutskiy 	int lnum, err;
5621e51764aSArtem Bityutskiy 
5631e51764aSArtem Bityutskiy 	for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
5641e51764aSArtem Bityutskiy 		err = ubifs_leb_unmap(c, lnum);
5651e51764aSArtem Bityutskiy 		if (err)
5661e51764aSArtem Bityutskiy 			return err;
5671e51764aSArtem Bityutskiy 	}
5681e51764aSArtem Bityutskiy 	c->ohead_lnum = c->orph_first;
5691e51764aSArtem Bityutskiy 	c->ohead_offs = 0;
5701e51764aSArtem Bityutskiy 	return 0;
5711e51764aSArtem Bityutskiy }
5721e51764aSArtem Bityutskiy 
5731e51764aSArtem Bityutskiy /**
5741e51764aSArtem Bityutskiy  * insert_dead_orphan - insert an orphan.
5751e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
5761e51764aSArtem Bityutskiy  * @inum: orphan inode number
5771e51764aSArtem Bityutskiy  *
5781e51764aSArtem Bityutskiy  * This function is a helper to the 'do_kill_orphans()' function. The orphan
5791e51764aSArtem Bityutskiy  * must be kept until the next commit, so it is added to the rb-tree and the
5801e51764aSArtem Bityutskiy  * deletion list.
5811e51764aSArtem Bityutskiy  */
insert_dead_orphan(struct ubifs_info * c,ino_t inum)5821e51764aSArtem Bityutskiy static int insert_dead_orphan(struct ubifs_info *c, ino_t inum)
5831e51764aSArtem Bityutskiy {
5841e51764aSArtem Bityutskiy 	struct ubifs_orphan *orphan, *o;
5851e51764aSArtem Bityutskiy 	struct rb_node **p, *parent = NULL;
5861e51764aSArtem Bityutskiy 
5871e51764aSArtem Bityutskiy 	orphan = kzalloc(sizeof(struct ubifs_orphan), GFP_KERNEL);
5881e51764aSArtem Bityutskiy 	if (!orphan)
5891e51764aSArtem Bityutskiy 		return -ENOMEM;
5901e51764aSArtem Bityutskiy 	orphan->inum = inum;
5911e51764aSArtem Bityutskiy 
5921e51764aSArtem Bityutskiy 	p = &c->orph_tree.rb_node;
5931e51764aSArtem Bityutskiy 	while (*p) {
5941e51764aSArtem Bityutskiy 		parent = *p;
5951e51764aSArtem Bityutskiy 		o = rb_entry(parent, struct ubifs_orphan, rb);
5961e51764aSArtem Bityutskiy 		if (inum < o->inum)
5971e51764aSArtem Bityutskiy 			p = &(*p)->rb_left;
5981e51764aSArtem Bityutskiy 		else if (inum > o->inum)
5991e51764aSArtem Bityutskiy 			p = &(*p)->rb_right;
6001e51764aSArtem Bityutskiy 		else {
6011e51764aSArtem Bityutskiy 			/* Already added - no problem */
6021e51764aSArtem Bityutskiy 			kfree(orphan);
6031e51764aSArtem Bityutskiy 			return 0;
6041e51764aSArtem Bityutskiy 		}
6051e51764aSArtem Bityutskiy 	}
6061e51764aSArtem Bityutskiy 	c->tot_orphans += 1;
6071e51764aSArtem Bityutskiy 	rb_link_node(&orphan->rb, parent, p);
6081e51764aSArtem Bityutskiy 	rb_insert_color(&orphan->rb, &c->orph_tree);
6091e51764aSArtem Bityutskiy 	list_add_tail(&orphan->list, &c->orph_list);
6108afd500cSAdam Thomas 	orphan->del = 1;
6111e51764aSArtem Bityutskiy 	orphan->dnext = c->orph_dnext;
6121e51764aSArtem Bityutskiy 	c->orph_dnext = orphan;
613e84461adSArtem Bityutskiy 	dbg_mnt("ino %lu, new %d, tot %d", (unsigned long)inum,
614e84461adSArtem Bityutskiy 		c->new_orphans, c->tot_orphans);
6151e51764aSArtem Bityutskiy 	return 0;
6161e51764aSArtem Bityutskiy }
6171e51764aSArtem Bityutskiy 
6181e51764aSArtem Bityutskiy /**
6191e51764aSArtem Bityutskiy  * do_kill_orphans - remove orphan inodes from the index.
6201e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
6211e51764aSArtem Bityutskiy  * @sleb: scanned LEB
62249d128aaSAdrian Hunter  * @last_cmt_no: cmt_no of last orphan node read is passed and returned here
6231e51764aSArtem Bityutskiy  * @outofdate: whether the LEB is out of date is returned here
62449d128aaSAdrian Hunter  * @last_flagged: whether the end orphan node is encountered
6251e51764aSArtem Bityutskiy  *
6261e51764aSArtem Bityutskiy  * This function is a helper to the 'kill_orphans()' function. It goes through
6271e51764aSArtem Bityutskiy  * every orphan node in a LEB and for every inode number recorded, removes
6281e51764aSArtem Bityutskiy  * all keys for that inode from the TNC.
6291e51764aSArtem Bityutskiy  */
do_kill_orphans(struct ubifs_info * c,struct ubifs_scan_leb * sleb,unsigned long long * last_cmt_no,int * outofdate,int * last_flagged)6301e51764aSArtem Bityutskiy static int do_kill_orphans(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
6311e51764aSArtem Bityutskiy 			   unsigned long long *last_cmt_no, int *outofdate,
6321e51764aSArtem Bityutskiy 			   int *last_flagged)
6331e51764aSArtem Bityutskiy {
6341e51764aSArtem Bityutskiy 	struct ubifs_scan_node *snod;
6351e51764aSArtem Bityutskiy 	struct ubifs_orph_node *orph;
636ee1438ceSRichard Weinberger 	struct ubifs_ino_node *ino = NULL;
6371e51764aSArtem Bityutskiy 	unsigned long long cmt_no;
6381e51764aSArtem Bityutskiy 	ino_t inum;
6391e51764aSArtem Bityutskiy 	int i, n, err, first = 1;
6401e51764aSArtem Bityutskiy 
64110256f00SZhihao Cheng 	ino = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
64210256f00SZhihao Cheng 	if (!ino)
64310256f00SZhihao Cheng 		return -ENOMEM;
64410256f00SZhihao Cheng 
6451e51764aSArtem Bityutskiy 	list_for_each_entry(snod, &sleb->nodes, list) {
6461e51764aSArtem Bityutskiy 		if (snod->type != UBIFS_ORPH_NODE) {
647235c362bSSheng Yong 			ubifs_err(c, "invalid node type %d in orphan area at %d:%d",
64879fda517SArtem Bityutskiy 				  snod->type, sleb->lnum, snod->offs);
649*a33e30a0SZhihao Cheng 			ubifs_dump_node(c, snod->node,
650*a33e30a0SZhihao Cheng 					c->leb_size - snod->offs);
65110256f00SZhihao Cheng 			err = -EINVAL;
65210256f00SZhihao Cheng 			goto out_free;
6531e51764aSArtem Bityutskiy 		}
6541e51764aSArtem Bityutskiy 
6551e51764aSArtem Bityutskiy 		orph = snod->node;
6561e51764aSArtem Bityutskiy 
6571e51764aSArtem Bityutskiy 		/* Check commit number */
6581e51764aSArtem Bityutskiy 		cmt_no = le64_to_cpu(orph->cmt_no) & LLONG_MAX;
6591e51764aSArtem Bityutskiy 		/*
6601e51764aSArtem Bityutskiy 		 * The commit number on the master node may be less, because
6611e51764aSArtem Bityutskiy 		 * of a failed commit. If there are several failed commits in a
66249d128aaSAdrian Hunter 		 * row, the commit number written on orphan nodes will continue
66349d128aaSAdrian Hunter 		 * to increase (because the commit number is adjusted here) even
6641e51764aSArtem Bityutskiy 		 * though the commit number on the master node stays the same
6651e51764aSArtem Bityutskiy 		 * because the master node has not been re-written.
6661e51764aSArtem Bityutskiy 		 */
6671e51764aSArtem Bityutskiy 		if (cmt_no > c->cmt_no)
6681e51764aSArtem Bityutskiy 			c->cmt_no = cmt_no;
6691e51764aSArtem Bityutskiy 		if (cmt_no < *last_cmt_no && *last_flagged) {
6701e51764aSArtem Bityutskiy 			/*
67149d128aaSAdrian Hunter 			 * The last orphan node had a higher commit number and
67249d128aaSAdrian Hunter 			 * was flagged as the last written for that commit
67349d128aaSAdrian Hunter 			 * number. That makes this orphan node, out of date.
6741e51764aSArtem Bityutskiy 			 */
6751e51764aSArtem Bityutskiy 			if (!first) {
676235c362bSSheng Yong 				ubifs_err(c, "out of order commit number %llu in orphan node at %d:%d",
6771e51764aSArtem Bityutskiy 					  cmt_no, sleb->lnum, snod->offs);
678*a33e30a0SZhihao Cheng 				ubifs_dump_node(c, snod->node,
679*a33e30a0SZhihao Cheng 						c->leb_size - snod->offs);
68010256f00SZhihao Cheng 				err = -EINVAL;
68110256f00SZhihao Cheng 				goto out_free;
6821e51764aSArtem Bityutskiy 			}
6831e51764aSArtem Bityutskiy 			dbg_rcvry("out of date LEB %d", sleb->lnum);
6841e51764aSArtem Bityutskiy 			*outofdate = 1;
68510256f00SZhihao Cheng 			err = 0;
68610256f00SZhihao Cheng 			goto out_free;
6871e51764aSArtem Bityutskiy 		}
6881e51764aSArtem Bityutskiy 
6891e51764aSArtem Bityutskiy 		if (first)
6901e51764aSArtem Bityutskiy 			first = 0;
6911e51764aSArtem Bityutskiy 
6921e51764aSArtem Bityutskiy 		n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
6931e51764aSArtem Bityutskiy 		for (i = 0; i < n; i++) {
694988bec41SRichard Weinberger 			union ubifs_key key1, key2;
695988bec41SRichard Weinberger 
6961e51764aSArtem Bityutskiy 			inum = le64_to_cpu(orph->inos[i]);
697ee1438ceSRichard Weinberger 
698ee1438ceSRichard Weinberger 			ino_key_init(c, &key1, inum);
699ee1438ceSRichard Weinberger 			err = ubifs_tnc_lookup(c, &key1, ino);
7004ab25ac8SRichard Weinberger 			if (err && err != -ENOENT)
701ee1438ceSRichard Weinberger 				goto out_free;
702ee1438ceSRichard Weinberger 
703ee1438ceSRichard Weinberger 			/*
704ee1438ceSRichard Weinberger 			 * Check whether an inode can really get deleted.
705ee1438ceSRichard Weinberger 			 * linkat() with O_TMPFILE allows rebirth of an inode.
706ee1438ceSRichard Weinberger 			 */
7074ab25ac8SRichard Weinberger 			if (err == 0 && ino->nlink == 0) {
708e84461adSArtem Bityutskiy 				dbg_rcvry("deleting orphaned inode %lu",
709e84461adSArtem Bityutskiy 					  (unsigned long)inum);
710988bec41SRichard Weinberger 
711988bec41SRichard Weinberger 				lowest_ino_key(c, &key1, inum);
712988bec41SRichard Weinberger 				highest_ino_key(c, &key2, inum);
713988bec41SRichard Weinberger 
714988bec41SRichard Weinberger 				err = ubifs_tnc_remove_range(c, &key1, &key2);
7151e51764aSArtem Bityutskiy 				if (err)
716ee1438ceSRichard Weinberger 					goto out_ro;
717ee1438ceSRichard Weinberger 			}
718ee1438ceSRichard Weinberger 
7191e51764aSArtem Bityutskiy 			err = insert_dead_orphan(c, inum);
7201e51764aSArtem Bityutskiy 			if (err)
721ee1438ceSRichard Weinberger 				goto out_free;
7221e51764aSArtem Bityutskiy 		}
7231e51764aSArtem Bityutskiy 
7241e51764aSArtem Bityutskiy 		*last_cmt_no = cmt_no;
7251e51764aSArtem Bityutskiy 		if (le64_to_cpu(orph->cmt_no) & (1ULL << 63)) {
7261e51764aSArtem Bityutskiy 			dbg_rcvry("last orph node for commit %llu at %d:%d",
7271e51764aSArtem Bityutskiy 				  cmt_no, sleb->lnum, snod->offs);
7281e51764aSArtem Bityutskiy 			*last_flagged = 1;
7291e51764aSArtem Bityutskiy 		} else
7301e51764aSArtem Bityutskiy 			*last_flagged = 0;
7311e51764aSArtem Bityutskiy 	}
7321e51764aSArtem Bityutskiy 
733ee1438ceSRichard Weinberger 	err = 0;
734ee1438ceSRichard Weinberger out_free:
735ee1438ceSRichard Weinberger 	kfree(ino);
736ee1438ceSRichard Weinberger 	return err;
737ee1438ceSRichard Weinberger 
738ee1438ceSRichard Weinberger out_ro:
739ee1438ceSRichard Weinberger 	ubifs_ro_mode(c, err);
740ee1438ceSRichard Weinberger 	kfree(ino);
741ee1438ceSRichard Weinberger 	return err;
7421e51764aSArtem Bityutskiy }
7431e51764aSArtem Bityutskiy 
7441e51764aSArtem Bityutskiy /**
7451e51764aSArtem Bityutskiy  * kill_orphans - remove all orphan inodes from the index.
7461e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
7471e51764aSArtem Bityutskiy  *
7481e51764aSArtem Bityutskiy  * If recovery is required, then orphan inodes recorded during the previous
7491e51764aSArtem Bityutskiy  * session (which ended with an unclean unmount) must be deleted from the index.
7501e51764aSArtem Bityutskiy  * This is done by updating the TNC, but since the index is not updated until
7511e51764aSArtem Bityutskiy  * the next commit, the LEBs where the orphan information is recorded are not
7521e51764aSArtem Bityutskiy  * erased until the next commit.
7531e51764aSArtem Bityutskiy  */
kill_orphans(struct ubifs_info * c)7541e51764aSArtem Bityutskiy static int kill_orphans(struct ubifs_info *c)
7551e51764aSArtem Bityutskiy {
7561e51764aSArtem Bityutskiy 	unsigned long long last_cmt_no = 0;
7571e51764aSArtem Bityutskiy 	int lnum, err = 0, outofdate = 0, last_flagged = 0;
7581e51764aSArtem Bityutskiy 
7591e51764aSArtem Bityutskiy 	c->ohead_lnum = c->orph_first;
7601e51764aSArtem Bityutskiy 	c->ohead_offs = 0;
7611e51764aSArtem Bityutskiy 	/* Check no-orphans flag and skip this if no orphans */
7621e51764aSArtem Bityutskiy 	if (c->no_orphs) {
7631e51764aSArtem Bityutskiy 		dbg_rcvry("no orphans");
7641e51764aSArtem Bityutskiy 		return 0;
7651e51764aSArtem Bityutskiy 	}
7661e51764aSArtem Bityutskiy 	/*
7671e51764aSArtem Bityutskiy 	 * Orph nodes always start at c->orph_first and are written to each
7681e51764aSArtem Bityutskiy 	 * successive LEB in turn. Generally unused LEBs will have been unmapped
76949d128aaSAdrian Hunter 	 * but may contain out of date orphan nodes if the unmap didn't go
77049d128aaSAdrian Hunter 	 * through. In addition, the last orphan node written for each commit is
7711e51764aSArtem Bityutskiy 	 * marked (top bit of orph->cmt_no is set to 1). It is possible that
77249d128aaSAdrian Hunter 	 * there are orphan nodes from the next commit (i.e. the commit did not
7731e51764aSArtem Bityutskiy 	 * complete successfully). In that case, no orphans will have been lost
7741e51764aSArtem Bityutskiy 	 * due to the way that orphans are written, and any orphans added will
7751e51764aSArtem Bityutskiy 	 * be valid orphans anyway and so can be deleted.
7761e51764aSArtem Bityutskiy 	 */
7771e51764aSArtem Bityutskiy 	for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
7781e51764aSArtem Bityutskiy 		struct ubifs_scan_leb *sleb;
7791e51764aSArtem Bityutskiy 
7801e51764aSArtem Bityutskiy 		dbg_rcvry("LEB %d", lnum);
781348709baSArtem Bityutskiy 		sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
7821e51764aSArtem Bityutskiy 		if (IS_ERR(sleb)) {
7830dcd18e4SArtem Bityutskiy 			if (PTR_ERR(sleb) == -EUCLEAN)
784c4361570SArtem Bityutskiy 				sleb = ubifs_recover_leb(c, lnum, 0,
785efcfde54SArtem Bityutskiy 							 c->sbuf, -1);
7861e51764aSArtem Bityutskiy 			if (IS_ERR(sleb)) {
7871e51764aSArtem Bityutskiy 				err = PTR_ERR(sleb);
7881e51764aSArtem Bityutskiy 				break;
7891e51764aSArtem Bityutskiy 			}
7901e51764aSArtem Bityutskiy 		}
7911e51764aSArtem Bityutskiy 		err = do_kill_orphans(c, sleb, &last_cmt_no, &outofdate,
7921e51764aSArtem Bityutskiy 				      &last_flagged);
7931e51764aSArtem Bityutskiy 		if (err || outofdate) {
7941e51764aSArtem Bityutskiy 			ubifs_scan_destroy(sleb);
7951e51764aSArtem Bityutskiy 			break;
7961e51764aSArtem Bityutskiy 		}
7971e51764aSArtem Bityutskiy 		if (sleb->endpt) {
7981e51764aSArtem Bityutskiy 			c->ohead_lnum = lnum;
7991e51764aSArtem Bityutskiy 			c->ohead_offs = sleb->endpt;
8001e51764aSArtem Bityutskiy 		}
8011e51764aSArtem Bityutskiy 		ubifs_scan_destroy(sleb);
8021e51764aSArtem Bityutskiy 	}
8031e51764aSArtem Bityutskiy 	return err;
8041e51764aSArtem Bityutskiy }
8051e51764aSArtem Bityutskiy 
8061e51764aSArtem Bityutskiy /**
8071e51764aSArtem Bityutskiy  * ubifs_mount_orphans - delete orphan inodes and erase LEBs that recorded them.
8081e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
8091e51764aSArtem Bityutskiy  * @unclean: indicates recovery from unclean unmount
8101e51764aSArtem Bityutskiy  * @read_only: indicates read only mount
8111e51764aSArtem Bityutskiy  *
8121e51764aSArtem Bityutskiy  * This function is called when mounting to erase orphans from the previous
8131e51764aSArtem Bityutskiy  * session. If UBIFS was not unmounted cleanly, then the inodes recorded as
8141e51764aSArtem Bityutskiy  * orphans are deleted.
8151e51764aSArtem Bityutskiy  */
ubifs_mount_orphans(struct ubifs_info * c,int unclean,int read_only)8161e51764aSArtem Bityutskiy int ubifs_mount_orphans(struct ubifs_info *c, int unclean, int read_only)
8171e51764aSArtem Bityutskiy {
8181e51764aSArtem Bityutskiy 	int err = 0;
8191e51764aSArtem Bityutskiy 
8201e51764aSArtem Bityutskiy 	c->max_orphans = tot_avail_orphs(c);
8211e51764aSArtem Bityutskiy 
8221e51764aSArtem Bityutskiy 	if (!read_only) {
8231e51764aSArtem Bityutskiy 		c->orph_buf = vmalloc(c->leb_size);
8241e51764aSArtem Bityutskiy 		if (!c->orph_buf)
8251e51764aSArtem Bityutskiy 			return -ENOMEM;
8261e51764aSArtem Bityutskiy 	}
8271e51764aSArtem Bityutskiy 
8281e51764aSArtem Bityutskiy 	if (unclean)
8291e51764aSArtem Bityutskiy 		err = kill_orphans(c);
8301e51764aSArtem Bityutskiy 	else if (!read_only)
83149d128aaSAdrian Hunter 		err = ubifs_clear_orphans(c);
8321e51764aSArtem Bityutskiy 
8331e51764aSArtem Bityutskiy 	return err;
8341e51764aSArtem Bityutskiy }
8351e51764aSArtem Bityutskiy 
836f70b7e52SArtem Bityutskiy /*
837f70b7e52SArtem Bityutskiy  * Everything below is related to debugging.
838f70b7e52SArtem Bityutskiy  */
8391e51764aSArtem Bityutskiy 
8401e51764aSArtem Bityutskiy struct check_orphan {
8411e51764aSArtem Bityutskiy 	struct rb_node rb;
8421e51764aSArtem Bityutskiy 	ino_t inum;
8431e51764aSArtem Bityutskiy };
8441e51764aSArtem Bityutskiy 
8451e51764aSArtem Bityutskiy struct check_info {
8461e51764aSArtem Bityutskiy 	unsigned long last_ino;
8471e51764aSArtem Bityutskiy 	unsigned long tot_inos;
8481e51764aSArtem Bityutskiy 	unsigned long missing;
8491e51764aSArtem Bityutskiy 	unsigned long long leaf_cnt;
8501e51764aSArtem Bityutskiy 	struct ubifs_ino_node *node;
8511e51764aSArtem Bityutskiy 	struct rb_root root;
8521e51764aSArtem Bityutskiy };
8531e51764aSArtem Bityutskiy 
dbg_find_orphan(struct ubifs_info * c,ino_t inum)854988bec41SRichard Weinberger static bool dbg_find_orphan(struct ubifs_info *c, ino_t inum)
8551e51764aSArtem Bityutskiy {
856988bec41SRichard Weinberger 	bool found = false;
8571e51764aSArtem Bityutskiy 
8581e51764aSArtem Bityutskiy 	spin_lock(&c->orphan_lock);
859988bec41SRichard Weinberger 	found = !!lookup_orphan(c, inum);
8601e51764aSArtem Bityutskiy 	spin_unlock(&c->orphan_lock);
861988bec41SRichard Weinberger 
862988bec41SRichard Weinberger 	return found;
8631e51764aSArtem Bityutskiy }
8641e51764aSArtem Bityutskiy 
dbg_ins_check_orphan(struct rb_root * root,ino_t inum)8651e51764aSArtem Bityutskiy static int dbg_ins_check_orphan(struct rb_root *root, ino_t inum)
8661e51764aSArtem Bityutskiy {
8671e51764aSArtem Bityutskiy 	struct check_orphan *orphan, *o;
8681e51764aSArtem Bityutskiy 	struct rb_node **p, *parent = NULL;
8691e51764aSArtem Bityutskiy 
8701e51764aSArtem Bityutskiy 	orphan = kzalloc(sizeof(struct check_orphan), GFP_NOFS);
8711e51764aSArtem Bityutskiy 	if (!orphan)
8721e51764aSArtem Bityutskiy 		return -ENOMEM;
8731e51764aSArtem Bityutskiy 	orphan->inum = inum;
8741e51764aSArtem Bityutskiy 
8751e51764aSArtem Bityutskiy 	p = &root->rb_node;
8761e51764aSArtem Bityutskiy 	while (*p) {
8771e51764aSArtem Bityutskiy 		parent = *p;
8781e51764aSArtem Bityutskiy 		o = rb_entry(parent, struct check_orphan, rb);
8791e51764aSArtem Bityutskiy 		if (inum < o->inum)
8801e51764aSArtem Bityutskiy 			p = &(*p)->rb_left;
8811e51764aSArtem Bityutskiy 		else if (inum > o->inum)
8821e51764aSArtem Bityutskiy 			p = &(*p)->rb_right;
8831e51764aSArtem Bityutskiy 		else {
8841e51764aSArtem Bityutskiy 			kfree(orphan);
8851e51764aSArtem Bityutskiy 			return 0;
8861e51764aSArtem Bityutskiy 		}
8871e51764aSArtem Bityutskiy 	}
8881e51764aSArtem Bityutskiy 	rb_link_node(&orphan->rb, parent, p);
8891e51764aSArtem Bityutskiy 	rb_insert_color(&orphan->rb, root);
8901e51764aSArtem Bityutskiy 	return 0;
8911e51764aSArtem Bityutskiy }
8921e51764aSArtem Bityutskiy 
dbg_find_check_orphan(struct rb_root * root,ino_t inum)8931e51764aSArtem Bityutskiy static int dbg_find_check_orphan(struct rb_root *root, ino_t inum)
8941e51764aSArtem Bityutskiy {
8951e51764aSArtem Bityutskiy 	struct check_orphan *o;
8961e51764aSArtem Bityutskiy 	struct rb_node *p;
8971e51764aSArtem Bityutskiy 
8981e51764aSArtem Bityutskiy 	p = root->rb_node;
8991e51764aSArtem Bityutskiy 	while (p) {
9001e51764aSArtem Bityutskiy 		o = rb_entry(p, struct check_orphan, rb);
9011e51764aSArtem Bityutskiy 		if (inum < o->inum)
9021e51764aSArtem Bityutskiy 			p = p->rb_left;
9031e51764aSArtem Bityutskiy 		else if (inum > o->inum)
9041e51764aSArtem Bityutskiy 			p = p->rb_right;
9051e51764aSArtem Bityutskiy 		else
9061e51764aSArtem Bityutskiy 			return 1;
9071e51764aSArtem Bityutskiy 	}
9081e51764aSArtem Bityutskiy 	return 0;
9091e51764aSArtem Bityutskiy }
9101e51764aSArtem Bityutskiy 
dbg_free_check_tree(struct rb_root * root)9111e51764aSArtem Bityutskiy static void dbg_free_check_tree(struct rb_root *root)
9121e51764aSArtem Bityutskiy {
913bb25e49fSCody P Schafer 	struct check_orphan *o, *n;
9141e51764aSArtem Bityutskiy 
915bb25e49fSCody P Schafer 	rbtree_postorder_for_each_entry_safe(o, n, root, rb)
9161e51764aSArtem Bityutskiy 		kfree(o);
9171e51764aSArtem Bityutskiy }
9181e51764aSArtem Bityutskiy 
dbg_orphan_check(struct ubifs_info * c,struct ubifs_zbranch * zbr,void * priv)9191e51764aSArtem Bityutskiy static int dbg_orphan_check(struct ubifs_info *c, struct ubifs_zbranch *zbr,
9201e51764aSArtem Bityutskiy 			    void *priv)
9211e51764aSArtem Bityutskiy {
9221e51764aSArtem Bityutskiy 	struct check_info *ci = priv;
9231e51764aSArtem Bityutskiy 	ino_t inum;
9241e51764aSArtem Bityutskiy 	int err;
9251e51764aSArtem Bityutskiy 
9261e51764aSArtem Bityutskiy 	inum = key_inum(c, &zbr->key);
9271e51764aSArtem Bityutskiy 	if (inum != ci->last_ino) {
9281e51764aSArtem Bityutskiy 		/* Lowest node type is the inode node, so it comes first */
9291e51764aSArtem Bityutskiy 		if (key_type(c, &zbr->key) != UBIFS_INO_KEY)
930235c362bSSheng Yong 			ubifs_err(c, "found orphan node ino %lu, type %d",
931e84461adSArtem Bityutskiy 				  (unsigned long)inum, key_type(c, &zbr->key));
9321e51764aSArtem Bityutskiy 		ci->last_ino = inum;
9331e51764aSArtem Bityutskiy 		ci->tot_inos += 1;
9341e51764aSArtem Bityutskiy 		err = ubifs_tnc_read_node(c, zbr, ci->node);
9351e51764aSArtem Bityutskiy 		if (err) {
936235c362bSSheng Yong 			ubifs_err(c, "node read failed, error %d", err);
9371e51764aSArtem Bityutskiy 			return err;
9381e51764aSArtem Bityutskiy 		}
9391e51764aSArtem Bityutskiy 		if (ci->node->nlink == 0)
9401e51764aSArtem Bityutskiy 			/* Must be recorded as an orphan */
9411e51764aSArtem Bityutskiy 			if (!dbg_find_check_orphan(&ci->root, inum) &&
9421e51764aSArtem Bityutskiy 			    !dbg_find_orphan(c, inum)) {
943235c362bSSheng Yong 				ubifs_err(c, "missing orphan, ino %lu",
944e84461adSArtem Bityutskiy 					  (unsigned long)inum);
9451e51764aSArtem Bityutskiy 				ci->missing += 1;
9461e51764aSArtem Bityutskiy 			}
9471e51764aSArtem Bityutskiy 	}
9481e51764aSArtem Bityutskiy 	ci->leaf_cnt += 1;
9491e51764aSArtem Bityutskiy 	return 0;
9501e51764aSArtem Bityutskiy }
9511e51764aSArtem Bityutskiy 
dbg_read_orphans(struct check_info * ci,struct ubifs_scan_leb * sleb)9521e51764aSArtem Bityutskiy static int dbg_read_orphans(struct check_info *ci, struct ubifs_scan_leb *sleb)
9531e51764aSArtem Bityutskiy {
9541e51764aSArtem Bityutskiy 	struct ubifs_scan_node *snod;
9551e51764aSArtem Bityutskiy 	struct ubifs_orph_node *orph;
9561e51764aSArtem Bityutskiy 	ino_t inum;
9571e51764aSArtem Bityutskiy 	int i, n, err;
9581e51764aSArtem Bityutskiy 
9591e51764aSArtem Bityutskiy 	list_for_each_entry(snod, &sleb->nodes, list) {
9601e51764aSArtem Bityutskiy 		cond_resched();
9611e51764aSArtem Bityutskiy 		if (snod->type != UBIFS_ORPH_NODE)
9621e51764aSArtem Bityutskiy 			continue;
9631e51764aSArtem Bityutskiy 		orph = snod->node;
9641e51764aSArtem Bityutskiy 		n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
9651e51764aSArtem Bityutskiy 		for (i = 0; i < n; i++) {
9661e51764aSArtem Bityutskiy 			inum = le64_to_cpu(orph->inos[i]);
9671e51764aSArtem Bityutskiy 			err = dbg_ins_check_orphan(&ci->root, inum);
9681e51764aSArtem Bityutskiy 			if (err)
9691e51764aSArtem Bityutskiy 				return err;
9701e51764aSArtem Bityutskiy 		}
9711e51764aSArtem Bityutskiy 	}
9721e51764aSArtem Bityutskiy 	return 0;
9731e51764aSArtem Bityutskiy }
9741e51764aSArtem Bityutskiy 
dbg_scan_orphans(struct ubifs_info * c,struct check_info * ci)9751e51764aSArtem Bityutskiy static int dbg_scan_orphans(struct ubifs_info *c, struct check_info *ci)
9761e51764aSArtem Bityutskiy {
9771e51764aSArtem Bityutskiy 	int lnum, err = 0;
978f5cf319cSArtem Bityutskiy 	void *buf;
9791e51764aSArtem Bityutskiy 
9801e51764aSArtem Bityutskiy 	/* Check no-orphans flag and skip this if no orphans */
9811e51764aSArtem Bityutskiy 	if (c->no_orphs)
9821e51764aSArtem Bityutskiy 		return 0;
9831e51764aSArtem Bityutskiy 
98488dca4caSChristoph Hellwig 	buf = __vmalloc(c->leb_size, GFP_NOFS);
985f5cf319cSArtem Bityutskiy 	if (!buf) {
986235c362bSSheng Yong 		ubifs_err(c, "cannot allocate memory to check orphans");
987f5cf319cSArtem Bityutskiy 		return 0;
988f5cf319cSArtem Bityutskiy 	}
989f5cf319cSArtem Bityutskiy 
9901e51764aSArtem Bityutskiy 	for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
9911e51764aSArtem Bityutskiy 		struct ubifs_scan_leb *sleb;
9921e51764aSArtem Bityutskiy 
993f5cf319cSArtem Bityutskiy 		sleb = ubifs_scan(c, lnum, 0, buf, 0);
9941e51764aSArtem Bityutskiy 		if (IS_ERR(sleb)) {
9951e51764aSArtem Bityutskiy 			err = PTR_ERR(sleb);
9961e51764aSArtem Bityutskiy 			break;
9971e51764aSArtem Bityutskiy 		}
9981e51764aSArtem Bityutskiy 
9991e51764aSArtem Bityutskiy 		err = dbg_read_orphans(ci, sleb);
10001e51764aSArtem Bityutskiy 		ubifs_scan_destroy(sleb);
10011e51764aSArtem Bityutskiy 		if (err)
10021e51764aSArtem Bityutskiy 			break;
10031e51764aSArtem Bityutskiy 	}
10041e51764aSArtem Bityutskiy 
1005f5cf319cSArtem Bityutskiy 	vfree(buf);
10061e51764aSArtem Bityutskiy 	return err;
10071e51764aSArtem Bityutskiy }
10081e51764aSArtem Bityutskiy 
dbg_check_orphans(struct ubifs_info * c)10091e51764aSArtem Bityutskiy static int dbg_check_orphans(struct ubifs_info *c)
10101e51764aSArtem Bityutskiy {
10111e51764aSArtem Bityutskiy 	struct check_info ci;
10121e51764aSArtem Bityutskiy 	int err;
10131e51764aSArtem Bityutskiy 
10142b1844a8SArtem Bityutskiy 	if (!dbg_is_chk_orph(c))
10151e51764aSArtem Bityutskiy 		return 0;
10161e51764aSArtem Bityutskiy 
10171e51764aSArtem Bityutskiy 	ci.last_ino = 0;
10181e51764aSArtem Bityutskiy 	ci.tot_inos = 0;
10191e51764aSArtem Bityutskiy 	ci.missing  = 0;
10201e51764aSArtem Bityutskiy 	ci.leaf_cnt = 0;
10211e51764aSArtem Bityutskiy 	ci.root = RB_ROOT;
10221e51764aSArtem Bityutskiy 	ci.node = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
10231e51764aSArtem Bityutskiy 	if (!ci.node) {
1024235c362bSSheng Yong 		ubifs_err(c, "out of memory");
10251e51764aSArtem Bityutskiy 		return -ENOMEM;
10261e51764aSArtem Bityutskiy 	}
10271e51764aSArtem Bityutskiy 
10281e51764aSArtem Bityutskiy 	err = dbg_scan_orphans(c, &ci);
10291e51764aSArtem Bityutskiy 	if (err)
10301e51764aSArtem Bityutskiy 		goto out;
10311e51764aSArtem Bityutskiy 
10321e51764aSArtem Bityutskiy 	err = dbg_walk_index(c, &dbg_orphan_check, NULL, &ci);
10331e51764aSArtem Bityutskiy 	if (err) {
1034235c362bSSheng Yong 		ubifs_err(c, "cannot scan TNC, error %d", err);
10351e51764aSArtem Bityutskiy 		goto out;
10361e51764aSArtem Bityutskiy 	}
10371e51764aSArtem Bityutskiy 
10381e51764aSArtem Bityutskiy 	if (ci.missing) {
1039235c362bSSheng Yong 		ubifs_err(c, "%lu missing orphan(s)", ci.missing);
10401e51764aSArtem Bityutskiy 		err = -EINVAL;
10411e51764aSArtem Bityutskiy 		goto out;
10421e51764aSArtem Bityutskiy 	}
10431e51764aSArtem Bityutskiy 
10441e51764aSArtem Bityutskiy 	dbg_cmt("last inode number is %lu", ci.last_ino);
10451e51764aSArtem Bityutskiy 	dbg_cmt("total number of inodes is %lu", ci.tot_inos);
10461e51764aSArtem Bityutskiy 	dbg_cmt("total number of leaf nodes is %llu", ci.leaf_cnt);
10471e51764aSArtem Bityutskiy 
10481e51764aSArtem Bityutskiy out:
10491e51764aSArtem Bityutskiy 	dbg_free_check_tree(&ci.root);
10501e51764aSArtem Bityutskiy 	kfree(ci.node);
10511e51764aSArtem Bityutskiy 	return err;
10521e51764aSArtem Bityutskiy }
1053