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