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