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