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: Artem Bityutskiy (Битюцкий Артём) |
| 20 | * Adrian Hunter |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * This file is a part of UBIFS journal implementation and contains various |
| 25 | * functions which manipulate the log. The log is a fixed area on the flash |
| 26 | * which does not contain any data but refers to buds. The log is a part of the |
| 27 | * journal. |
| 28 | */ |
| 29 | |
| 30 | #include "ubifs.h" |
| 31 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 32 | static int dbg_check_bud_bytes(struct ubifs_info *c); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * ubifs_search_bud - search bud LEB. |
| 36 | * @c: UBIFS file-system description object |
| 37 | * @lnum: logical eraseblock number to search |
| 38 | * |
| 39 | * This function searches bud LEB @lnum. Returns bud description object in case |
| 40 | * of success and %NULL if there is no bud with this LEB number. |
| 41 | */ |
| 42 | struct ubifs_bud *ubifs_search_bud(struct ubifs_info *c, int lnum) |
| 43 | { |
| 44 | struct rb_node *p; |
| 45 | struct ubifs_bud *bud; |
| 46 | |
| 47 | spin_lock(&c->buds_lock); |
| 48 | p = c->buds.rb_node; |
| 49 | while (p) { |
| 50 | bud = rb_entry(p, struct ubifs_bud, rb); |
| 51 | if (lnum < bud->lnum) |
| 52 | p = p->rb_left; |
| 53 | else if (lnum > bud->lnum) |
| 54 | p = p->rb_right; |
| 55 | else { |
| 56 | spin_unlock(&c->buds_lock); |
| 57 | return bud; |
| 58 | } |
| 59 | } |
| 60 | spin_unlock(&c->buds_lock); |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * ubifs_get_wbuf - get the wbuf associated with a LEB, if there is one. |
| 66 | * @c: UBIFS file-system description object |
| 67 | * @lnum: logical eraseblock number to search |
| 68 | * |
| 69 | * This functions returns the wbuf for @lnum or %NULL if there is not one. |
| 70 | */ |
| 71 | struct ubifs_wbuf *ubifs_get_wbuf(struct ubifs_info *c, int lnum) |
| 72 | { |
| 73 | struct rb_node *p; |
| 74 | struct ubifs_bud *bud; |
| 75 | int jhead; |
| 76 | |
| 77 | if (!c->jheads) |
| 78 | return NULL; |
| 79 | |
| 80 | spin_lock(&c->buds_lock); |
| 81 | p = c->buds.rb_node; |
| 82 | while (p) { |
| 83 | bud = rb_entry(p, struct ubifs_bud, rb); |
| 84 | if (lnum < bud->lnum) |
| 85 | p = p->rb_left; |
| 86 | else if (lnum > bud->lnum) |
| 87 | p = p->rb_right; |
| 88 | else { |
| 89 | jhead = bud->jhead; |
| 90 | spin_unlock(&c->buds_lock); |
| 91 | return &c->jheads[jhead].wbuf; |
| 92 | } |
| 93 | } |
| 94 | spin_unlock(&c->buds_lock); |
| 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | /** |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 99 | * empty_log_bytes - calculate amount of empty space in the log. |
| 100 | * @c: UBIFS file-system description object |
| 101 | */ |
| 102 | static inline long long empty_log_bytes(const struct ubifs_info *c) |
| 103 | { |
| 104 | long long h, t; |
| 105 | |
| 106 | h = (long long)c->lhead_lnum * c->leb_size + c->lhead_offs; |
| 107 | t = (long long)c->ltail_lnum * c->leb_size; |
| 108 | |
Artem Bityutskiy | ba29e72 | 2014-07-16 15:22:29 +0300 | [diff] [blame] | 109 | if (h > t) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 110 | return c->log_bytes - h + t; |
Artem Bityutskiy | ba29e72 | 2014-07-16 15:22:29 +0300 | [diff] [blame] | 111 | else if (h != t) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 112 | return t - h; |
Artem Bityutskiy | ba29e72 | 2014-07-16 15:22:29 +0300 | [diff] [blame] | 113 | else if (c->lhead_lnum != c->ltail_lnum) |
| 114 | return 0; |
| 115 | else |
| 116 | return c->log_bytes; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
| 120 | * ubifs_add_bud - add bud LEB to the tree of buds and its journal head list. |
| 121 | * @c: UBIFS file-system description object |
| 122 | * @bud: the bud to add |
| 123 | */ |
| 124 | void ubifs_add_bud(struct ubifs_info *c, struct ubifs_bud *bud) |
| 125 | { |
| 126 | struct rb_node **p, *parent = NULL; |
| 127 | struct ubifs_bud *b; |
| 128 | struct ubifs_jhead *jhead; |
| 129 | |
| 130 | spin_lock(&c->buds_lock); |
| 131 | p = &c->buds.rb_node; |
| 132 | while (*p) { |
| 133 | parent = *p; |
| 134 | b = rb_entry(parent, struct ubifs_bud, rb); |
Richard Weinberger | 6eb61d5 | 2018-07-12 13:01:57 +0200 | [diff] [blame] | 135 | ubifs_assert(c, bud->lnum != b->lnum); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 136 | if (bud->lnum < b->lnum) |
| 137 | p = &(*p)->rb_left; |
| 138 | else |
| 139 | p = &(*p)->rb_right; |
| 140 | } |
| 141 | |
| 142 | rb_link_node(&bud->rb, parent, p); |
| 143 | rb_insert_color(&bud->rb, &c->buds); |
| 144 | if (c->jheads) { |
| 145 | jhead = &c->jheads[bud->jhead]; |
| 146 | list_add_tail(&bud->list, &jhead->buds_list); |
| 147 | } else |
Richard Weinberger | 6eb61d5 | 2018-07-12 13:01:57 +0200 | [diff] [blame] | 148 | ubifs_assert(c, c->replaying && c->ro_mount); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 149 | |
| 150 | /* |
| 151 | * Note, although this is a new bud, we anyway account this space now, |
| 152 | * before any data has been written to it, because this is about to |
| 153 | * guarantee fixed mount time, and this bud will anyway be read and |
| 154 | * scanned. |
| 155 | */ |
| 156 | c->bud_bytes += c->leb_size - bud->start; |
| 157 | |
Artem Bityutskiy | 77a7ae5 | 2009-09-15 15:03:51 +0300 | [diff] [blame] | 158 | dbg_log("LEB %d:%d, jhead %s, bud_bytes %lld", bud->lnum, |
| 159 | bud->start, dbg_jhead(bud->jhead), c->bud_bytes); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 160 | spin_unlock(&c->buds_lock); |
| 161 | } |
| 162 | |
| 163 | /** |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 164 | * ubifs_add_bud_to_log - add a new bud to the log. |
| 165 | * @c: UBIFS file-system description object |
| 166 | * @jhead: journal head the bud belongs to |
| 167 | * @lnum: LEB number of the bud |
| 168 | * @offs: starting offset of the bud |
| 169 | * |
Sascha Hauer | 71d561f0 | 2018-05-14 10:18:17 +0200 | [diff] [blame] | 170 | * This function writes a reference node for the new bud LEB @lnum to the log, |
| 171 | * and adds it to the buds trees. It also makes sure that log size does not |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 172 | * exceed the 'c->max_bud_bytes' limit. Returns zero in case of success, |
Sascha Hauer | 71d561f0 | 2018-05-14 10:18:17 +0200 | [diff] [blame] | 173 | * %-EAGAIN if commit is required, and a negative error code in case of |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 174 | * failure. |
| 175 | */ |
| 176 | int ubifs_add_bud_to_log(struct ubifs_info *c, int jhead, int lnum, int offs) |
| 177 | { |
| 178 | int err; |
| 179 | struct ubifs_bud *bud; |
| 180 | struct ubifs_ref_node *ref; |
| 181 | |
| 182 | bud = kmalloc(sizeof(struct ubifs_bud), GFP_NOFS); |
| 183 | if (!bud) |
| 184 | return -ENOMEM; |
| 185 | ref = kzalloc(c->ref_node_alsz, GFP_NOFS); |
| 186 | if (!ref) { |
| 187 | kfree(bud); |
| 188 | return -ENOMEM; |
| 189 | } |
| 190 | |
| 191 | mutex_lock(&c->log_mutex); |
Richard Weinberger | 6eb61d5 | 2018-07-12 13:01:57 +0200 | [diff] [blame] | 192 | ubifs_assert(c, !c->ro_media && !c->ro_mount); |
Artem Bityutskiy | 2680d72 | 2010-09-17 16:44:28 +0300 | [diff] [blame] | 193 | if (c->ro_error) { |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 194 | err = -EROFS; |
| 195 | goto out_unlock; |
| 196 | } |
| 197 | |
| 198 | /* Make sure we have enough space in the log */ |
| 199 | if (empty_log_bytes(c) - c->ref_node_alsz < c->min_log_bytes) { |
| 200 | dbg_log("not enough log space - %lld, required %d", |
| 201 | empty_log_bytes(c), c->min_log_bytes); |
| 202 | ubifs_commit_required(c); |
| 203 | err = -EAGAIN; |
| 204 | goto out_unlock; |
| 205 | } |
| 206 | |
| 207 | /* |
Artem Bityutskiy | 7d4e9cc | 2009-03-20 19:11:12 +0200 | [diff] [blame] | 208 | * Make sure the amount of space in buds will not exceed the |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 209 | * 'c->max_bud_bytes' limit, because we want to guarantee mount time |
| 210 | * limits. |
| 211 | * |
| 212 | * It is not necessary to hold @c->buds_lock when reading @c->bud_bytes |
| 213 | * because we are holding @c->log_mutex. All @c->bud_bytes take place |
| 214 | * when both @c->log_mutex and @c->bud_bytes are locked. |
| 215 | */ |
| 216 | if (c->bud_bytes + c->leb_size - offs > c->max_bud_bytes) { |
| 217 | dbg_log("bud bytes %lld (%lld max), require commit", |
| 218 | c->bud_bytes, c->max_bud_bytes); |
| 219 | ubifs_commit_required(c); |
| 220 | err = -EAGAIN; |
| 221 | goto out_unlock; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * If the journal is full enough - start background commit. Note, it is |
| 226 | * OK to read 'c->cmt_state' without spinlock because integer reads |
| 227 | * are atomic in the kernel. |
| 228 | */ |
| 229 | if (c->bud_bytes >= c->bg_bud_bytes && |
| 230 | c->cmt_state == COMMIT_RESTING) { |
| 231 | dbg_log("bud bytes %lld (%lld max), initiate BG commit", |
| 232 | c->bud_bytes, c->max_bud_bytes); |
| 233 | ubifs_request_bg_commit(c); |
| 234 | } |
| 235 | |
| 236 | bud->lnum = lnum; |
| 237 | bud->start = offs; |
| 238 | bud->jhead = jhead; |
Sascha Hauer | 6a98bc4 | 2018-09-07 14:36:36 +0200 | [diff] [blame] | 239 | bud->log_hash = NULL; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 240 | |
| 241 | ref->ch.node_type = UBIFS_REF_NODE; |
| 242 | ref->lnum = cpu_to_le32(bud->lnum); |
| 243 | ref->offs = cpu_to_le32(bud->start); |
| 244 | ref->jhead = cpu_to_le32(jhead); |
| 245 | |
| 246 | if (c->lhead_offs > c->leb_size - c->ref_node_alsz) { |
Artem Bityutskiy | e11602e | 2011-05-06 17:52:32 +0300 | [diff] [blame] | 247 | c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum); |
Richard Weinberger | 6eb61d5 | 2018-07-12 13:01:57 +0200 | [diff] [blame] | 248 | ubifs_assert(c, c->lhead_lnum != c->ltail_lnum); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 249 | c->lhead_offs = 0; |
| 250 | } |
| 251 | |
| 252 | if (c->lhead_offs == 0) { |
| 253 | /* Must ensure next log LEB has been unmapped */ |
| 254 | err = ubifs_leb_unmap(c, c->lhead_lnum); |
| 255 | if (err) |
| 256 | goto out_unlock; |
| 257 | } |
| 258 | |
| 259 | if (bud->start == 0) { |
| 260 | /* |
| 261 | * Before writing the LEB reference which refers an empty LEB |
| 262 | * to the log, we have to make sure it is mapped, because |
| 263 | * otherwise we'd risk to refer an LEB with garbage in case of |
| 264 | * an unclean reboot, because the target LEB might have been |
| 265 | * unmapped, but not yet physically erased. |
| 266 | */ |
Richard Weinberger | b36a261 | 2012-05-14 17:55:51 +0200 | [diff] [blame] | 267 | err = ubifs_leb_map(c, bud->lnum); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 268 | if (err) |
| 269 | goto out_unlock; |
| 270 | } |
| 271 | |
| 272 | dbg_log("write ref LEB %d:%d", |
| 273 | c->lhead_lnum, c->lhead_offs); |
| 274 | err = ubifs_write_node(c, ref, UBIFS_REF_NODE_SZ, c->lhead_lnum, |
Richard Weinberger | b36a261 | 2012-05-14 17:55:51 +0200 | [diff] [blame] | 275 | c->lhead_offs); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 276 | if (err) |
| 277 | goto out_unlock; |
| 278 | |
Sascha Hauer | 6a98bc4 | 2018-09-07 14:36:36 +0200 | [diff] [blame] | 279 | err = ubifs_shash_update(c, c->log_hash, ref, UBIFS_REF_NODE_SZ); |
| 280 | if (err) |
| 281 | goto out_unlock; |
| 282 | |
| 283 | err = ubifs_shash_copy_state(c, c->log_hash, c->jheads[jhead].log_hash); |
| 284 | if (err) |
| 285 | goto out_unlock; |
| 286 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 287 | c->lhead_offs += c->ref_node_alsz; |
| 288 | |
| 289 | ubifs_add_bud(c, bud); |
| 290 | |
| 291 | mutex_unlock(&c->log_mutex); |
| 292 | kfree(ref); |
| 293 | return 0; |
| 294 | |
| 295 | out_unlock: |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 296 | mutex_unlock(&c->log_mutex); |
| 297 | kfree(ref); |
| 298 | kfree(bud); |
| 299 | return err; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * remove_buds - remove used buds. |
| 304 | * @c: UBIFS file-system description object |
| 305 | * |
| 306 | * This function removes use buds from the buds tree. It does not remove the |
| 307 | * buds which are pointed to by journal heads. |
| 308 | */ |
| 309 | static void remove_buds(struct ubifs_info *c) |
| 310 | { |
| 311 | struct rb_node *p; |
| 312 | |
Richard Weinberger | 6eb61d5 | 2018-07-12 13:01:57 +0200 | [diff] [blame] | 313 | ubifs_assert(c, list_empty(&c->old_buds)); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 314 | c->cmt_bud_bytes = 0; |
| 315 | spin_lock(&c->buds_lock); |
| 316 | p = rb_first(&c->buds); |
| 317 | while (p) { |
| 318 | struct rb_node *p1 = p; |
| 319 | struct ubifs_bud *bud; |
| 320 | struct ubifs_wbuf *wbuf; |
| 321 | |
| 322 | p = rb_next(p); |
| 323 | bud = rb_entry(p1, struct ubifs_bud, rb); |
| 324 | wbuf = &c->jheads[bud->jhead].wbuf; |
| 325 | |
| 326 | if (wbuf->lnum == bud->lnum) { |
| 327 | /* |
| 328 | * Do not remove buds which are pointed to by journal |
| 329 | * heads (non-closed buds). |
| 330 | */ |
| 331 | c->cmt_bud_bytes += wbuf->offs - bud->start; |
Artem Bityutskiy | 79fda51 | 2012-08-27 13:34:09 +0300 | [diff] [blame] | 332 | dbg_log("preserve %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld", |
| 333 | bud->lnum, bud->start, dbg_jhead(bud->jhead), |
| 334 | wbuf->offs - bud->start, c->cmt_bud_bytes); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 335 | bud->start = wbuf->offs; |
| 336 | } else { |
| 337 | c->cmt_bud_bytes += c->leb_size - bud->start; |
Artem Bityutskiy | 79fda51 | 2012-08-27 13:34:09 +0300 | [diff] [blame] | 338 | dbg_log("remove %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld", |
| 339 | bud->lnum, bud->start, dbg_jhead(bud->jhead), |
| 340 | c->leb_size - bud->start, c->cmt_bud_bytes); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 341 | rb_erase(p1, &c->buds); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 342 | /* |
| 343 | * If the commit does not finish, the recovery will need |
| 344 | * to replay the journal, in which case the old buds |
| 345 | * must be unchanged. Do not release them until post |
| 346 | * commit i.e. do not allow them to be garbage |
| 347 | * collected. |
| 348 | */ |
Eric Sesterhenn | ec32816 | 2009-02-13 09:13:11 +0100 | [diff] [blame] | 349 | list_move(&bud->list, &c->old_buds); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | spin_unlock(&c->buds_lock); |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * ubifs_log_start_commit - start commit. |
| 357 | * @c: UBIFS file-system description object |
| 358 | * @ltail_lnum: return new log tail LEB number |
| 359 | * |
| 360 | * The commit operation starts with writing "commit start" node to the log and |
| 361 | * reference nodes for all journal heads which will define new journal after |
| 362 | * the commit has been finished. The commit start and reference nodes are |
| 363 | * written in one go to the nearest empty log LEB (hence, when commit is |
| 364 | * finished UBIFS may safely unmap all the previous log LEBs). This function |
| 365 | * returns zero in case of success and a negative error code in case of |
| 366 | * failure. |
| 367 | */ |
| 368 | int ubifs_log_start_commit(struct ubifs_info *c, int *ltail_lnum) |
| 369 | { |
| 370 | void *buf; |
| 371 | struct ubifs_cs_node *cs; |
| 372 | struct ubifs_ref_node *ref; |
| 373 | int err, i, max_len, len; |
| 374 | |
| 375 | err = dbg_check_bud_bytes(c); |
| 376 | if (err) |
| 377 | return err; |
| 378 | |
| 379 | max_len = UBIFS_CS_NODE_SZ + c->jhead_cnt * UBIFS_REF_NODE_SZ; |
| 380 | max_len = ALIGN(max_len, c->min_io_size); |
| 381 | buf = cs = kmalloc(max_len, GFP_NOFS); |
| 382 | if (!buf) |
| 383 | return -ENOMEM; |
| 384 | |
| 385 | cs->ch.node_type = UBIFS_CS_NODE; |
Artem Bityutskiy | 014eb04 | 2008-07-21 17:14:29 +0300 | [diff] [blame] | 386 | cs->cmt_no = cpu_to_le64(c->cmt_no); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 387 | ubifs_prepare_node(c, cs, UBIFS_CS_NODE_SZ, 0); |
| 388 | |
Sascha Hauer | 6a98bc4 | 2018-09-07 14:36:36 +0200 | [diff] [blame] | 389 | err = ubifs_shash_init(c, c->log_hash); |
| 390 | if (err) |
| 391 | goto out; |
| 392 | |
| 393 | err = ubifs_shash_update(c, c->log_hash, cs, UBIFS_CS_NODE_SZ); |
| 394 | if (err < 0) |
| 395 | goto out; |
| 396 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 397 | /* |
| 398 | * Note, we do not lock 'c->log_mutex' because this is the commit start |
| 399 | * phase and we are exclusively using the log. And we do not lock |
| 400 | * write-buffer because nobody can write to the file-system at this |
| 401 | * phase. |
| 402 | */ |
| 403 | |
| 404 | len = UBIFS_CS_NODE_SZ; |
| 405 | for (i = 0; i < c->jhead_cnt; i++) { |
| 406 | int lnum = c->jheads[i].wbuf.lnum; |
| 407 | int offs = c->jheads[i].wbuf.offs; |
| 408 | |
| 409 | if (lnum == -1 || offs == c->leb_size) |
| 410 | continue; |
| 411 | |
Artem Bityutskiy | 77a7ae5 | 2009-09-15 15:03:51 +0300 | [diff] [blame] | 412 | dbg_log("add ref to LEB %d:%d for jhead %s", |
| 413 | lnum, offs, dbg_jhead(i)); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 414 | ref = buf + len; |
| 415 | ref->ch.node_type = UBIFS_REF_NODE; |
| 416 | ref->lnum = cpu_to_le32(lnum); |
| 417 | ref->offs = cpu_to_le32(offs); |
| 418 | ref->jhead = cpu_to_le32(i); |
| 419 | |
| 420 | ubifs_prepare_node(c, ref, UBIFS_REF_NODE_SZ, 0); |
| 421 | len += UBIFS_REF_NODE_SZ; |
Sascha Hauer | 6a98bc4 | 2018-09-07 14:36:36 +0200 | [diff] [blame] | 422 | |
| 423 | err = ubifs_shash_update(c, c->log_hash, ref, |
| 424 | UBIFS_REF_NODE_SZ); |
| 425 | if (err) |
| 426 | goto out; |
| 427 | ubifs_shash_copy_state(c, c->log_hash, c->jheads[i].log_hash); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | ubifs_pad(c, buf + len, ALIGN(len, c->min_io_size) - len); |
| 431 | |
| 432 | /* Switch to the next log LEB */ |
| 433 | if (c->lhead_offs) { |
Artem Bityutskiy | e11602e | 2011-05-06 17:52:32 +0300 | [diff] [blame] | 434 | c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum); |
Richard Weinberger | 6eb61d5 | 2018-07-12 13:01:57 +0200 | [diff] [blame] | 435 | ubifs_assert(c, c->lhead_lnum != c->ltail_lnum); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 436 | c->lhead_offs = 0; |
| 437 | } |
| 438 | |
Artem Bityutskiy | f1cb705 | 2014-06-29 17:22:16 +0300 | [diff] [blame] | 439 | /* Must ensure next LEB has been unmapped */ |
| 440 | err = ubifs_leb_unmap(c, c->lhead_lnum); |
| 441 | if (err) |
| 442 | goto out; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 443 | |
| 444 | len = ALIGN(len, c->min_io_size); |
| 445 | dbg_log("writing commit start at LEB %d:0, len %d", c->lhead_lnum, len); |
Richard Weinberger | b36a261 | 2012-05-14 17:55:51 +0200 | [diff] [blame] | 446 | err = ubifs_leb_write(c, c->lhead_lnum, cs, 0, len); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 447 | if (err) |
| 448 | goto out; |
| 449 | |
| 450 | *ltail_lnum = c->lhead_lnum; |
| 451 | |
| 452 | c->lhead_offs += len; |
| 453 | if (c->lhead_offs == c->leb_size) { |
Artem Bityutskiy | e11602e | 2011-05-06 17:52:32 +0300 | [diff] [blame] | 454 | c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 455 | c->lhead_offs = 0; |
| 456 | } |
| 457 | |
| 458 | remove_buds(c); |
| 459 | |
| 460 | /* |
| 461 | * We have started the commit and now users may use the rest of the log |
| 462 | * for new writes. |
| 463 | */ |
| 464 | c->min_log_bytes = 0; |
| 465 | |
| 466 | out: |
| 467 | kfree(buf); |
| 468 | return err; |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * ubifs_log_end_commit - end commit. |
| 473 | * @c: UBIFS file-system description object |
| 474 | * @ltail_lnum: new log tail LEB number |
| 475 | * |
| 476 | * This function is called on when the commit operation was finished. It |
Artem Bityutskiy | 052c280 | 2014-06-29 17:00:45 +0300 | [diff] [blame] | 477 | * moves log tail to new position and updates the master node so that it stores |
| 478 | * the new log tail LEB number. Returns zero in case of success and a negative |
| 479 | * error code in case of failure. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 480 | */ |
| 481 | int ubifs_log_end_commit(struct ubifs_info *c, int ltail_lnum) |
| 482 | { |
| 483 | int err; |
| 484 | |
| 485 | /* |
| 486 | * At this phase we have to lock 'c->log_mutex' because UBIFS allows FS |
| 487 | * writes during commit. Its only short "commit" start phase when |
| 488 | * writers are blocked. |
| 489 | */ |
| 490 | mutex_lock(&c->log_mutex); |
| 491 | |
| 492 | dbg_log("old tail was LEB %d:0, new tail is LEB %d:0", |
| 493 | c->ltail_lnum, ltail_lnum); |
| 494 | |
| 495 | c->ltail_lnum = ltail_lnum; |
| 496 | /* |
| 497 | * The commit is finished and from now on it must be guaranteed that |
| 498 | * there is always enough space for the next commit. |
| 499 | */ |
| 500 | c->min_log_bytes = c->leb_size; |
| 501 | |
| 502 | spin_lock(&c->buds_lock); |
| 503 | c->bud_bytes -= c->cmt_bud_bytes; |
| 504 | spin_unlock(&c->buds_lock); |
| 505 | |
| 506 | err = dbg_check_bud_bytes(c); |
Artem Bityutskiy | 052c280 | 2014-06-29 17:00:45 +0300 | [diff] [blame] | 507 | if (err) |
| 508 | goto out; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 509 | |
Artem Bityutskiy | 052c280 | 2014-06-29 17:00:45 +0300 | [diff] [blame] | 510 | err = ubifs_write_master(c); |
| 511 | |
| 512 | out: |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 513 | mutex_unlock(&c->log_mutex); |
| 514 | return err; |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * ubifs_log_post_commit - things to do after commit is completed. |
| 519 | * @c: UBIFS file-system description object |
| 520 | * @old_ltail_lnum: old log tail LEB number |
| 521 | * |
| 522 | * Release buds only after commit is completed, because they must be unchanged |
| 523 | * if recovery is needed. |
| 524 | * |
| 525 | * Unmap log LEBs only after commit is completed, because they may be needed for |
| 526 | * recovery. |
| 527 | * |
| 528 | * This function returns %0 on success and a negative error code on failure. |
| 529 | */ |
| 530 | int ubifs_log_post_commit(struct ubifs_info *c, int old_ltail_lnum) |
| 531 | { |
| 532 | int lnum, err = 0; |
| 533 | |
| 534 | while (!list_empty(&c->old_buds)) { |
| 535 | struct ubifs_bud *bud; |
| 536 | |
| 537 | bud = list_entry(c->old_buds.next, struct ubifs_bud, list); |
| 538 | err = ubifs_return_leb(c, bud->lnum); |
| 539 | if (err) |
| 540 | return err; |
| 541 | list_del(&bud->list); |
Sascha Hauer | 6a98bc4 | 2018-09-07 14:36:36 +0200 | [diff] [blame] | 542 | kfree(bud->log_hash); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 543 | kfree(bud); |
| 544 | } |
| 545 | mutex_lock(&c->log_mutex); |
| 546 | for (lnum = old_ltail_lnum; lnum != c->ltail_lnum; |
Artem Bityutskiy | e11602e | 2011-05-06 17:52:32 +0300 | [diff] [blame] | 547 | lnum = ubifs_next_log_lnum(c, lnum)) { |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 548 | dbg_log("unmap log LEB %d", lnum); |
| 549 | err = ubifs_leb_unmap(c, lnum); |
| 550 | if (err) |
| 551 | goto out; |
| 552 | } |
| 553 | out: |
| 554 | mutex_unlock(&c->log_mutex); |
| 555 | return err; |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * struct done_ref - references that have been done. |
| 560 | * @rb: rb-tree node |
| 561 | * @lnum: LEB number |
| 562 | */ |
| 563 | struct done_ref { |
| 564 | struct rb_node rb; |
| 565 | int lnum; |
| 566 | }; |
| 567 | |
| 568 | /** |
| 569 | * done_already - determine if a reference has been done already. |
| 570 | * @done_tree: rb-tree to store references that have been done |
| 571 | * @lnum: LEB number of reference |
| 572 | * |
| 573 | * This function returns %1 if the reference has been done, %0 if not, otherwise |
| 574 | * a negative error code is returned. |
| 575 | */ |
| 576 | static int done_already(struct rb_root *done_tree, int lnum) |
| 577 | { |
| 578 | struct rb_node **p = &done_tree->rb_node, *parent = NULL; |
| 579 | struct done_ref *dr; |
| 580 | |
| 581 | while (*p) { |
| 582 | parent = *p; |
| 583 | dr = rb_entry(parent, struct done_ref, rb); |
| 584 | if (lnum < dr->lnum) |
| 585 | p = &(*p)->rb_left; |
| 586 | else if (lnum > dr->lnum) |
| 587 | p = &(*p)->rb_right; |
| 588 | else |
| 589 | return 1; |
| 590 | } |
| 591 | |
| 592 | dr = kzalloc(sizeof(struct done_ref), GFP_NOFS); |
| 593 | if (!dr) |
| 594 | return -ENOMEM; |
| 595 | |
| 596 | dr->lnum = lnum; |
| 597 | |
| 598 | rb_link_node(&dr->rb, parent, p); |
| 599 | rb_insert_color(&dr->rb, done_tree); |
| 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | /** |
| 605 | * destroy_done_tree - destroy the done tree. |
| 606 | * @done_tree: done tree to destroy |
| 607 | */ |
| 608 | static void destroy_done_tree(struct rb_root *done_tree) |
| 609 | { |
Cody P Schafer | bb25e49 | 2014-01-23 15:56:08 -0800 | [diff] [blame] | 610 | struct done_ref *dr, *n; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 611 | |
Cody P Schafer | bb25e49 | 2014-01-23 15:56:08 -0800 | [diff] [blame] | 612 | rbtree_postorder_for_each_entry_safe(dr, n, done_tree, rb) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 613 | kfree(dr); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /** |
| 617 | * add_node - add a node to the consolidated log. |
| 618 | * @c: UBIFS file-system description object |
| 619 | * @buf: buffer to which to add |
| 620 | * @lnum: LEB number to which to write is passed and returned here |
| 621 | * @offs: offset to where to write is passed and returned here |
| 622 | * @node: node to add |
| 623 | * |
| 624 | * This function returns %0 on success and a negative error code on failure. |
| 625 | */ |
| 626 | static int add_node(struct ubifs_info *c, void *buf, int *lnum, int *offs, |
| 627 | void *node) |
| 628 | { |
| 629 | struct ubifs_ch *ch = node; |
| 630 | int len = le32_to_cpu(ch->len), remains = c->leb_size - *offs; |
| 631 | |
| 632 | if (len > remains) { |
| 633 | int sz = ALIGN(*offs, c->min_io_size), err; |
| 634 | |
| 635 | ubifs_pad(c, buf + *offs, sz - *offs); |
Richard Weinberger | b36a261 | 2012-05-14 17:55:51 +0200 | [diff] [blame] | 636 | err = ubifs_leb_change(c, *lnum, buf, sz); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 637 | if (err) |
| 638 | return err; |
Artem Bityutskiy | e11602e | 2011-05-06 17:52:32 +0300 | [diff] [blame] | 639 | *lnum = ubifs_next_log_lnum(c, *lnum); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 640 | *offs = 0; |
| 641 | } |
| 642 | memcpy(buf + *offs, node, len); |
| 643 | *offs += ALIGN(len, 8); |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * ubifs_consolidate_log - consolidate the log. |
| 649 | * @c: UBIFS file-system description object |
| 650 | * |
| 651 | * Repeated failed commits could cause the log to be full, but at least 1 LEB is |
| 652 | * needed for commit. This function rewrites the reference nodes in the log |
| 653 | * omitting duplicates, and failed CS nodes, and leaving no gaps. |
| 654 | * |
| 655 | * This function returns %0 on success and a negative error code on failure. |
| 656 | */ |
| 657 | int ubifs_consolidate_log(struct ubifs_info *c) |
| 658 | { |
| 659 | struct ubifs_scan_leb *sleb; |
| 660 | struct ubifs_scan_node *snod; |
| 661 | struct rb_root done_tree = RB_ROOT; |
| 662 | int lnum, err, first = 1, write_lnum, offs = 0; |
| 663 | void *buf; |
| 664 | |
| 665 | dbg_rcvry("log tail LEB %d, log head LEB %d", c->ltail_lnum, |
| 666 | c->lhead_lnum); |
| 667 | buf = vmalloc(c->leb_size); |
| 668 | if (!buf) |
| 669 | return -ENOMEM; |
| 670 | lnum = c->ltail_lnum; |
| 671 | write_lnum = lnum; |
| 672 | while (1) { |
Artem Bityutskiy | 348709b | 2009-08-25 15:00:55 +0300 | [diff] [blame] | 673 | sleb = ubifs_scan(c, lnum, 0, c->sbuf, 0); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 674 | if (IS_ERR(sleb)) { |
| 675 | err = PTR_ERR(sleb); |
| 676 | goto out_free; |
| 677 | } |
| 678 | list_for_each_entry(snod, &sleb->nodes, list) { |
| 679 | switch (snod->type) { |
| 680 | case UBIFS_REF_NODE: { |
| 681 | struct ubifs_ref_node *ref = snod->node; |
| 682 | int ref_lnum = le32_to_cpu(ref->lnum); |
| 683 | |
| 684 | err = done_already(&done_tree, ref_lnum); |
| 685 | if (err < 0) |
| 686 | goto out_scan; |
| 687 | if (err != 1) { |
| 688 | err = add_node(c, buf, &write_lnum, |
| 689 | &offs, snod->node); |
| 690 | if (err) |
| 691 | goto out_scan; |
| 692 | } |
| 693 | break; |
| 694 | } |
| 695 | case UBIFS_CS_NODE: |
| 696 | if (!first) |
| 697 | break; |
| 698 | err = add_node(c, buf, &write_lnum, &offs, |
| 699 | snod->node); |
| 700 | if (err) |
| 701 | goto out_scan; |
| 702 | first = 0; |
| 703 | break; |
| 704 | } |
| 705 | } |
| 706 | ubifs_scan_destroy(sleb); |
| 707 | if (lnum == c->lhead_lnum) |
| 708 | break; |
Artem Bityutskiy | e11602e | 2011-05-06 17:52:32 +0300 | [diff] [blame] | 709 | lnum = ubifs_next_log_lnum(c, lnum); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 710 | } |
| 711 | if (offs) { |
| 712 | int sz = ALIGN(offs, c->min_io_size); |
| 713 | |
| 714 | ubifs_pad(c, buf + offs, sz - offs); |
Richard Weinberger | b36a261 | 2012-05-14 17:55:51 +0200 | [diff] [blame] | 715 | err = ubifs_leb_change(c, write_lnum, buf, sz); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 716 | if (err) |
| 717 | goto out_free; |
| 718 | offs = ALIGN(offs, c->min_io_size); |
| 719 | } |
| 720 | destroy_done_tree(&done_tree); |
| 721 | vfree(buf); |
| 722 | if (write_lnum == c->lhead_lnum) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 723 | ubifs_err(c, "log is too full"); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 724 | return -EINVAL; |
| 725 | } |
| 726 | /* Unmap remaining LEBs */ |
| 727 | lnum = write_lnum; |
| 728 | do { |
Artem Bityutskiy | e11602e | 2011-05-06 17:52:32 +0300 | [diff] [blame] | 729 | lnum = ubifs_next_log_lnum(c, lnum); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 730 | err = ubifs_leb_unmap(c, lnum); |
| 731 | if (err) |
| 732 | return err; |
| 733 | } while (lnum != c->lhead_lnum); |
| 734 | c->lhead_lnum = write_lnum; |
| 735 | c->lhead_offs = offs; |
| 736 | dbg_rcvry("new log head at %d:%d", c->lhead_lnum, c->lhead_offs); |
| 737 | return 0; |
| 738 | |
| 739 | out_scan: |
| 740 | ubifs_scan_destroy(sleb); |
| 741 | out_free: |
| 742 | destroy_done_tree(&done_tree); |
| 743 | vfree(buf); |
| 744 | return err; |
| 745 | } |
| 746 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 747 | /** |
| 748 | * dbg_check_bud_bytes - make sure bud bytes calculation are all right. |
| 749 | * @c: UBIFS file-system description object |
| 750 | * |
| 751 | * This function makes sure the amount of flash space used by closed buds |
| 752 | * ('c->bud_bytes' is correct). Returns zero in case of success and %-EINVAL in |
| 753 | * case of failure. |
| 754 | */ |
| 755 | static int dbg_check_bud_bytes(struct ubifs_info *c) |
| 756 | { |
| 757 | int i, err = 0; |
| 758 | struct ubifs_bud *bud; |
| 759 | long long bud_bytes = 0; |
| 760 | |
Artem Bityutskiy | 2b1844a | 2011-06-03 08:31:29 +0300 | [diff] [blame] | 761 | if (!dbg_is_chk_gen(c)) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 762 | return 0; |
| 763 | |
| 764 | spin_lock(&c->buds_lock); |
| 765 | for (i = 0; i < c->jhead_cnt; i++) |
| 766 | list_for_each_entry(bud, &c->jheads[i].buds_list, list) |
| 767 | bud_bytes += c->leb_size - bud->start; |
| 768 | |
| 769 | if (c->bud_bytes != bud_bytes) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 770 | ubifs_err(c, "bad bud_bytes %lld, calculated %lld", |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 771 | c->bud_bytes, bud_bytes); |
| 772 | err = -EINVAL; |
| 773 | } |
| 774 | spin_unlock(&c->buds_lock); |
| 775 | |
| 776 | return err; |
| 777 | } |