^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * This file is part of UBIFS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2006-2008 Nokia Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Authors: Adrian Hunter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Artem Bityutskiy (Битюцкий Артём)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * This file implements garbage collection. The procedure for garbage collection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * is different depending on whether a LEB as an index LEB (contains index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * nodes) or not. For non-index LEBs, garbage collection finds a LEB which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * contains a lot of dirty space (obsolete nodes), and copies the non-obsolete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * nodes to the journal, at which point the garbage-collected LEB is free to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * reused. For index LEBs, garbage collection marks the non-obsolete index nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * dirty in the TNC, and after the next commit, the garbage-collected LEB is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * to be reused. Garbage collection will cause the number of dirty index nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * to grow, however sufficient space is reserved for the index to ensure the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * commit will never run out of space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * Notes about dead watermark. At current UBIFS implementation we assume that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * LEBs which have less than @c->dead_wm bytes of free + dirty space are full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * and not worth garbage-collecting. The dead watermark is one min. I/O unit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * size, or min. UBIFS node size, depending on what is greater. Indeed, UBIFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * Garbage Collector has to synchronize the GC head's write buffer before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * returning, so this is about wasting one min. I/O unit. However, UBIFS GC can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * actually reclaim even very small pieces of dirty space by garbage collecting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * enough dirty LEBs, but we do not bother doing this at this implementation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * Notes about dark watermark. The results of GC work depends on how big are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * the UBIFS nodes GC deals with. Large nodes make GC waste more space. Indeed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * if GC move data from LEB A to LEB B and nodes in LEB A are large, GC would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * have to waste large pieces of free space at the end of LEB B, because nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * from LEB A would not fit. And the worst situation is when all nodes are of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * maximum size. So dark watermark is the amount of free + dirty space in LEB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * which are guaranteed to be reclaimable. If LEB has less space, the GC might
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * be unable to reclaim it. So, LEBs with free + dirty greater than dark
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * watermark are "good" LEBs from GC's point of view. The other LEBs are not so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * good, and GC takes extra care when moving them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include <linux/list_sort.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include "ubifs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * GC may need to move more than one LEB to make progress. The below constants
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * define "soft" and "hard" limits on the number of LEBs the garbage collector
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * may move.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define SOFT_LEBS_LIMIT 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define HARD_LEBS_LIMIT 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * switch_gc_head - switch the garbage collection journal head.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * This function switch the GC head to the next LEB which is reserved in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * @c->gc_lnum. Returns %0 in case of success, %-EAGAIN if commit is required,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * and other negative error code in case of failures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static int switch_gc_head(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) int err, gc_lnum = c->gc_lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) ubifs_assert(c, gc_lnum != -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) dbg_gc("switch GC head from LEB %d:%d to LEB %d (waste %d bytes)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) wbuf->lnum, wbuf->offs + wbuf->used, gc_lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) c->leb_size - wbuf->offs - wbuf->used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) err = ubifs_wbuf_sync_nolock(wbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * The GC write-buffer was synchronized, we may safely unmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * 'c->gc_lnum'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) err = ubifs_leb_unmap(c, gc_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) err = ubifs_add_bud_to_log(c, GCHD, gc_lnum, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) c->gc_lnum = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) err = ubifs_wbuf_seek_nolock(wbuf, gc_lnum, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * data_nodes_cmp - compare 2 data nodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * @priv: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * @a: first data node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * @b: second data node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * This function compares data nodes @a and @b. Returns %1 if @a has greater
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * inode or block number, and %-1 otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static int data_nodes_cmp(void *priv, struct list_head *a, struct list_head *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) ino_t inuma, inumb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct ubifs_info *c = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct ubifs_scan_node *sa, *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (a == b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) sa = list_entry(a, struct ubifs_scan_node, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) sb = list_entry(b, struct ubifs_scan_node, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) ubifs_assert(c, key_type(c, &sa->key) == UBIFS_DATA_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) ubifs_assert(c, key_type(c, &sb->key) == UBIFS_DATA_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) ubifs_assert(c, sa->type == UBIFS_DATA_NODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) ubifs_assert(c, sb->type == UBIFS_DATA_NODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) inuma = key_inum(c, &sa->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) inumb = key_inum(c, &sb->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (inuma == inumb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) unsigned int blka = key_block(c, &sa->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) unsigned int blkb = key_block(c, &sb->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (blka <= blkb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) } else if (inuma <= inumb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * nondata_nodes_cmp - compare 2 non-data nodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * @priv: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * @a: first node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * @a: second node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * This function compares nodes @a and @b. It makes sure that inode nodes go
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * first and sorted by length in descending order. Directory entry nodes go
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * after inode nodes and are sorted in ascending hash valuer order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static int nondata_nodes_cmp(void *priv, struct list_head *a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct list_head *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) ino_t inuma, inumb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct ubifs_info *c = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct ubifs_scan_node *sa, *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (a == b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) sa = list_entry(a, struct ubifs_scan_node, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) sb = list_entry(b, struct ubifs_scan_node, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) ubifs_assert(c, key_type(c, &sa->key) != UBIFS_DATA_KEY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) key_type(c, &sb->key) != UBIFS_DATA_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) ubifs_assert(c, sa->type != UBIFS_DATA_NODE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) sb->type != UBIFS_DATA_NODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /* Inodes go before directory entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (sa->type == UBIFS_INO_NODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (sb->type == UBIFS_INO_NODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return sb->len - sa->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (sb->type == UBIFS_INO_NODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) ubifs_assert(c, key_type(c, &sa->key) == UBIFS_DENT_KEY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) key_type(c, &sa->key) == UBIFS_XENT_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) ubifs_assert(c, key_type(c, &sb->key) == UBIFS_DENT_KEY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) key_type(c, &sb->key) == UBIFS_XENT_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) ubifs_assert(c, sa->type == UBIFS_DENT_NODE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) sa->type == UBIFS_XENT_NODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) ubifs_assert(c, sb->type == UBIFS_DENT_NODE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) sb->type == UBIFS_XENT_NODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) inuma = key_inum(c, &sa->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) inumb = key_inum(c, &sb->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (inuma == inumb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) uint32_t hasha = key_hash(c, &sa->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) uint32_t hashb = key_hash(c, &sb->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (hasha <= hashb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) } else if (inuma <= inumb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * sort_nodes - sort nodes for GC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * @sleb: describes nodes to sort and contains the result on exit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * @nondata: contains non-data nodes on exit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * @min: minimum node size is returned here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * This function sorts the list of inodes to garbage collect. First of all, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * kills obsolete nodes and separates data and non-data nodes to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * @sleb->nodes and @nondata lists correspondingly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * Data nodes are then sorted in block number order - this is important for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * bulk-read; data nodes with lower inode number go before data nodes with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * higher inode number, and data nodes with lower block number go before data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * nodes with higher block number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * Non-data nodes are sorted as follows.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * o First go inode nodes - they are sorted in descending length order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * o Then go directory entry nodes - they are sorted in hash order, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * should supposedly optimize 'readdir()'. Direntry nodes with lower parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * inode number go before direntry nodes with higher parent inode number,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * and direntry nodes with lower name hash values go before direntry nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * with higher name hash values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * This function returns zero in case of success and a negative error code in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static int sort_nodes(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) struct list_head *nondata, int *min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct ubifs_scan_node *snod, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) *min = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /* Separate data nodes and non-data nodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) list_for_each_entry_safe(snod, tmp, &sleb->nodes, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) ubifs_assert(c, snod->type == UBIFS_INO_NODE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) snod->type == UBIFS_DATA_NODE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) snod->type == UBIFS_DENT_NODE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) snod->type == UBIFS_XENT_NODE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) snod->type == UBIFS_TRUN_NODE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) snod->type == UBIFS_AUTH_NODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (snod->type != UBIFS_INO_NODE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) snod->type != UBIFS_DATA_NODE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) snod->type != UBIFS_DENT_NODE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) snod->type != UBIFS_XENT_NODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) /* Probably truncation node, zap it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) list_del(&snod->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) kfree(snod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) ubifs_assert(c, key_type(c, &snod->key) == UBIFS_DATA_KEY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) key_type(c, &snod->key) == UBIFS_INO_KEY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) key_type(c, &snod->key) == UBIFS_DENT_KEY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) key_type(c, &snod->key) == UBIFS_XENT_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) err = ubifs_tnc_has_node(c, &snod->key, 0, sleb->lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) snod->offs, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) /* The node is obsolete, remove it from the list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) list_del(&snod->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) kfree(snod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (snod->len < *min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) *min = snod->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (key_type(c, &snod->key) != UBIFS_DATA_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) list_move_tail(&snod->list, nondata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) /* Sort data and non-data nodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) list_sort(c, &sleb->nodes, &data_nodes_cmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) list_sort(c, nondata, &nondata_nodes_cmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) err = dbg_check_data_nodes_order(c, &sleb->nodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) err = dbg_check_nondata_nodes_order(c, nondata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * move_node - move a node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * @sleb: describes the LEB to move nodes from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * @snod: the mode to move
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * @wbuf: write-buffer to move node to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * This function moves node @snod to @wbuf, changes TNC correspondingly, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * destroys @snod. Returns zero in case of success and a negative error code in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static int move_node(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) struct ubifs_scan_node *snod, struct ubifs_wbuf *wbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) int err, new_lnum = wbuf->lnum, new_offs = wbuf->offs + wbuf->used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) err = ubifs_wbuf_write_nolock(wbuf, snod->node, snod->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) err = ubifs_tnc_replace(c, &snod->key, sleb->lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) snod->offs, new_lnum, new_offs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) snod->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) list_del(&snod->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) kfree(snod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * move_nodes - move nodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * @sleb: describes the LEB to move nodes from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * This function moves valid nodes from data LEB described by @sleb to the GC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) * journal head. This function returns zero in case of success, %-EAGAIN if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * commit is required, and other negative error codes in case of other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * failures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) static int move_nodes(struct ubifs_info *c, struct ubifs_scan_leb *sleb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) int err, min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) LIST_HEAD(nondata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (wbuf->lnum == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * The GC journal head is not set, because it is the first GC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * invocation since mount.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) err = switch_gc_head(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) err = sort_nodes(c, sleb, &nondata, &min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) /* Write nodes to their new location. Use the first-fit strategy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) int avail, moved = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) struct ubifs_scan_node *snod, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) /* Move data nodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) list_for_each_entry_safe(snod, tmp, &sleb->nodes, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) avail = c->leb_size - wbuf->offs - wbuf->used -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) ubifs_auth_node_sz(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) if (snod->len > avail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) * Do not skip data nodes in order to optimize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * bulk-read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) err = ubifs_shash_update(c, c->jheads[GCHD].log_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) snod->node, snod->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) err = move_node(c, sleb, snod, wbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) moved = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) /* Move non-data nodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) list_for_each_entry_safe(snod, tmp, &nondata, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) avail = c->leb_size - wbuf->offs - wbuf->used -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) ubifs_auth_node_sz(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) if (avail < min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (snod->len > avail) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * Keep going only if this is an inode with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * some data. Otherwise stop and switch the GC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * head. IOW, we assume that data-less inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) * nodes and direntry nodes are roughly of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) * same size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) if (key_type(c, &snod->key) == UBIFS_DENT_KEY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) snod->len == UBIFS_INO_NODE_SZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) err = ubifs_shash_update(c, c->jheads[GCHD].log_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) snod->node, snod->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) err = move_node(c, sleb, snod, wbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) moved = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) if (ubifs_authenticated(c) && moved) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) struct ubifs_auth_node *auth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) auth = kmalloc(ubifs_auth_node_sz(c), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) if (!auth) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) err = ubifs_prepare_auth_node(c, auth,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) c->jheads[GCHD].log_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) kfree(auth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) err = ubifs_wbuf_write_nolock(wbuf, auth,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) ubifs_auth_node_sz(c));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) kfree(auth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) ubifs_add_dirt(c, wbuf->lnum, ubifs_auth_node_sz(c));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) if (list_empty(&sleb->nodes) && list_empty(&nondata))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) * Waste the rest of the space in the LEB and switch to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) * next LEB.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) err = switch_gc_head(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) list_splice_tail(&nondata, &sleb->nodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) * gc_sync_wbufs - sync write-buffers for GC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * We must guarantee that obsoleting nodes are on flash. Unfortunately they may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) * be in a write-buffer instead. That is, a node could be written to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) * write-buffer, obsoleting another node in a LEB that is GC'd. If that LEB is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * erased before the write-buffer is sync'd and then there is an unclean
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) * unmount, then an existing node is lost. To avoid this, we sync all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * write-buffers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * This function returns %0 on success or a negative error code on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) static int gc_sync_wbufs(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) int err, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) for (i = 0; i < c->jhead_cnt; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) if (i == GCHD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * ubifs_garbage_collect_leb - garbage-collect a logical eraseblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) * @lp: describes the LEB to garbage collect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) * This function garbage-collects an LEB and returns one of the @LEB_FREED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) * @LEB_RETAINED, etc positive codes in case of success, %-EAGAIN if commit is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) * required, and other negative error codes in case of failures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) int ubifs_garbage_collect_leb(struct ubifs_info *c, struct ubifs_lprops *lp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) struct ubifs_scan_leb *sleb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) struct ubifs_scan_node *snod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) int err = 0, lnum = lp->lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) ubifs_assert(c, c->gc_lnum != -1 || wbuf->offs + wbuf->used == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) c->need_recovery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) ubifs_assert(c, c->gc_lnum != lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) ubifs_assert(c, wbuf->lnum != lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) if (lp->free + lp->dirty == c->leb_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) /* Special case - a free LEB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) dbg_gc("LEB %d is free, return it", lp->lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) ubifs_assert(c, !(lp->flags & LPROPS_INDEX));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) if (lp->free != c->leb_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) * Write buffers must be sync'd before unmapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) * freeable LEBs, because one of them may contain data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) * which obsoletes something in 'lp->lnum'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) err = gc_sync_wbufs(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) err = ubifs_change_one_lp(c, lp->lnum, c->leb_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 0, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) err = ubifs_leb_unmap(c, lp->lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) if (c->gc_lnum == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) c->gc_lnum = lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) return LEB_RETAINED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) return LEB_FREED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) * We scan the entire LEB even though we only really need to scan up to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) * (c->leb_size - lp->free).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) sleb = ubifs_scan(c, lnum, 0, c->sbuf, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) if (IS_ERR(sleb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) return PTR_ERR(sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) ubifs_assert(c, !list_empty(&sleb->nodes));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) if (snod->type == UBIFS_IDX_NODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) struct ubifs_gced_idx_leb *idx_gc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) dbg_gc("indexing LEB %d (free %d, dirty %d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) lnum, lp->free, lp->dirty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) list_for_each_entry(snod, &sleb->nodes, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) struct ubifs_idx_node *idx = snod->node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) int level = le16_to_cpu(idx->level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) ubifs_assert(c, snod->type == UBIFS_IDX_NODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) key_read(c, ubifs_idx_key(c, idx), &snod->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) err = ubifs_dirty_idx_node(c, &snod->key, level, lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) snod->offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) idx_gc = kmalloc(sizeof(struct ubifs_gced_idx_leb), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) if (!idx_gc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) idx_gc->lnum = lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) idx_gc->unmap = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) list_add(&idx_gc->list, &c->idx_gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) * Don't release the LEB until after the next commit, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) * it may contain data which is needed for recovery. So
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) * although we freed this LEB, it will become usable only after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) * the commit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) err = ubifs_change_one_lp(c, lnum, c->leb_size, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) LPROPS_INDEX, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) err = LEB_FREED_IDX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) dbg_gc("data LEB %d (free %d, dirty %d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) lnum, lp->free, lp->dirty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) err = move_nodes(c, sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) goto out_inc_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) err = gc_sync_wbufs(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) goto out_inc_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) err = ubifs_change_one_lp(c, lnum, c->leb_size, 0, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) goto out_inc_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) /* Allow for races with TNC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) c->gced_lnum = lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) c->gc_seq += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) if (c->gc_lnum == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) c->gc_lnum = lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) err = LEB_RETAINED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) err = ubifs_wbuf_sync_nolock(wbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) err = ubifs_leb_unmap(c, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) err = LEB_FREED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) ubifs_scan_destroy(sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) out_inc_seq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) /* We may have moved at least some nodes so allow for races with TNC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) c->gced_lnum = lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) c->gc_seq += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) * ubifs_garbage_collect - UBIFS garbage collector.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) * @anyway: do GC even if there are free LEBs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) * This function does out-of-place garbage collection. The return codes are:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) * o positive LEB number if the LEB has been freed and may be used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) * o %-EAGAIN if the caller has to run commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) * o %-ENOSPC if GC failed to make any progress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) * o other negative error codes in case of other errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) * Garbage collector writes data to the journal when GC'ing data LEBs, and just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) * marking indexing nodes dirty when GC'ing indexing LEBs. Thus, at some point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) * commit may be required. But commit cannot be run from inside GC, because the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) * caller might be holding the commit lock, so %-EAGAIN is returned instead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) * And this error code means that the caller has to run commit, and re-run GC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) * if there is still no free space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) * There are many reasons why this function may return %-EAGAIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) * o the log is full and there is no space to write an LEB reference for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) * @c->gc_lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) * o the journal is too large and exceeds size limitations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) * o GC moved indexing LEBs, but they can be used only after the commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) * o the shrinker fails to find clean znodes to free and requests the commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) * o etc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) * Note, if the file-system is close to be full, this function may return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) * %-EAGAIN infinitely, so the caller has to limit amount of re-invocations of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) * the function. E.g., this happens if the limits on the journal size are too
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) * tough and GC writes too much to the journal before an LEB is freed. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) * might also mean that the journal is too large, and the TNC becomes to big,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) * so that the shrinker is constantly called, finds not clean znodes to free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) * and requests commit. Well, this may also happen if the journal is all right,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) * but another kernel process consumes too much memory. Anyway, infinite
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) * %-EAGAIN may happen, but in some extreme/misconfiguration cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) int ubifs_garbage_collect(struct ubifs_info *c, int anyway)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) int i, err, ret, min_space = c->dead_wm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) struct ubifs_lprops lp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) ubifs_assert_cmt_locked(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) ubifs_assert(c, !c->ro_media && !c->ro_mount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) if (ubifs_gc_should_commit(c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) if (c->ro_error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) ret = -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) /* We expect the write-buffer to be empty on entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) ubifs_assert(c, !wbuf->used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) for (i = 0; ; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) int space_before, space_after;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) /* Give the commit an opportunity to run */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) if (ubifs_gc_should_commit(c)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) if (i > SOFT_LEBS_LIMIT && !list_empty(&c->idx_gc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) * We've done enough iterations. Indexing LEBs were
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) * moved and will be available after the commit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) dbg_gc("soft limit, some index LEBs GC'ed, -EAGAIN");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) ubifs_commit_required(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) if (i > HARD_LEBS_LIMIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) * We've moved too many LEBs and have not made
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) * progress, give up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) dbg_gc("hard limit, -ENOSPC");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) * Empty and freeable LEBs can turn up while we waited for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) * the wbuf lock, or while we have been running GC. In that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) * case, we should just return one of those instead of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) * continuing to GC dirty LEBs. Hence we request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) * 'ubifs_find_dirty_leb()' to return an empty LEB if it can.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) ret = ubifs_find_dirty_leb(c, &lp, min_space, anyway ? 0 : 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) if (ret == -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) dbg_gc("no more dirty LEBs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) dbg_gc("found LEB %d: free %d, dirty %d, sum %d (min. space %d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) lp.lnum, lp.free, lp.dirty, lp.free + lp.dirty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) min_space);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) space_before = c->leb_size - wbuf->offs - wbuf->used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) if (wbuf->lnum == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) space_before = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) ret = ubifs_garbage_collect_leb(c, &lp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) if (ret == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) * This is not error, so we have to return the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) * LEB to lprops. But if 'ubifs_return_leb()'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) * fails, its failure code is propagated to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) * caller instead of the original '-EAGAIN'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) err = ubifs_return_leb(c, lp.lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) if (ret == LEB_FREED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) /* An LEB has been freed and is ready for use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) dbg_gc("LEB %d freed, return", lp.lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) ret = lp.lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) if (ret == LEB_FREED_IDX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) * This was an indexing LEB and it cannot be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) * immediately used. And instead of requesting the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) * commit straight away, we try to garbage collect some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) * more.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) dbg_gc("indexing LEB %d freed, continue", lp.lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) ubifs_assert(c, ret == LEB_RETAINED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) space_after = c->leb_size - wbuf->offs - wbuf->used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) dbg_gc("LEB %d retained, freed %d bytes", lp.lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) space_after - space_before);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) if (space_after > space_before) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) /* GC makes progress, keep working */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) min_space >>= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) if (min_space < c->dead_wm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) min_space = c->dead_wm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) dbg_gc("did not make progress");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) * GC moved an LEB bud have not done any progress. This means
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) * that the previous GC head LEB contained too few free space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) * and the LEB which was GC'ed contained only large nodes which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) * did not fit that space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) * We can do 2 things:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) * 1. pick another LEB in a hope it'll contain a small node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) * which will fit the space we have at the end of current GC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) * head LEB, but there is no guarantee, so we try this out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) * unless we have already been working for too long;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) * 2. request an LEB with more dirty space, which will force
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) * 'ubifs_find_dirty_leb()' to start scanning the lprops
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) * table, instead of just picking one from the heap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) * (previously it already picked the dirtiest LEB).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) if (i < SOFT_LEBS_LIMIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) dbg_gc("try again");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) min_space <<= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) if (min_space > c->dark_wm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) min_space = c->dark_wm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) dbg_gc("set min. space to %d", min_space);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) if (ret == -ENOSPC && !list_empty(&c->idx_gc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) dbg_gc("no space, some index LEBs GC'ed, -EAGAIN");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) ubifs_commit_required(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) err = ubifs_wbuf_sync_nolock(wbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) err = ubifs_leb_unmap(c, c->gc_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) mutex_unlock(&wbuf->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) ubifs_assert(c, ret < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) ubifs_assert(c, ret != -ENOSPC && ret != -EAGAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) ubifs_wbuf_sync_nolock(wbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) ubifs_ro_mode(c, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) mutex_unlock(&wbuf->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) ubifs_return_leb(c, lp.lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) * ubifs_gc_start_commit - garbage collection at start of commit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) * If a LEB has only dirty and free space, then we may safely unmap it and make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) * it free. Note, we cannot do this with indexing LEBs because dirty space may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) * correspond index nodes that are required for recovery. In that case, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) * LEB cannot be unmapped until after the next commit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) * This function returns %0 upon success and a negative error code upon failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) int ubifs_gc_start_commit(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) struct ubifs_gced_idx_leb *idx_gc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) const struct ubifs_lprops *lp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) int err = 0, flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) ubifs_get_lprops(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) * Unmap (non-index) freeable LEBs. Note that recovery requires that all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) * wbufs are sync'd before this, which is done in 'do_commit()'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) lp = ubifs_fast_find_freeable(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) if (!lp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) ubifs_assert(c, !(lp->flags & LPROPS_TAKEN));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) ubifs_assert(c, !(lp->flags & LPROPS_INDEX));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) err = ubifs_leb_unmap(c, lp->lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) lp = ubifs_change_lp(c, lp, c->leb_size, 0, lp->flags, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) if (IS_ERR(lp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) err = PTR_ERR(lp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) ubifs_assert(c, !(lp->flags & LPROPS_TAKEN));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) ubifs_assert(c, !(lp->flags & LPROPS_INDEX));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) /* Mark GC'd index LEBs OK to unmap after this commit finishes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) list_for_each_entry(idx_gc, &c->idx_gc, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) idx_gc->unmap = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) /* Record index freeable LEBs for unmapping after commit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) lp = ubifs_fast_find_frdi_idx(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) if (IS_ERR(lp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) err = PTR_ERR(lp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) if (!lp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) idx_gc = kmalloc(sizeof(struct ubifs_gced_idx_leb), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) if (!idx_gc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) ubifs_assert(c, !(lp->flags & LPROPS_TAKEN));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) ubifs_assert(c, lp->flags & LPROPS_INDEX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) /* Don't release the LEB until after the next commit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) flags = (lp->flags | LPROPS_TAKEN) ^ LPROPS_INDEX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) lp = ubifs_change_lp(c, lp, c->leb_size, 0, flags, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) if (IS_ERR(lp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) err = PTR_ERR(lp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) kfree(idx_gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) ubifs_assert(c, lp->flags & LPROPS_TAKEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) ubifs_assert(c, !(lp->flags & LPROPS_INDEX));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) idx_gc->lnum = lp->lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) idx_gc->unmap = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) list_add(&idx_gc->list, &c->idx_gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) ubifs_release_lprops(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) * ubifs_gc_end_commit - garbage collection at end of commit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) * This function completes out-of-place garbage collection of index LEBs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) int ubifs_gc_end_commit(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) struct ubifs_gced_idx_leb *idx_gc, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) struct ubifs_wbuf *wbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) wbuf = &c->jheads[GCHD].wbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) list_for_each_entry_safe(idx_gc, tmp, &c->idx_gc, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) if (idx_gc->unmap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) dbg_gc("LEB %d", idx_gc->lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) err = ubifs_leb_unmap(c, idx_gc->lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) err = ubifs_change_one_lp(c, idx_gc->lnum, LPROPS_NC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) LPROPS_NC, 0, LPROPS_TAKEN, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) list_del(&idx_gc->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) kfree(idx_gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) mutex_unlock(&wbuf->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) * ubifs_destroy_idx_gc - destroy idx_gc list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) * This function destroys the @c->idx_gc list. It is called when unmounting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) * so locks are not needed. Returns zero in case of success and a negative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) * error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) void ubifs_destroy_idx_gc(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) while (!list_empty(&c->idx_gc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) struct ubifs_gced_idx_leb *idx_gc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) idx_gc = list_entry(c->idx_gc.next, struct ubifs_gced_idx_leb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) c->idx_gc_cnt -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) list_del(&idx_gc->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) kfree(idx_gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) * ubifs_get_idx_gc_leb - get a LEB from GC'd index LEB list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) * Called during start commit so locks are not needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) int ubifs_get_idx_gc_leb(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) struct ubifs_gced_idx_leb *idx_gc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) int lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) if (list_empty(&c->idx_gc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) idx_gc = list_entry(c->idx_gc.next, struct ubifs_gced_idx_leb, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) lnum = idx_gc->lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) /* c->idx_gc_cnt is updated by the caller when lprops are updated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) list_del(&idx_gc->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) kfree(idx_gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) return lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) }