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