Blame view

fs/ubifs/orphan.c 24.7 KB
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  /*
   * This file is part of UBIFS.
   *
   * Copyright (C) 2006-2008 Nokia Corporation.
   *
   * This program is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 as published by
   * the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful, but WITHOUT
   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   * more details.
   *
   * You should have received a copy of the GNU General Public License along with
   * this program; if not, write to the Free Software Foundation, Inc., 51
   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
   *
   * Author: Adrian Hunter
   */
  
  #include "ubifs.h"
  
  /*
   * An orphan is an inode number whose inode node has been committed to the index
   * with a link count of zero. That happens when an open file is deleted
   * (unlinked) and then a commit is run. In the normal course of events the inode
   * would be deleted when the file is closed. However in the case of an unclean
   * unmount, orphans need to be accounted for. After an unclean unmount, the
   * orphans' inodes must be deleted which means either scanning the entire index
   * looking for them, or keeping a list on flash somewhere. This unit implements
   * the latter approach.
   *
   * The orphan area is a fixed number of LEBs situated between the LPT area and
   * the main area. The number of orphan area LEBs is specified when the file
   * system is created. The minimum number is 1. The size of the orphan area
   * should be so that it can hold the maximum number of orphans that are expected
   * to ever exist at one time.
   *
   * The number of orphans that can fit in a LEB is:
   *
   *         (c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64)
   *
   * For example: a 15872 byte LEB can fit 1980 orphans so 1 LEB may be enough.
   *
   * Orphans are accumulated in a rb-tree. When an inode's link count drops to
   * zero, the inode number is added to the rb-tree. It is removed from the tree
   * when the inode is deleted.  Any new orphans that are in the orphan tree when
49d128aa6   Adrian Hunter   UBIFS: ensure orp...
49
   * the commit is run, are written to the orphan area in 1 or more orphan nodes.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
50
51
52
53
   * If the orphan area is full, it is consolidated to make space.  There is
   * always enough space because validation prevents the user from creating more
   * than the maximum number of orphans allowed.
   */
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
54
  static int dbg_check_orphans(struct ubifs_info *c);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  
  /**
   * ubifs_add_orphan - add an orphan.
   * @c: UBIFS file-system description object
   * @inum: orphan inode number
   *
   * Add an orphan. This function is called when an inodes link count drops to
   * zero.
   */
  int ubifs_add_orphan(struct ubifs_info *c, ino_t inum)
  {
  	struct ubifs_orphan *orphan, *o;
  	struct rb_node **p, *parent = NULL;
  
  	orphan = kzalloc(sizeof(struct ubifs_orphan), GFP_NOFS);
  	if (!orphan)
  		return -ENOMEM;
  	orphan->inum = inum;
  	orphan->new = 1;
  
  	spin_lock(&c->orphan_lock);
  	if (c->tot_orphans >= c->max_orphans) {
  		spin_unlock(&c->orphan_lock);
  		kfree(orphan);
  		return -ENFILE;
  	}
  	p = &c->orph_tree.rb_node;
  	while (*p) {
  		parent = *p;
  		o = rb_entry(parent, struct ubifs_orphan, rb);
  		if (inum < o->inum)
  			p = &(*p)->rb_left;
  		else if (inum > o->inum)
  			p = &(*p)->rb_right;
  		else {
235c362bd   Sheng Yong   UBIFS: extend deb...
90
  			ubifs_err(c, "orphaned twice");
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
91
92
93
94
95
96
97
98
99
100
101
102
  			spin_unlock(&c->orphan_lock);
  			kfree(orphan);
  			return 0;
  		}
  	}
  	c->tot_orphans += 1;
  	c->new_orphans += 1;
  	rb_link_node(&orphan->rb, parent, p);
  	rb_insert_color(&orphan->rb, &c->orph_tree);
  	list_add_tail(&orphan->list, &c->orph_list);
  	list_add_tail(&orphan->new_list, &c->orph_new);
  	spin_unlock(&c->orphan_lock);
e84461ad9   Artem Bityutskiy   UBIFS: fix compil...
103
  	dbg_gen("ino %lu", (unsigned long)inum);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  	return 0;
  }
  
  /**
   * ubifs_delete_orphan - delete an orphan.
   * @c: UBIFS file-system description object
   * @inum: orphan inode number
   *
   * Delete an orphan. This function is called when an inode is deleted.
   */
  void ubifs_delete_orphan(struct ubifs_info *c, ino_t inum)
  {
  	struct ubifs_orphan *o;
  	struct rb_node *p;
  
  	spin_lock(&c->orphan_lock);
  	p = c->orph_tree.rb_node;
  	while (p) {
  		o = rb_entry(p, struct ubifs_orphan, rb);
  		if (inum < o->inum)
  			p = p->rb_left;
  		else if (inum > o->inum)
  			p = p->rb_right;
  		else {
8afd500cb   Adam Thomas   UBIFS: fix double...
128
  			if (o->del) {
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
129
  				spin_unlock(&c->orphan_lock);
e84461ad9   Artem Bityutskiy   UBIFS: fix compil...
130
131
  				dbg_gen("deleted twice ino %lu",
  					(unsigned long)inum);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
132
133
  				return;
  			}
2928f0d0c   Adam Thomas   UBIFS: fix use of...
134
  			if (o->cmt) {
8afd500cb   Adam Thomas   UBIFS: fix double...
135
  				o->del = 1;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
136
137
138
  				o->dnext = c->orph_dnext;
  				c->orph_dnext = o;
  				spin_unlock(&c->orphan_lock);
e84461ad9   Artem Bityutskiy   UBIFS: fix compil...
139
140
  				dbg_gen("delete later ino %lu",
  					(unsigned long)inum);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
141
142
143
144
145
146
147
148
149
150
151
  				return;
  			}
  			rb_erase(p, &c->orph_tree);
  			list_del(&o->list);
  			c->tot_orphans -= 1;
  			if (o->new) {
  				list_del(&o->new_list);
  				c->new_orphans -= 1;
  			}
  			spin_unlock(&c->orphan_lock);
  			kfree(o);
e84461ad9   Artem Bityutskiy   UBIFS: fix compil...
152
  			dbg_gen("inum %lu", (unsigned long)inum);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
153
154
155
156
  			return;
  		}
  	}
  	spin_unlock(&c->orphan_lock);
235c362bd   Sheng Yong   UBIFS: extend deb...
157
  	ubifs_err(c, "missing orphan ino %lu", (unsigned long)inum);
7c46d0ae2   Artem Bityutskiy   UBIFS: get rid of...
158
  	dump_stack();
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  }
  
  /**
   * ubifs_orphan_start_commit - start commit of orphans.
   * @c: UBIFS file-system description object
   *
   * Start commit of orphans.
   */
  int ubifs_orphan_start_commit(struct ubifs_info *c)
  {
  	struct ubifs_orphan *orphan, **last;
  
  	spin_lock(&c->orphan_lock);
  	last = &c->orph_cnext;
  	list_for_each_entry(orphan, &c->orph_new, new_list) {
  		ubifs_assert(orphan->new);
2928f0d0c   Adam Thomas   UBIFS: fix use of...
175
  		ubifs_assert(!orphan->cmt);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
176
  		orphan->new = 0;
2928f0d0c   Adam Thomas   UBIFS: fix use of...
177
  		orphan->cmt = 1;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
178
179
180
  		*last = orphan;
  		last = &orphan->cnext;
  	}
7074e5eb2   Julia Lawall   UBIFS: remove inv...
181
  	*last = NULL;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
  	c->cmt_orphans = c->new_orphans;
  	c->new_orphans = 0;
  	dbg_cmt("%d orphans to commit", c->cmt_orphans);
  	INIT_LIST_HEAD(&c->orph_new);
  	if (c->tot_orphans == 0)
  		c->no_orphs = 1;
  	else
  		c->no_orphs = 0;
  	spin_unlock(&c->orphan_lock);
  	return 0;
  }
  
  /**
   * avail_orphs - calculate available space.
   * @c: UBIFS file-system description object
   *
   * This function returns the number of orphans that can be written in the
   * available space.
   */
  static int avail_orphs(struct ubifs_info *c)
  {
  	int avail_lebs, avail, gap;
  
  	avail_lebs = c->orph_lebs - (c->ohead_lnum - c->orph_first) - 1;
  	avail = avail_lebs *
  	       ((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
  	gap = c->leb_size - c->ohead_offs;
  	if (gap >= UBIFS_ORPH_NODE_SZ + sizeof(__le64))
  		avail += (gap - UBIFS_ORPH_NODE_SZ) / sizeof(__le64);
  	return avail;
  }
  
  /**
   * tot_avail_orphs - calculate total space.
   * @c: UBIFS file-system description object
   *
   * This function returns the number of orphans that can be written in half
   * the total space. That leaves half the space for adding new orphans.
   */
  static int tot_avail_orphs(struct ubifs_info *c)
  {
  	int avail_lebs, avail;
  
  	avail_lebs = c->orph_lebs;
  	avail = avail_lebs *
  	       ((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
  	return avail / 2;
  }
  
  /**
49d128aa6   Adrian Hunter   UBIFS: ensure orp...
232
   * do_write_orph_node - write a node to the orphan head.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
   * @c: UBIFS file-system description object
   * @len: length of node
   * @atomic: write atomically
   *
   * This function writes a node to the orphan head from the orphan buffer. If
   * %atomic is not zero, then the write is done atomically. On success, %0 is
   * returned, otherwise a negative error code is returned.
   */
  static int do_write_orph_node(struct ubifs_info *c, int len, int atomic)
  {
  	int err = 0;
  
  	if (atomic) {
  		ubifs_assert(c->ohead_offs == 0);
  		ubifs_prepare_node(c, c->orph_buf, len, 1);
  		len = ALIGN(len, c->min_io_size);
b36a261e8   Richard Weinberger   UBI: Kill data ty...
249
  		err = ubifs_leb_change(c, c->ohead_lnum, c->orph_buf, len);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
250
251
252
253
254
255
256
257
  	} else {
  		if (c->ohead_offs == 0) {
  			/* Ensure LEB has been unmapped */
  			err = ubifs_leb_unmap(c, c->ohead_lnum);
  			if (err)
  				return err;
  		}
  		err = ubifs_write_node(c, c->orph_buf, len, c->ohead_lnum,
b36a261e8   Richard Weinberger   UBI: Kill data ty...
258
  				       c->ohead_offs);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
259
260
261
262
263
  	}
  	return err;
  }
  
  /**
49d128aa6   Adrian Hunter   UBIFS: ensure orp...
264
   * write_orph_node - write an orphan node.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
265
266
267
   * @c: UBIFS file-system description object
   * @atomic: write atomically
   *
49d128aa6   Adrian Hunter   UBIFS: ensure orp...
268
   * This function builds an orphan node from the cnext list and writes it to the
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
   * orphan head. On success, %0 is returned, otherwise a negative error code
   * is returned.
   */
  static int write_orph_node(struct ubifs_info *c, int atomic)
  {
  	struct ubifs_orphan *orphan, *cnext;
  	struct ubifs_orph_node *orph;
  	int gap, err, len, cnt, i;
  
  	ubifs_assert(c->cmt_orphans > 0);
  	gap = c->leb_size - c->ohead_offs;
  	if (gap < UBIFS_ORPH_NODE_SZ + sizeof(__le64)) {
  		c->ohead_lnum += 1;
  		c->ohead_offs = 0;
  		gap = c->leb_size;
  		if (c->ohead_lnum > c->orph_last) {
  			/*
  			 * We limit the number of orphans so that this should
  			 * never happen.
  			 */
235c362bd   Sheng Yong   UBIFS: extend deb...
289
  			ubifs_err(c, "out of space in orphan area");
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
290
291
292
293
294
295
296
297
298
299
300
301
302
303
  			return -EINVAL;
  		}
  	}
  	cnt = (gap - UBIFS_ORPH_NODE_SZ) / sizeof(__le64);
  	if (cnt > c->cmt_orphans)
  		cnt = c->cmt_orphans;
  	len = UBIFS_ORPH_NODE_SZ + cnt * sizeof(__le64);
  	ubifs_assert(c->orph_buf);
  	orph = c->orph_buf;
  	orph->ch.node_type = UBIFS_ORPH_NODE;
  	spin_lock(&c->orphan_lock);
  	cnext = c->orph_cnext;
  	for (i = 0; i < cnt; i++) {
  		orphan = cnext;
2928f0d0c   Adam Thomas   UBIFS: fix use of...
304
  		ubifs_assert(orphan->cmt);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
305
  		orph->inos[i] = cpu_to_le64(orphan->inum);
2928f0d0c   Adam Thomas   UBIFS: fix use of...
306
  		orphan->cmt = 0;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
307
308
309
310
311
312
313
  		cnext = orphan->cnext;
  		orphan->cnext = NULL;
  	}
  	c->orph_cnext = cnext;
  	c->cmt_orphans -= cnt;
  	spin_unlock(&c->orphan_lock);
  	if (c->cmt_orphans)
014eb04b0   Artem Bityutskiy   UBIFS: increment ...
314
  		orph->cmt_no = cpu_to_le64(c->cmt_no);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
315
316
  	else
  		/* Mark the last node of the commit */
014eb04b0   Artem Bityutskiy   UBIFS: increment ...
317
  		orph->cmt_no = cpu_to_le64((c->cmt_no) | (1ULL << 63));
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
318
319
320
321
322
323
324
325
326
327
  	ubifs_assert(c->ohead_offs + len <= c->leb_size);
  	ubifs_assert(c->ohead_lnum >= c->orph_first);
  	ubifs_assert(c->ohead_lnum <= c->orph_last);
  	err = do_write_orph_node(c, len, atomic);
  	c->ohead_offs += ALIGN(len, c->min_io_size);
  	c->ohead_offs = ALIGN(c->ohead_offs, 8);
  	return err;
  }
  
  /**
49d128aa6   Adrian Hunter   UBIFS: ensure orp...
328
   * write_orph_nodes - write orphan nodes until there are no more to commit.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
329
330
331
   * @c: UBIFS file-system description object
   * @atomic: write atomically
   *
49d128aa6   Adrian Hunter   UBIFS: ensure orp...
332
   * This function writes orphan nodes for all the orphans to commit. On success,
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
   * %0 is returned, otherwise a negative error code is returned.
   */
  static int write_orph_nodes(struct ubifs_info *c, int atomic)
  {
  	int err;
  
  	while (c->cmt_orphans > 0) {
  		err = write_orph_node(c, atomic);
  		if (err)
  			return err;
  	}
  	if (atomic) {
  		int lnum;
  
  		/* Unmap any unused LEBs after consolidation */
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
  		for (lnum = c->ohead_lnum + 1; lnum <= c->orph_last; lnum++) {
  			err = ubifs_leb_unmap(c, lnum);
  			if (err)
  				return err;
  		}
  	}
  	return 0;
  }
  
  /**
   * consolidate - consolidate the orphan area.
   * @c: UBIFS file-system description object
   *
   * This function enables consolidation by putting all the orphans into the list
   * to commit. The list is in the order that the orphans were added, and the
   * LEBs are written atomically in order, so at no time can orphans be lost by
   * an unclean unmount.
   *
   * This function returns %0 on success and a negative error code on failure.
   */
  static int consolidate(struct ubifs_info *c)
  {
  	int tot_avail = tot_avail_orphs(c), err = 0;
  
  	spin_lock(&c->orphan_lock);
  	dbg_cmt("there is space for %d orphans and there are %d",
  		tot_avail, c->tot_orphans);
  	if (c->tot_orphans - c->new_orphans <= tot_avail) {
  		struct ubifs_orphan *orphan, **last;
  		int cnt = 0;
  
  		/* Change the cnext list to include all non-new orphans */
  		last = &c->orph_cnext;
  		list_for_each_entry(orphan, &c->orph_list, list) {
  			if (orphan->new)
  				continue;
2928f0d0c   Adam Thomas   UBIFS: fix use of...
384
  			orphan->cmt = 1;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
385
386
387
388
  			*last = orphan;
  			last = &orphan->cnext;
  			cnt += 1;
  		}
7074e5eb2   Julia Lawall   UBIFS: remove inv...
389
  		*last = NULL;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
390
391
392
393
394
395
396
397
398
  		ubifs_assert(cnt == c->tot_orphans - c->new_orphans);
  		c->cmt_orphans = cnt;
  		c->ohead_lnum = c->orph_first;
  		c->ohead_offs = 0;
  	} else {
  		/*
  		 * We limit the number of orphans so that this should
  		 * never happen.
  		 */
235c362bd   Sheng Yong   UBIFS: extend deb...
399
  		ubifs_err(c, "out of space in orphan area");
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
  		err = -EINVAL;
  	}
  	spin_unlock(&c->orphan_lock);
  	return err;
  }
  
  /**
   * commit_orphans - commit orphans.
   * @c: UBIFS file-system description object
   *
   * This function commits orphans to flash. On success, %0 is returned,
   * otherwise a negative error code is returned.
   */
  static int commit_orphans(struct ubifs_info *c)
  {
  	int avail, atomic = 0, err;
  
  	ubifs_assert(c->cmt_orphans > 0);
  	avail = avail_orphs(c);
  	if (avail < c->cmt_orphans) {
  		/* Not enough space to write new orphans, so consolidate */
  		err = consolidate(c);
  		if (err)
  			return err;
  		atomic = 1;
  	}
  	err = write_orph_nodes(c, atomic);
  	return err;
  }
  
  /**
   * erase_deleted - erase the orphans marked for deletion.
   * @c: UBIFS file-system description object
   *
   * During commit, the orphans being committed cannot be deleted, so they are
   * marked for deletion and deleted by this function. Also, the recovery
   * adds killed orphans to the deletion list, and therefore they are deleted
   * here too.
   */
  static void erase_deleted(struct ubifs_info *c)
  {
  	struct ubifs_orphan *orphan, *dnext;
  
  	spin_lock(&c->orphan_lock);
  	dnext = c->orph_dnext;
  	while (dnext) {
  		orphan = dnext;
  		dnext = orphan->dnext;
  		ubifs_assert(!orphan->new);
8afd500cb   Adam Thomas   UBIFS: fix double...
449
  		ubifs_assert(orphan->del);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
450
451
452
  		rb_erase(&orphan->rb, &c->orph_tree);
  		list_del(&orphan->list);
  		c->tot_orphans -= 1;
e84461ad9   Artem Bityutskiy   UBIFS: fix compil...
453
  		dbg_gen("deleting orphan ino %lu", (unsigned long)orphan->inum);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
  		kfree(orphan);
  	}
  	c->orph_dnext = NULL;
  	spin_unlock(&c->orphan_lock);
  }
  
  /**
   * ubifs_orphan_end_commit - end commit of orphans.
   * @c: UBIFS file-system description object
   *
   * End commit of orphans.
   */
  int ubifs_orphan_end_commit(struct ubifs_info *c)
  {
  	int err;
  
  	if (c->cmt_orphans != 0) {
  		err = commit_orphans(c);
  		if (err)
  			return err;
  	}
  	erase_deleted(c);
  	err = dbg_check_orphans(c);
  	return err;
  }
  
  /**
49d128aa6   Adrian Hunter   UBIFS: ensure orp...
481
   * ubifs_clear_orphans - erase all LEBs used for orphans.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
482
483
484
485
486
487
   * @c: UBIFS file-system description object
   *
   * If recovery is not required, then the orphans from the previous session
   * are not needed. This function locates the LEBs used to record
   * orphans, and un-maps them.
   */
49d128aa6   Adrian Hunter   UBIFS: ensure orp...
488
  int ubifs_clear_orphans(struct ubifs_info *c)
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
  {
  	int lnum, err;
  
  	for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
  		err = ubifs_leb_unmap(c, lnum);
  		if (err)
  			return err;
  	}
  	c->ohead_lnum = c->orph_first;
  	c->ohead_offs = 0;
  	return 0;
  }
  
  /**
   * insert_dead_orphan - insert an orphan.
   * @c: UBIFS file-system description object
   * @inum: orphan inode number
   *
   * This function is a helper to the 'do_kill_orphans()' function. The orphan
   * must be kept until the next commit, so it is added to the rb-tree and the
   * deletion list.
   */
  static int insert_dead_orphan(struct ubifs_info *c, ino_t inum)
  {
  	struct ubifs_orphan *orphan, *o;
  	struct rb_node **p, *parent = NULL;
  
  	orphan = kzalloc(sizeof(struct ubifs_orphan), GFP_KERNEL);
  	if (!orphan)
  		return -ENOMEM;
  	orphan->inum = inum;
  
  	p = &c->orph_tree.rb_node;
  	while (*p) {
  		parent = *p;
  		o = rb_entry(parent, struct ubifs_orphan, rb);
  		if (inum < o->inum)
  			p = &(*p)->rb_left;
  		else if (inum > o->inum)
  			p = &(*p)->rb_right;
  		else {
  			/* Already added - no problem */
  			kfree(orphan);
  			return 0;
  		}
  	}
  	c->tot_orphans += 1;
  	rb_link_node(&orphan->rb, parent, p);
  	rb_insert_color(&orphan->rb, &c->orph_tree);
  	list_add_tail(&orphan->list, &c->orph_list);
8afd500cb   Adam Thomas   UBIFS: fix double...
539
  	orphan->del = 1;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
540
541
  	orphan->dnext = c->orph_dnext;
  	c->orph_dnext = orphan;
e84461ad9   Artem Bityutskiy   UBIFS: fix compil...
542
543
  	dbg_mnt("ino %lu, new %d, tot %d", (unsigned long)inum,
  		c->new_orphans, c->tot_orphans);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
544
545
546
547
548
549
550
  	return 0;
  }
  
  /**
   * do_kill_orphans - remove orphan inodes from the index.
   * @c: UBIFS file-system description object
   * @sleb: scanned LEB
49d128aa6   Adrian Hunter   UBIFS: ensure orp...
551
   * @last_cmt_no: cmt_no of last orphan node read is passed and returned here
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
552
   * @outofdate: whether the LEB is out of date is returned here
49d128aa6   Adrian Hunter   UBIFS: ensure orp...
553
   * @last_flagged: whether the end orphan node is encountered
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
   *
   * This function is a helper to the 'kill_orphans()' function. It goes through
   * every orphan node in a LEB and for every inode number recorded, removes
   * all keys for that inode from the TNC.
   */
  static int do_kill_orphans(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
  			   unsigned long long *last_cmt_no, int *outofdate,
  			   int *last_flagged)
  {
  	struct ubifs_scan_node *snod;
  	struct ubifs_orph_node *orph;
  	unsigned long long cmt_no;
  	ino_t inum;
  	int i, n, err, first = 1;
  
  	list_for_each_entry(snod, &sleb->nodes, list) {
  		if (snod->type != UBIFS_ORPH_NODE) {
235c362bd   Sheng Yong   UBIFS: extend deb...
571
  			ubifs_err(c, "invalid node type %d in orphan area at %d:%d",
79fda5179   Artem Bityutskiy   UBIFS: comply wit...
572
  				  snod->type, sleb->lnum, snod->offs);
edf6be245   Artem Bityutskiy   UBIFS: rename dum...
573
  			ubifs_dump_node(c, snod->node);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
574
575
576
577
578
579
580
581
582
583
  			return -EINVAL;
  		}
  
  		orph = snod->node;
  
  		/* Check commit number */
  		cmt_no = le64_to_cpu(orph->cmt_no) & LLONG_MAX;
  		/*
  		 * The commit number on the master node may be less, because
  		 * of a failed commit. If there are several failed commits in a
49d128aa6   Adrian Hunter   UBIFS: ensure orp...
584
585
  		 * row, the commit number written on orphan nodes will continue
  		 * to increase (because the commit number is adjusted here) even
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
586
587
588
589
590
591
592
  		 * though the commit number on the master node stays the same
  		 * because the master node has not been re-written.
  		 */
  		if (cmt_no > c->cmt_no)
  			c->cmt_no = cmt_no;
  		if (cmt_no < *last_cmt_no && *last_flagged) {
  			/*
49d128aa6   Adrian Hunter   UBIFS: ensure orp...
593
594
595
  			 * The last orphan node had a higher commit number and
  			 * was flagged as the last written for that commit
  			 * number. That makes this orphan node, out of date.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
596
597
  			 */
  			if (!first) {
235c362bd   Sheng Yong   UBIFS: extend deb...
598
  				ubifs_err(c, "out of order commit number %llu in orphan node at %d:%d",
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
599
  					  cmt_no, sleb->lnum, snod->offs);
edf6be245   Artem Bityutskiy   UBIFS: rename dum...
600
  				ubifs_dump_node(c, snod->node);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
601
602
603
604
605
606
607
608
609
610
611
612
613
  				return -EINVAL;
  			}
  			dbg_rcvry("out of date LEB %d", sleb->lnum);
  			*outofdate = 1;
  			return 0;
  		}
  
  		if (first)
  			first = 0;
  
  		n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
  		for (i = 0; i < n; i++) {
  			inum = le64_to_cpu(orph->inos[i]);
e84461ad9   Artem Bityutskiy   UBIFS: fix compil...
614
615
  			dbg_rcvry("deleting orphaned inode %lu",
  				  (unsigned long)inum);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
  			err = ubifs_tnc_remove_ino(c, inum);
  			if (err)
  				return err;
  			err = insert_dead_orphan(c, inum);
  			if (err)
  				return err;
  		}
  
  		*last_cmt_no = cmt_no;
  		if (le64_to_cpu(orph->cmt_no) & (1ULL << 63)) {
  			dbg_rcvry("last orph node for commit %llu at %d:%d",
  				  cmt_no, sleb->lnum, snod->offs);
  			*last_flagged = 1;
  		} else
  			*last_flagged = 0;
  	}
  
  	return 0;
  }
  
  /**
   * kill_orphans - remove all orphan inodes from the index.
   * @c: UBIFS file-system description object
   *
   * If recovery is required, then orphan inodes recorded during the previous
   * session (which ended with an unclean unmount) must be deleted from the index.
   * This is done by updating the TNC, but since the index is not updated until
   * the next commit, the LEBs where the orphan information is recorded are not
   * erased until the next commit.
   */
  static int kill_orphans(struct ubifs_info *c)
  {
  	unsigned long long last_cmt_no = 0;
  	int lnum, err = 0, outofdate = 0, last_flagged = 0;
  
  	c->ohead_lnum = c->orph_first;
  	c->ohead_offs = 0;
  	/* Check no-orphans flag and skip this if no orphans */
  	if (c->no_orphs) {
  		dbg_rcvry("no orphans");
  		return 0;
  	}
  	/*
  	 * Orph nodes always start at c->orph_first and are written to each
  	 * successive LEB in turn. Generally unused LEBs will have been unmapped
49d128aa6   Adrian Hunter   UBIFS: ensure orp...
661
662
  	 * but may contain out of date orphan nodes if the unmap didn't go
  	 * through. In addition, the last orphan node written for each commit is
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
663
  	 * marked (top bit of orph->cmt_no is set to 1). It is possible that
49d128aa6   Adrian Hunter   UBIFS: ensure orp...
664
  	 * there are orphan nodes from the next commit (i.e. the commit did not
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
665
666
667
668
669
670
671
672
  	 * complete successfully). In that case, no orphans will have been lost
  	 * due to the way that orphans are written, and any orphans added will
  	 * be valid orphans anyway and so can be deleted.
  	 */
  	for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
  		struct ubifs_scan_leb *sleb;
  
  		dbg_rcvry("LEB %d", lnum);
348709bad   Artem Bityutskiy   UBIFS: do not pri...
673
  		sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
674
  		if (IS_ERR(sleb)) {
0dcd18e40   Artem Bityutskiy   UBIFS: check ubif...
675
  			if (PTR_ERR(sleb) == -EUCLEAN)
c43615702   Artem Bityutskiy   UBIFS: fix minor ...
676
  				sleb = ubifs_recover_leb(c, lnum, 0,
efcfde54c   Artem Bityutskiy   UBIFS: amend ubif...
677
  							 c->sbuf, -1);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
  			if (IS_ERR(sleb)) {
  				err = PTR_ERR(sleb);
  				break;
  			}
  		}
  		err = do_kill_orphans(c, sleb, &last_cmt_no, &outofdate,
  				      &last_flagged);
  		if (err || outofdate) {
  			ubifs_scan_destroy(sleb);
  			break;
  		}
  		if (sleb->endpt) {
  			c->ohead_lnum = lnum;
  			c->ohead_offs = sleb->endpt;
  		}
  		ubifs_scan_destroy(sleb);
  	}
  	return err;
  }
  
  /**
   * ubifs_mount_orphans - delete orphan inodes and erase LEBs that recorded them.
   * @c: UBIFS file-system description object
   * @unclean: indicates recovery from unclean unmount
   * @read_only: indicates read only mount
   *
   * This function is called when mounting to erase orphans from the previous
   * session. If UBIFS was not unmounted cleanly, then the inodes recorded as
   * orphans are deleted.
   */
  int ubifs_mount_orphans(struct ubifs_info *c, int unclean, int read_only)
  {
  	int err = 0;
  
  	c->max_orphans = tot_avail_orphs(c);
  
  	if (!read_only) {
  		c->orph_buf = vmalloc(c->leb_size);
  		if (!c->orph_buf)
  			return -ENOMEM;
  	}
  
  	if (unclean)
  		err = kill_orphans(c);
  	else if (!read_only)
49d128aa6   Adrian Hunter   UBIFS: ensure orp...
723
  		err = ubifs_clear_orphans(c);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
724
725
726
  
  	return err;
  }
f70b7e52a   Artem Bityutskiy   UBIFS: remove Kco...
727
728
729
  /*
   * Everything below is related to debugging.
   */
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
  
  struct check_orphan {
  	struct rb_node rb;
  	ino_t inum;
  };
  
  struct check_info {
  	unsigned long last_ino;
  	unsigned long tot_inos;
  	unsigned long missing;
  	unsigned long long leaf_cnt;
  	struct ubifs_ino_node *node;
  	struct rb_root root;
  };
  
  static int dbg_find_orphan(struct ubifs_info *c, ino_t inum)
  {
  	struct ubifs_orphan *o;
  	struct rb_node *p;
  
  	spin_lock(&c->orphan_lock);
  	p = c->orph_tree.rb_node;
  	while (p) {
  		o = rb_entry(p, struct ubifs_orphan, rb);
  		if (inum < o->inum)
  			p = p->rb_left;
  		else if (inum > o->inum)
  			p = p->rb_right;
  		else {
  			spin_unlock(&c->orphan_lock);
  			return 1;
  		}
  	}
  	spin_unlock(&c->orphan_lock);
  	return 0;
  }
  
  static int dbg_ins_check_orphan(struct rb_root *root, ino_t inum)
  {
  	struct check_orphan *orphan, *o;
  	struct rb_node **p, *parent = NULL;
  
  	orphan = kzalloc(sizeof(struct check_orphan), GFP_NOFS);
  	if (!orphan)
  		return -ENOMEM;
  	orphan->inum = inum;
  
  	p = &root->rb_node;
  	while (*p) {
  		parent = *p;
  		o = rb_entry(parent, struct check_orphan, rb);
  		if (inum < o->inum)
  			p = &(*p)->rb_left;
  		else if (inum > o->inum)
  			p = &(*p)->rb_right;
  		else {
  			kfree(orphan);
  			return 0;
  		}
  	}
  	rb_link_node(&orphan->rb, parent, p);
  	rb_insert_color(&orphan->rb, root);
  	return 0;
  }
  
  static int dbg_find_check_orphan(struct rb_root *root, ino_t inum)
  {
  	struct check_orphan *o;
  	struct rb_node *p;
  
  	p = root->rb_node;
  	while (p) {
  		o = rb_entry(p, struct check_orphan, rb);
  		if (inum < o->inum)
  			p = p->rb_left;
  		else if (inum > o->inum)
  			p = p->rb_right;
  		else
  			return 1;
  	}
  	return 0;
  }
  
  static void dbg_free_check_tree(struct rb_root *root)
  {
bb25e49ff   Cody P Schafer   fs/ubifs: use rbt...
815
  	struct check_orphan *o, *n;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
816

bb25e49ff   Cody P Schafer   fs/ubifs: use rbt...
817
  	rbtree_postorder_for_each_entry_safe(o, n, root, rb)
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
818
  		kfree(o);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
819
820
821
822
823
824
825
826
827
828
829
830
831
  }
  
  static int dbg_orphan_check(struct ubifs_info *c, struct ubifs_zbranch *zbr,
  			    void *priv)
  {
  	struct check_info *ci = priv;
  	ino_t inum;
  	int err;
  
  	inum = key_inum(c, &zbr->key);
  	if (inum != ci->last_ino) {
  		/* Lowest node type is the inode node, so it comes first */
  		if (key_type(c, &zbr->key) != UBIFS_INO_KEY)
235c362bd   Sheng Yong   UBIFS: extend deb...
832
  			ubifs_err(c, "found orphan node ino %lu, type %d",
e84461ad9   Artem Bityutskiy   UBIFS: fix compil...
833
  				  (unsigned long)inum, key_type(c, &zbr->key));
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
834
835
836
837
  		ci->last_ino = inum;
  		ci->tot_inos += 1;
  		err = ubifs_tnc_read_node(c, zbr, ci->node);
  		if (err) {
235c362bd   Sheng Yong   UBIFS: extend deb...
838
  			ubifs_err(c, "node read failed, error %d", err);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
839
840
841
842
843
844
  			return err;
  		}
  		if (ci->node->nlink == 0)
  			/* Must be recorded as an orphan */
  			if (!dbg_find_check_orphan(&ci->root, inum) &&
  			    !dbg_find_orphan(c, inum)) {
235c362bd   Sheng Yong   UBIFS: extend deb...
845
  				ubifs_err(c, "missing orphan, ino %lu",
e84461ad9   Artem Bityutskiy   UBIFS: fix compil...
846
  					  (unsigned long)inum);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
  				ci->missing += 1;
  			}
  	}
  	ci->leaf_cnt += 1;
  	return 0;
  }
  
  static int dbg_read_orphans(struct check_info *ci, struct ubifs_scan_leb *sleb)
  {
  	struct ubifs_scan_node *snod;
  	struct ubifs_orph_node *orph;
  	ino_t inum;
  	int i, n, err;
  
  	list_for_each_entry(snod, &sleb->nodes, list) {
  		cond_resched();
  		if (snod->type != UBIFS_ORPH_NODE)
  			continue;
  		orph = snod->node;
  		n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
  		for (i = 0; i < n; i++) {
  			inum = le64_to_cpu(orph->inos[i]);
  			err = dbg_ins_check_orphan(&ci->root, inum);
  			if (err)
  				return err;
  		}
  	}
  	return 0;
  }
  
  static int dbg_scan_orphans(struct ubifs_info *c, struct check_info *ci)
  {
  	int lnum, err = 0;
f5cf319cf   Artem Bityutskiy   UBIFS: allocate o...
880
  	void *buf;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
881
882
883
884
  
  	/* Check no-orphans flag and skip this if no orphans */
  	if (c->no_orphs)
  		return 0;
fc5e58c0c   Artem Bityutskiy   UBIFS: use GFP_NO...
885
  	buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
f5cf319cf   Artem Bityutskiy   UBIFS: allocate o...
886
  	if (!buf) {
235c362bd   Sheng Yong   UBIFS: extend deb...
887
  		ubifs_err(c, "cannot allocate memory to check orphans");
f5cf319cf   Artem Bityutskiy   UBIFS: allocate o...
888
889
  		return 0;
  	}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
890
891
  	for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
  		struct ubifs_scan_leb *sleb;
f5cf319cf   Artem Bityutskiy   UBIFS: allocate o...
892
  		sleb = ubifs_scan(c, lnum, 0, buf, 0);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
893
894
895
896
897
898
899
900
901
902
  		if (IS_ERR(sleb)) {
  			err = PTR_ERR(sleb);
  			break;
  		}
  
  		err = dbg_read_orphans(ci, sleb);
  		ubifs_scan_destroy(sleb);
  		if (err)
  			break;
  	}
f5cf319cf   Artem Bityutskiy   UBIFS: allocate o...
903
  	vfree(buf);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
904
905
906
907
908
909
910
  	return err;
  }
  
  static int dbg_check_orphans(struct ubifs_info *c)
  {
  	struct check_info ci;
  	int err;
2b1844a8c   Artem Bityutskiy   UBIFS: introduce ...
911
  	if (!dbg_is_chk_orph(c))
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
912
913
914
915
916
917
918
919
920
  		return 0;
  
  	ci.last_ino = 0;
  	ci.tot_inos = 0;
  	ci.missing  = 0;
  	ci.leaf_cnt = 0;
  	ci.root = RB_ROOT;
  	ci.node = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
  	if (!ci.node) {
235c362bd   Sheng Yong   UBIFS: extend deb...
921
  		ubifs_err(c, "out of memory");
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
922
923
924
925
926
927
928
929
930
  		return -ENOMEM;
  	}
  
  	err = dbg_scan_orphans(c, &ci);
  	if (err)
  		goto out;
  
  	err = dbg_walk_index(c, &dbg_orphan_check, NULL, &ci);
  	if (err) {
235c362bd   Sheng Yong   UBIFS: extend deb...
931
  		ubifs_err(c, "cannot scan TNC, error %d", err);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
932
933
934
935
  		goto out;
  	}
  
  	if (ci.missing) {
235c362bd   Sheng Yong   UBIFS: extend deb...
936
  		ubifs_err(c, "%lu missing orphan(s)", ci.missing);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
937
938
939
940
941
942
943
944
945
946
947
948
949
  		err = -EINVAL;
  		goto out;
  	}
  
  	dbg_cmt("last inode number is %lu", ci.last_ino);
  	dbg_cmt("total number of inodes is %lu", ci.tot_inos);
  	dbg_cmt("total number of leaf nodes is %llu", ci.leaf_cnt);
  
  out:
  	dbg_free_check_tree(&ci.root);
  	kfree(ci.node);
  	return err;
  }