hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/fs/ubifs/orphan.c
....@@ -1,20 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * This file is part of UBIFS.
34 *
45 * Copyright (C) 2006-2008 Nokia Corporation.
5
- *
6
- * This program is free software; you can redistribute it and/or modify it
7
- * under the terms of the GNU General Public License version 2 as published by
8
- * the Free Software Foundation.
9
- *
10
- * This program is distributed in the hope that it will be useful, but WITHOUT
11
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13
- * more details.
14
- *
15
- * You should have received a copy of the GNU General Public License along with
16
- * this program; if not, write to the Free Software Foundation, Inc., 51
17
- * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
186 *
197 * Author: Adrian Hunter
208 */
....@@ -54,30 +42,24 @@
5442
5543 static int dbg_check_orphans(struct ubifs_info *c);
5644
57
-/**
58
- * ubifs_add_orphan - add an orphan.
59
- * @c: UBIFS file-system description object
60
- * @inum: orphan inode number
61
- *
62
- * Add an orphan. This function is called when an inodes link count drops to
63
- * zero.
64
- */
65
-int ubifs_add_orphan(struct ubifs_info *c, ino_t inum)
45
+static struct ubifs_orphan *orphan_add(struct ubifs_info *c, ino_t inum,
46
+ struct ubifs_orphan *parent_orphan)
6647 {
6748 struct ubifs_orphan *orphan, *o;
6849 struct rb_node **p, *parent = NULL;
6950
7051 orphan = kzalloc(sizeof(struct ubifs_orphan), GFP_NOFS);
7152 if (!orphan)
72
- return -ENOMEM;
53
+ return ERR_PTR(-ENOMEM);
7354 orphan->inum = inum;
7455 orphan->new = 1;
56
+ INIT_LIST_HEAD(&orphan->child_list);
7557
7658 spin_lock(&c->orphan_lock);
7759 if (c->tot_orphans >= c->max_orphans) {
7860 spin_unlock(&c->orphan_lock);
7961 kfree(orphan);
80
- return -ENFILE;
62
+ return ERR_PTR(-ENFILE);
8163 }
8264 p = &c->orph_tree.rb_node;
8365 while (*p) {
....@@ -91,7 +73,7 @@
9173 ubifs_err(c, "orphaned twice");
9274 spin_unlock(&c->orphan_lock);
9375 kfree(orphan);
94
- return 0;
76
+ return ERR_PTR(-EINVAL);
9577 }
9678 }
9779 c->tot_orphans += 1;
....@@ -100,8 +82,118 @@
10082 rb_insert_color(&orphan->rb, &c->orph_tree);
10183 list_add_tail(&orphan->list, &c->orph_list);
10284 list_add_tail(&orphan->new_list, &c->orph_new);
85
+
86
+ if (parent_orphan) {
87
+ list_add_tail(&orphan->child_list,
88
+ &parent_orphan->child_list);
89
+ }
90
+
10391 spin_unlock(&c->orphan_lock);
10492 dbg_gen("ino %lu", (unsigned long)inum);
93
+ return orphan;
94
+}
95
+
96
+static struct ubifs_orphan *lookup_orphan(struct ubifs_info *c, ino_t inum)
97
+{
98
+ struct ubifs_orphan *o;
99
+ struct rb_node *p;
100
+
101
+ p = c->orph_tree.rb_node;
102
+ while (p) {
103
+ o = rb_entry(p, struct ubifs_orphan, rb);
104
+ if (inum < o->inum)
105
+ p = p->rb_left;
106
+ else if (inum > o->inum)
107
+ p = p->rb_right;
108
+ else {
109
+ return o;
110
+ }
111
+ }
112
+ return NULL;
113
+}
114
+
115
+static void __orphan_drop(struct ubifs_info *c, struct ubifs_orphan *o)
116
+{
117
+ rb_erase(&o->rb, &c->orph_tree);
118
+ list_del(&o->list);
119
+ c->tot_orphans -= 1;
120
+
121
+ if (o->new) {
122
+ list_del(&o->new_list);
123
+ c->new_orphans -= 1;
124
+ }
125
+
126
+ kfree(o);
127
+}
128
+
129
+static void orphan_delete(struct ubifs_info *c, struct ubifs_orphan *orph)
130
+{
131
+ if (orph->del) {
132
+ dbg_gen("deleted twice ino %lu", (unsigned long)orph->inum);
133
+ return;
134
+ }
135
+
136
+ if (orph->cmt) {
137
+ orph->del = 1;
138
+ orph->dnext = c->orph_dnext;
139
+ c->orph_dnext = orph;
140
+ dbg_gen("delete later ino %lu", (unsigned long)orph->inum);
141
+ return;
142
+ }
143
+
144
+ __orphan_drop(c, orph);
145
+}
146
+
147
+/**
148
+ * ubifs_add_orphan - add an orphan.
149
+ * @c: UBIFS file-system description object
150
+ * @inum: orphan inode number
151
+ *
152
+ * Add an orphan. This function is called when an inodes link count drops to
153
+ * zero.
154
+ */
155
+int ubifs_add_orphan(struct ubifs_info *c, ino_t inum)
156
+{
157
+ int err = 0;
158
+ ino_t xattr_inum;
159
+ union ubifs_key key;
160
+ struct ubifs_dent_node *xent, *pxent = NULL;
161
+ struct fscrypt_name nm = {0};
162
+ struct ubifs_orphan *xattr_orphan;
163
+ struct ubifs_orphan *orphan;
164
+
165
+ orphan = orphan_add(c, inum, NULL);
166
+ if (IS_ERR(orphan))
167
+ return PTR_ERR(orphan);
168
+
169
+ lowest_xent_key(c, &key, inum);
170
+ while (1) {
171
+ xent = ubifs_tnc_next_ent(c, &key, &nm);
172
+ if (IS_ERR(xent)) {
173
+ err = PTR_ERR(xent);
174
+ if (err == -ENOENT)
175
+ break;
176
+ kfree(pxent);
177
+ return err;
178
+ }
179
+
180
+ fname_name(&nm) = xent->name;
181
+ fname_len(&nm) = le16_to_cpu(xent->nlen);
182
+ xattr_inum = le64_to_cpu(xent->inum);
183
+
184
+ xattr_orphan = orphan_add(c, xattr_inum, orphan);
185
+ if (IS_ERR(xattr_orphan)) {
186
+ kfree(pxent);
187
+ kfree(xent);
188
+ return PTR_ERR(xattr_orphan);
189
+ }
190
+
191
+ kfree(pxent);
192
+ pxent = xent;
193
+ key_read(c, &xent->key, &key);
194
+ }
195
+ kfree(pxent);
196
+
105197 return 0;
106198 }
107199
....@@ -114,49 +206,27 @@
114206 */
115207 void ubifs_delete_orphan(struct ubifs_info *c, ino_t inum)
116208 {
117
- struct ubifs_orphan *o;
118
- struct rb_node *p;
209
+ struct ubifs_orphan *orph, *child_orph, *tmp_o;
119210
120211 spin_lock(&c->orphan_lock);
121
- p = c->orph_tree.rb_node;
122
- while (p) {
123
- o = rb_entry(p, struct ubifs_orphan, rb);
124
- if (inum < o->inum)
125
- p = p->rb_left;
126
- else if (inum > o->inum)
127
- p = p->rb_right;
128
- else {
129
- if (o->del) {
130
- spin_unlock(&c->orphan_lock);
131
- dbg_gen("deleted twice ino %lu",
132
- (unsigned long)inum);
133
- return;
134
- }
135
- if (o->cmt) {
136
- o->del = 1;
137
- o->dnext = c->orph_dnext;
138
- c->orph_dnext = o;
139
- spin_unlock(&c->orphan_lock);
140
- dbg_gen("delete later ino %lu",
141
- (unsigned long)inum);
142
- return;
143
- }
144
- rb_erase(p, &c->orph_tree);
145
- list_del(&o->list);
146
- c->tot_orphans -= 1;
147
- if (o->new) {
148
- list_del(&o->new_list);
149
- c->new_orphans -= 1;
150
- }
151
- spin_unlock(&c->orphan_lock);
152
- kfree(o);
153
- dbg_gen("inum %lu", (unsigned long)inum);
154
- return;
155
- }
212
+
213
+ orph = lookup_orphan(c, inum);
214
+ if (!orph) {
215
+ spin_unlock(&c->orphan_lock);
216
+ ubifs_err(c, "missing orphan ino %lu", (unsigned long)inum);
217
+ dump_stack();
218
+
219
+ return;
156220 }
221
+
222
+ list_for_each_entry_safe(child_orph, tmp_o, &orph->child_list, child_list) {
223
+ list_del(&child_orph->child_list);
224
+ orphan_delete(c, child_orph);
225
+ }
226
+
227
+ orphan_delete(c, orph);
228
+
157229 spin_unlock(&c->orphan_lock);
158
- ubifs_err(c, "missing orphan ino %lu", (unsigned long)inum);
159
- dump_stack();
160230 }
161231
162232 /**
....@@ -563,16 +633,22 @@
563633 {
564634 struct ubifs_scan_node *snod;
565635 struct ubifs_orph_node *orph;
636
+ struct ubifs_ino_node *ino = NULL;
566637 unsigned long long cmt_no;
567638 ino_t inum;
568639 int i, n, err, first = 1;
640
+
641
+ ino = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
642
+ if (!ino)
643
+ return -ENOMEM;
569644
570645 list_for_each_entry(snod, &sleb->nodes, list) {
571646 if (snod->type != UBIFS_ORPH_NODE) {
572647 ubifs_err(c, "invalid node type %d in orphan area at %d:%d",
573648 snod->type, sleb->lnum, snod->offs);
574649 ubifs_dump_node(c, snod->node);
575
- return -EINVAL;
650
+ err = -EINVAL;
651
+ goto out_free;
576652 }
577653
578654 orph = snod->node;
....@@ -599,11 +675,13 @@
599675 ubifs_err(c, "out of order commit number %llu in orphan node at %d:%d",
600676 cmt_no, sleb->lnum, snod->offs);
601677 ubifs_dump_node(c, snod->node);
602
- return -EINVAL;
678
+ err = -EINVAL;
679
+ goto out_free;
603680 }
604681 dbg_rcvry("out of date LEB %d", sleb->lnum);
605682 *outofdate = 1;
606
- return 0;
683
+ err = 0;
684
+ goto out_free;
607685 }
608686
609687 if (first)
....@@ -611,15 +689,34 @@
611689
612690 n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
613691 for (i = 0; i < n; i++) {
692
+ union ubifs_key key1, key2;
693
+
614694 inum = le64_to_cpu(orph->inos[i]);
615
- dbg_rcvry("deleting orphaned inode %lu",
616
- (unsigned long)inum);
617
- err = ubifs_tnc_remove_ino(c, inum);
618
- if (err)
619
- return err;
695
+
696
+ ino_key_init(c, &key1, inum);
697
+ err = ubifs_tnc_lookup(c, &key1, ino);
698
+ if (err && err != -ENOENT)
699
+ goto out_free;
700
+
701
+ /*
702
+ * Check whether an inode can really get deleted.
703
+ * linkat() with O_TMPFILE allows rebirth of an inode.
704
+ */
705
+ if (err == 0 && ino->nlink == 0) {
706
+ dbg_rcvry("deleting orphaned inode %lu",
707
+ (unsigned long)inum);
708
+
709
+ lowest_ino_key(c, &key1, inum);
710
+ highest_ino_key(c, &key2, inum);
711
+
712
+ err = ubifs_tnc_remove_range(c, &key1, &key2);
713
+ if (err)
714
+ goto out_ro;
715
+ }
716
+
620717 err = insert_dead_orphan(c, inum);
621718 if (err)
622
- return err;
719
+ goto out_free;
623720 }
624721
625722 *last_cmt_no = cmt_no;
....@@ -631,7 +728,15 @@
631728 *last_flagged = 0;
632729 }
633730
634
- return 0;
731
+ err = 0;
732
+out_free:
733
+ kfree(ino);
734
+ return err;
735
+
736
+out_ro:
737
+ ubifs_ro_mode(c, err);
738
+ kfree(ino);
739
+ return err;
635740 }
636741
637742 /**
....@@ -744,26 +849,15 @@
744849 struct rb_root root;
745850 };
746851
747
-static int dbg_find_orphan(struct ubifs_info *c, ino_t inum)
852
+static bool dbg_find_orphan(struct ubifs_info *c, ino_t inum)
748853 {
749
- struct ubifs_orphan *o;
750
- struct rb_node *p;
854
+ bool found = false;
751855
752856 spin_lock(&c->orphan_lock);
753
- p = c->orph_tree.rb_node;
754
- while (p) {
755
- o = rb_entry(p, struct ubifs_orphan, rb);
756
- if (inum < o->inum)
757
- p = p->rb_left;
758
- else if (inum > o->inum)
759
- p = p->rb_right;
760
- else {
761
- spin_unlock(&c->orphan_lock);
762
- return 1;
763
- }
764
- }
857
+ found = !!lookup_orphan(c, inum);
765858 spin_unlock(&c->orphan_lock);
766
- return 0;
859
+
860
+ return found;
767861 }
768862
769863 static int dbg_ins_check_orphan(struct rb_root *root, ino_t inum)
....@@ -885,7 +979,7 @@
885979 if (c->no_orphs)
886980 return 0;
887981
888
- buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
982
+ buf = __vmalloc(c->leb_size, GFP_NOFS);
889983 if (!buf) {
890984 ubifs_err(c, "cannot allocate memory to check orphans");
891985 return 0;