2
* This file is part of UBIFS.
4
* Copyright (C) 2006-2008 Nokia Corporation.
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.
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
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
19
* Author: Adrian Hunter
25
* An orphan is an inode number whose inode node has been committed to the index
26
* with a link count of zero. That happens when an open file is deleted
27
* (unlinked) and then a commit is run. In the normal course of events the inode
28
* would be deleted when the file is closed. However in the case of an unclean
29
* unmount, orphans need to be accounted for. After an unclean unmount, the
30
* orphans' inodes must be deleted which means either scanning the entire index
31
* looking for them, or keeping a list on flash somewhere. This unit implements
32
* the latter approach.
34
* The orphan area is a fixed number of LEBs situated between the LPT area and
35
* the main area. The number of orphan area LEBs is specified when the file
36
* system is created. The minimum number is 1. The size of the orphan area
37
* should be so that it can hold the maximum number of orphans that are expected
38
* to ever exist at one time.
40
* The number of orphans that can fit in a LEB is:
42
* (c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64)
44
* For example: a 15872 byte LEB can fit 1980 orphans so 1 LEB may be enough.
46
* Orphans are accumulated in a rb-tree. When an inode's link count drops to
47
* zero, the inode number is added to the rb-tree. It is removed from the tree
48
* when the inode is deleted. Any new orphans that are in the orphan tree when
49
* the commit is run, are written to the orphan area in 1 or more orphan nodes.
50
* If the orphan area is full, it is consolidated to make space. There is
51
* always enough space because validation prevents the user from creating more
52
* than the maximum number of orphans allowed.
56
* tot_avail_orphs - calculate total space.
57
* @c: UBIFS file-system description object
59
* This function returns the number of orphans that can be written in half
60
* the total space. That leaves half the space for adding new orphans.
62
static int tot_avail_orphs(struct ubifs_info *c)
64
int avail_lebs, avail;
66
avail_lebs = c->orph_lebs;
68
((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
73
* ubifs_clear_orphans - erase all LEBs used for orphans.
74
* @c: UBIFS file-system description object
76
* If recovery is not required, then the orphans from the previous session
77
* are not needed. This function locates the LEBs used to record
78
* orphans, and un-maps them.
80
int ubifs_clear_orphans(struct ubifs_info *c)
84
for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
85
err = ubifs_leb_unmap(c, lnum);
89
c->ohead_lnum = c->orph_first;
95
* insert_dead_orphan - insert an orphan.
96
* @c: UBIFS file-system description object
97
* @inum: orphan inode number
99
* This function is a helper to the 'do_kill_orphans()' function. The orphan
100
* must be kept until the next commit, so it is added to the rb-tree and the
103
static int insert_dead_orphan(struct ubifs_info *c, ino_t inum)
105
struct ubifs_orphan *orphan, *o;
106
struct rb_node **p, *parent = NULL;
108
orphan = kzalloc(sizeof(struct ubifs_orphan), GFP_KERNEL);
113
p = &c->orph_tree.rb_node;
116
o = rb_entry(parent, struct ubifs_orphan, rb);
119
else if (inum > o->inum)
122
/* Already added - no problem */
128
rb_link_node(&orphan->rb, parent, p);
129
rb_insert_color(&orphan->rb, &c->orph_tree);
130
list_add_tail(&orphan->list, &c->orph_list);
131
orphan->dnext = c->orph_dnext;
132
c->orph_dnext = orphan;
133
dbg_mnt("ino %lu, new %d, tot %d", (unsigned long)inum,
134
c->new_orphans, c->tot_orphans);
139
* do_kill_orphans - remove orphan inodes from the index.
140
* @c: UBIFS file-system description object
142
* @last_cmt_no: cmt_no of last orphan node read is passed and returned here
143
* @outofdate: whether the LEB is out of date is returned here
144
* @last_flagged: whether the end orphan node is encountered
146
* This function is a helper to the 'kill_orphans()' function. It goes through
147
* every orphan node in a LEB and for every inode number recorded, removes
148
* all keys for that inode from the TNC.
150
static int do_kill_orphans(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
151
unsigned long long *last_cmt_no, int *outofdate,
154
struct ubifs_scan_node *snod;
155
struct ubifs_orph_node *orph;
156
unsigned long long cmt_no;
158
int i, n, err, first = 1;
160
list_for_each_entry(snod, &sleb->nodes, list) {
161
if (snod->type != UBIFS_ORPH_NODE) {
162
ubifs_err("invalid node type %d in orphan area at "
163
"%d:%d", snod->type, sleb->lnum, snod->offs);
164
dbg_dump_node(c, snod->node);
170
/* Check commit number */
171
cmt_no = le64_to_cpu(orph->cmt_no) & LLONG_MAX;
173
* The commit number on the master node may be less, because
174
* of a failed commit. If there are several failed commits in a
175
* row, the commit number written on orphan nodes will continue
176
* to increase (because the commit number is adjusted here) even
177
* though the commit number on the master node stays the same
178
* because the master node has not been re-written.
180
if (cmt_no > c->cmt_no)
182
if (cmt_no < *last_cmt_no && *last_flagged) {
184
* The last orphan node had a higher commit number and
185
* was flagged as the last written for that commit
186
* number. That makes this orphan node, out of date.
189
ubifs_err("out of order commit number %llu in "
190
"orphan node at %d:%d",
191
cmt_no, sleb->lnum, snod->offs);
192
dbg_dump_node(c, snod->node);
195
dbg_rcvry("out of date LEB %d", sleb->lnum);
203
n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
204
for (i = 0; i < n; i++) {
205
inum = le64_to_cpu(orph->inos[i]);
206
dbg_rcvry("deleting orphaned inode %lu",
207
(unsigned long)inum);
208
err = ubifs_tnc_remove_ino(c, inum);
211
err = insert_dead_orphan(c, inum);
216
*last_cmt_no = cmt_no;
217
if (le64_to_cpu(orph->cmt_no) & (1ULL << 63)) {
218
dbg_rcvry("last orph node for commit %llu at %d:%d",
219
cmt_no, sleb->lnum, snod->offs);
229
* kill_orphans - remove all orphan inodes from the index.
230
* @c: UBIFS file-system description object
232
* If recovery is required, then orphan inodes recorded during the previous
233
* session (which ended with an unclean unmount) must be deleted from the index.
234
* This is done by updating the TNC, but since the index is not updated until
235
* the next commit, the LEBs where the orphan information is recorded are not
236
* erased until the next commit.
238
static int kill_orphans(struct ubifs_info *c)
240
unsigned long long last_cmt_no = 0;
241
int lnum, err = 0, outofdate = 0, last_flagged = 0;
243
c->ohead_lnum = c->orph_first;
245
/* Check no-orphans flag and skip this if no orphans */
247
dbg_rcvry("no orphans");
251
* Orph nodes always start at c->orph_first and are written to each
252
* successive LEB in turn. Generally unused LEBs will have been unmapped
253
* but may contain out of date orphan nodes if the unmap didn't go
254
* through. In addition, the last orphan node written for each commit is
255
* marked (top bit of orph->cmt_no is set to 1). It is possible that
256
* there are orphan nodes from the next commit (i.e. the commit did not
257
* complete successfully). In that case, no orphans will have been lost
258
* due to the way that orphans are written, and any orphans added will
259
* be valid orphans anyway and so can be deleted.
261
for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
262
struct ubifs_scan_leb *sleb;
264
dbg_rcvry("LEB %d", lnum);
265
sleb = ubifs_scan(c, lnum, 0, c->sbuf);
267
sleb = ubifs_recover_leb(c, lnum, 0, c->sbuf, 0);
273
err = do_kill_orphans(c, sleb, &last_cmt_no, &outofdate,
275
if (err || outofdate) {
276
ubifs_scan_destroy(sleb);
280
c->ohead_lnum = lnum;
281
c->ohead_offs = sleb->endpt;
283
ubifs_scan_destroy(sleb);
289
* ubifs_mount_orphans - delete orphan inodes and erase LEBs that recorded them.
290
* @c: UBIFS file-system description object
291
* @unclean: indicates recovery from unclean unmount
292
* @read_only: indicates read only mount
294
* This function is called when mounting to erase orphans from the previous
295
* session. If UBIFS was not unmounted cleanly, then the inodes recorded as
296
* orphans are deleted.
298
int ubifs_mount_orphans(struct ubifs_info *c, int unclean, int read_only)
302
c->max_orphans = tot_avail_orphs(c);
305
c->orph_buf = vmalloc(c->leb_size);
311
err = kill_orphans(c);
313
err = ubifs_clear_orphans(c);