// SPDX-License-Identifier: GPL-2.0-only /* * This file is part of UBIFS. * * Copyright (C) 2006-2008 Nokia Corporation. * * 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 * the commit is run, are written to the orphan area in 1 or more orphan nodes. * 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. */ static int dbg_check_orphans(struct ubifs_info *c); /** * 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) { … } static struct ubifs_orphan *lookup_orphan(struct ubifs_info *c, ino_t inum) { … } static void __orphan_drop(struct ubifs_info *c, struct ubifs_orphan *o) { … } static void orphan_delete(struct ubifs_info *c, struct ubifs_orphan *orph) { … } /** * 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) { … } /** * 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) { … } /** * 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) { … } /** * 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) { … } /** * do_write_orph_node - write a node to the orphan head. * @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) { … } /** * write_orph_node - write an orphan node. * @c: UBIFS file-system description object * @atomic: write atomically * * This function builds an orphan node from the cnext list and writes it to the * 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) { … } /** * write_orph_nodes - write orphan nodes until there are no more to commit. * @c: UBIFS file-system description object * @atomic: write atomically * * This function writes orphan nodes for all the orphans to commit. On success, * %0 is returned, otherwise a negative error code is returned. */ static int write_orph_nodes(struct ubifs_info *c, int atomic) { … } /** * 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) { … } /** * 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) { … } /** * 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) { … } /** * 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) { … } /** * ubifs_clear_orphans - erase all LEBs used for orphans. * @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. */ int ubifs_clear_orphans(struct ubifs_info *c) { … } /** * do_kill_orphans - remove orphan inodes from the index. * @c: UBIFS file-system description object * @sleb: scanned LEB * @last_cmt_no: cmt_no of last orphan node read is passed and returned here * @outofdate: whether the LEB is out of date is returned here * @last_flagged: whether the end orphan node is encountered * * 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) { … } /** * 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) { … } /** * 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) { … } /* * Everything below is related to debugging. */ struct check_orphan { … }; struct check_info { … }; static bool dbg_find_orphan(struct ubifs_info *c, ino_t inum) { … } static int dbg_ins_check_orphan(struct rb_root *root, ino_t inum) { … } static int dbg_find_check_orphan(struct rb_root *root, ino_t inum) { … } static void dbg_free_check_tree(struct rb_root *root) { … } static int dbg_orphan_check(struct ubifs_info *c, struct ubifs_zbranch *zbr, void *priv) { … } static int dbg_read_orphans(struct check_info *ci, struct ubifs_scan_leb *sleb) { … } static int dbg_scan_orphans(struct ubifs_info *c, struct check_info *ci) { … } static int dbg_check_orphans(struct ubifs_info *c) { … }