]>
Commit | Line | Data |
---|---|---|
1e51764a AB |
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 functions that manage the running of the commit process. | |
25 | * Each affected module has its own functions to accomplish their part in the | |
26 | * commit and those functions are called here. | |
27 | * | |
28 | * The commit is the process whereby all updates to the index and LEB properties | |
29 | * are written out together and the journal becomes empty. This keeps the | |
30 | * file system consistent - at all times the state can be recreated by reading | |
31 | * the index and LEB properties and then replaying the journal. | |
32 | * | |
33 | * The commit is split into two parts named "commit start" and "commit end". | |
34 | * During commit start, the commit process has exclusive access to the journal | |
35 | * by holding the commit semaphore down for writing. As few I/O operations as | |
36 | * possible are performed during commit start, instead the nodes that are to be | |
37 | * written are merely identified. During commit end, the commit semaphore is no | |
38 | * longer held and the journal is again in operation, allowing users to continue | |
39 | * to use the file system while the bulk of the commit I/O is performed. The | |
40 | * purpose of this two-step approach is to prevent the commit from causing any | |
41 | * latency blips. Note that in any case, the commit does not prevent lookups | |
42 | * (as permitted by the TNC mutex), or access to VFS data structures e.g. page | |
43 | * cache. | |
44 | */ | |
45 | ||
46 | #include <linux/freezer.h> | |
47 | #include <linux/kthread.h> | |
5a0e3ad6 | 48 | #include <linux/slab.h> |
1e51764a AB |
49 | #include "ubifs.h" |
50 | ||
51 | /** | |
52 | * do_commit - commit the journal. | |
53 | * @c: UBIFS file-system description object | |
54 | * | |
55 | * This function implements UBIFS commit. It has to be called with commit lock | |
56 | * locked. Returns zero in case of success and a negative error code in case of | |
57 | * failure. | |
58 | */ | |
59 | static int do_commit(struct ubifs_info *c) | |
60 | { | |
61 | int err, new_ltail_lnum, old_ltail_lnum, i; | |
62 | struct ubifs_zbranch zroot; | |
63 | struct ubifs_lp_stats lst; | |
64 | ||
65 | dbg_cmt("start"); | |
66 | if (c->ro_media) { | |
67 | err = -EROFS; | |
68 | goto out_up; | |
69 | } | |
70 | ||
71 | /* Sync all write buffers (necessary for recovery) */ | |
72 | for (i = 0; i < c->jhead_cnt; i++) { | |
73 | err = ubifs_wbuf_sync(&c->jheads[i].wbuf); | |
74 | if (err) | |
75 | goto out_up; | |
76 | } | |
77 | ||
014eb04b | 78 | c->cmt_no += 1; |
1e51764a AB |
79 | err = ubifs_gc_start_commit(c); |
80 | if (err) | |
81 | goto out_up; | |
82 | err = dbg_check_lprops(c); | |
83 | if (err) | |
84 | goto out_up; | |
85 | err = ubifs_log_start_commit(c, &new_ltail_lnum); | |
86 | if (err) | |
87 | goto out_up; | |
88 | err = ubifs_tnc_start_commit(c, &zroot); | |
89 | if (err) | |
90 | goto out_up; | |
91 | err = ubifs_lpt_start_commit(c); | |
92 | if (err) | |
93 | goto out_up; | |
94 | err = ubifs_orphan_start_commit(c); | |
95 | if (err) | |
96 | goto out_up; | |
97 | ||
98 | ubifs_get_lp_stats(c, &lst); | |
99 | ||
100 | up_write(&c->commit_sem); | |
101 | ||
102 | err = ubifs_tnc_end_commit(c); | |
103 | if (err) | |
104 | goto out; | |
105 | err = ubifs_lpt_end_commit(c); | |
106 | if (err) | |
107 | goto out; | |
108 | err = ubifs_orphan_end_commit(c); | |
109 | if (err) | |
110 | goto out; | |
111 | old_ltail_lnum = c->ltail_lnum; | |
112 | err = ubifs_log_end_commit(c, new_ltail_lnum); | |
113 | if (err) | |
114 | goto out; | |
115 | err = dbg_check_old_index(c, &zroot); | |
116 | if (err) | |
117 | goto out; | |
118 | ||
119 | mutex_lock(&c->mst_mutex); | |
014eb04b | 120 | c->mst_node->cmt_no = cpu_to_le64(c->cmt_no); |
1e51764a AB |
121 | c->mst_node->log_lnum = cpu_to_le32(new_ltail_lnum); |
122 | c->mst_node->root_lnum = cpu_to_le32(zroot.lnum); | |
123 | c->mst_node->root_offs = cpu_to_le32(zroot.offs); | |
124 | c->mst_node->root_len = cpu_to_le32(zroot.len); | |
125 | c->mst_node->ihead_lnum = cpu_to_le32(c->ihead_lnum); | |
126 | c->mst_node->ihead_offs = cpu_to_le32(c->ihead_offs); | |
127 | c->mst_node->index_size = cpu_to_le64(c->old_idx_sz); | |
128 | c->mst_node->lpt_lnum = cpu_to_le32(c->lpt_lnum); | |
129 | c->mst_node->lpt_offs = cpu_to_le32(c->lpt_offs); | |
130 | c->mst_node->nhead_lnum = cpu_to_le32(c->nhead_lnum); | |
131 | c->mst_node->nhead_offs = cpu_to_le32(c->nhead_offs); | |
132 | c->mst_node->ltab_lnum = cpu_to_le32(c->ltab_lnum); | |
133 | c->mst_node->ltab_offs = cpu_to_le32(c->ltab_offs); | |
134 | c->mst_node->lsave_lnum = cpu_to_le32(c->lsave_lnum); | |
135 | c->mst_node->lsave_offs = cpu_to_le32(c->lsave_offs); | |
136 | c->mst_node->lscan_lnum = cpu_to_le32(c->lscan_lnum); | |
137 | c->mst_node->empty_lebs = cpu_to_le32(lst.empty_lebs); | |
138 | c->mst_node->idx_lebs = cpu_to_le32(lst.idx_lebs); | |
139 | c->mst_node->total_free = cpu_to_le64(lst.total_free); | |
140 | c->mst_node->total_dirty = cpu_to_le64(lst.total_dirty); | |
141 | c->mst_node->total_used = cpu_to_le64(lst.total_used); | |
142 | c->mst_node->total_dead = cpu_to_le64(lst.total_dead); | |
143 | c->mst_node->total_dark = cpu_to_le64(lst.total_dark); | |
144 | if (c->no_orphs) | |
145 | c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS); | |
146 | else | |
147 | c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_NO_ORPHS); | |
148 | err = ubifs_write_master(c); | |
149 | mutex_unlock(&c->mst_mutex); | |
150 | if (err) | |
151 | goto out; | |
152 | ||
153 | err = ubifs_log_post_commit(c, old_ltail_lnum); | |
154 | if (err) | |
155 | goto out; | |
156 | err = ubifs_gc_end_commit(c); | |
157 | if (err) | |
158 | goto out; | |
159 | err = ubifs_lpt_post_commit(c); | |
160 | if (err) | |
161 | goto out; | |
162 | ||
163 | spin_lock(&c->cs_lock); | |
164 | c->cmt_state = COMMIT_RESTING; | |
165 | wake_up(&c->cmt_wq); | |
166 | dbg_cmt("commit end"); | |
167 | spin_unlock(&c->cs_lock); | |
168 | ||
169 | return 0; | |
170 | ||
171 | out_up: | |
172 | up_write(&c->commit_sem); | |
173 | out: | |
174 | ubifs_err("commit failed, error %d", err); | |
175 | spin_lock(&c->cs_lock); | |
176 | c->cmt_state = COMMIT_BROKEN; | |
177 | wake_up(&c->cmt_wq); | |
178 | spin_unlock(&c->cs_lock); | |
179 | ubifs_ro_mode(c, err); | |
180 | return err; | |
181 | } | |
182 | ||
183 | /** | |
184 | * run_bg_commit - run background commit if it is needed. | |
185 | * @c: UBIFS file-system description object | |
186 | * | |
187 | * This function runs background commit if it is needed. Returns zero in case | |
188 | * of success and a negative error code in case of failure. | |
189 | */ | |
190 | static int run_bg_commit(struct ubifs_info *c) | |
191 | { | |
192 | spin_lock(&c->cs_lock); | |
193 | /* | |
194 | * Run background commit only if background commit was requested or if | |
195 | * commit is required. | |
196 | */ | |
197 | if (c->cmt_state != COMMIT_BACKGROUND && | |
198 | c->cmt_state != COMMIT_REQUIRED) | |
199 | goto out; | |
200 | spin_unlock(&c->cs_lock); | |
201 | ||
202 | down_write(&c->commit_sem); | |
203 | spin_lock(&c->cs_lock); | |
204 | if (c->cmt_state == COMMIT_REQUIRED) | |
205 | c->cmt_state = COMMIT_RUNNING_REQUIRED; | |
206 | else if (c->cmt_state == COMMIT_BACKGROUND) | |
207 | c->cmt_state = COMMIT_RUNNING_BACKGROUND; | |
208 | else | |
209 | goto out_cmt_unlock; | |
210 | spin_unlock(&c->cs_lock); | |
211 | ||
212 | return do_commit(c); | |
213 | ||
214 | out_cmt_unlock: | |
215 | up_write(&c->commit_sem); | |
216 | out: | |
217 | spin_unlock(&c->cs_lock); | |
218 | return 0; | |
219 | } | |
220 | ||
221 | /** | |
222 | * ubifs_bg_thread - UBIFS background thread function. | |
223 | * @info: points to the file-system description object | |
224 | * | |
225 | * This function implements various file-system background activities: | |
226 | * o when a write-buffer timer expires it synchronizes the appropriate | |
227 | * write-buffer; | |
228 | * o when the journal is about to be full, it starts in-advance commit. | |
229 | * | |
230 | * Note, other stuff like background garbage collection may be added here in | |
231 | * future. | |
232 | */ | |
233 | int ubifs_bg_thread(void *info) | |
234 | { | |
235 | int err; | |
236 | struct ubifs_info *c = info; | |
237 | ||
069782a1 AB |
238 | dbg_msg("background thread \"%s\" started, PID %d", |
239 | c->bgt_name, current->pid); | |
1e51764a AB |
240 | set_freezable(); |
241 | ||
242 | while (1) { | |
243 | if (kthread_should_stop()) | |
244 | break; | |
245 | ||
246 | if (try_to_freeze()) | |
247 | continue; | |
248 | ||
249 | set_current_state(TASK_INTERRUPTIBLE); | |
250 | /* Check if there is something to do */ | |
251 | if (!c->need_bgt) { | |
252 | /* | |
253 | * Nothing prevents us from going sleep now and | |
254 | * be never woken up and block the task which | |
255 | * could wait in 'kthread_stop()' forever. | |
256 | */ | |
257 | if (kthread_should_stop()) | |
258 | break; | |
259 | schedule(); | |
260 | continue; | |
261 | } else | |
262 | __set_current_state(TASK_RUNNING); | |
263 | ||
264 | c->need_bgt = 0; | |
265 | err = ubifs_bg_wbufs_sync(c); | |
266 | if (err) | |
267 | ubifs_ro_mode(c, err); | |
268 | ||
269 | run_bg_commit(c); | |
270 | cond_resched(); | |
271 | } | |
272 | ||
273 | dbg_msg("background thread \"%s\" stops", c->bgt_name); | |
274 | return 0; | |
275 | } | |
276 | ||
277 | /** | |
278 | * ubifs_commit_required - set commit state to "required". | |
279 | * @c: UBIFS file-system description object | |
280 | * | |
281 | * This function is called if a commit is required but cannot be done from the | |
282 | * calling function, so it is just flagged instead. | |
283 | */ | |
284 | void ubifs_commit_required(struct ubifs_info *c) | |
285 | { | |
286 | spin_lock(&c->cs_lock); | |
287 | switch (c->cmt_state) { | |
288 | case COMMIT_RESTING: | |
289 | case COMMIT_BACKGROUND: | |
290 | dbg_cmt("old: %s, new: %s", dbg_cstate(c->cmt_state), | |
291 | dbg_cstate(COMMIT_REQUIRED)); | |
292 | c->cmt_state = COMMIT_REQUIRED; | |
293 | break; | |
294 | case COMMIT_RUNNING_BACKGROUND: | |
295 | dbg_cmt("old: %s, new: %s", dbg_cstate(c->cmt_state), | |
296 | dbg_cstate(COMMIT_RUNNING_REQUIRED)); | |
297 | c->cmt_state = COMMIT_RUNNING_REQUIRED; | |
298 | break; | |
299 | case COMMIT_REQUIRED: | |
300 | case COMMIT_RUNNING_REQUIRED: | |
301 | case COMMIT_BROKEN: | |
302 | break; | |
303 | } | |
304 | spin_unlock(&c->cs_lock); | |
305 | } | |
306 | ||
307 | /** | |
308 | * ubifs_request_bg_commit - notify the background thread to do a commit. | |
309 | * @c: UBIFS file-system description object | |
310 | * | |
311 | * This function is called if the journal is full enough to make a commit | |
312 | * worthwhile, so background thread is kicked to start it. | |
313 | */ | |
314 | void ubifs_request_bg_commit(struct ubifs_info *c) | |
315 | { | |
316 | spin_lock(&c->cs_lock); | |
317 | if (c->cmt_state == COMMIT_RESTING) { | |
318 | dbg_cmt("old: %s, new: %s", dbg_cstate(c->cmt_state), | |
319 | dbg_cstate(COMMIT_BACKGROUND)); | |
320 | c->cmt_state = COMMIT_BACKGROUND; | |
321 | spin_unlock(&c->cs_lock); | |
322 | ubifs_wake_up_bgt(c); | |
323 | } else | |
324 | spin_unlock(&c->cs_lock); | |
325 | } | |
326 | ||
327 | /** | |
328 | * wait_for_commit - wait for commit. | |
329 | * @c: UBIFS file-system description object | |
330 | * | |
331 | * This function sleeps until the commit operation is no longer running. | |
332 | */ | |
333 | static int wait_for_commit(struct ubifs_info *c) | |
334 | { | |
335 | dbg_cmt("pid %d goes sleep", current->pid); | |
336 | ||
337 | /* | |
338 | * The following sleeps if the condition is false, and will be woken | |
339 | * when the commit ends. It is possible, although very unlikely, that we | |
340 | * will wake up and see the subsequent commit running, rather than the | |
341 | * one we were waiting for, and go back to sleep. However, we will be | |
342 | * woken again, so there is no danger of sleeping forever. | |
343 | */ | |
344 | wait_event(c->cmt_wq, c->cmt_state != COMMIT_RUNNING_BACKGROUND && | |
345 | c->cmt_state != COMMIT_RUNNING_REQUIRED); | |
346 | dbg_cmt("commit finished, pid %d woke up", current->pid); | |
347 | return 0; | |
348 | } | |
349 | ||
350 | /** | |
351 | * ubifs_run_commit - run or wait for commit. | |
352 | * @c: UBIFS file-system description object | |
353 | * | |
354 | * This function runs commit and returns zero in case of success and a negative | |
355 | * error code in case of failure. | |
356 | */ | |
357 | int ubifs_run_commit(struct ubifs_info *c) | |
358 | { | |
359 | int err = 0; | |
360 | ||
361 | spin_lock(&c->cs_lock); | |
362 | if (c->cmt_state == COMMIT_BROKEN) { | |
363 | err = -EINVAL; | |
364 | goto out; | |
365 | } | |
366 | ||
367 | if (c->cmt_state == COMMIT_RUNNING_BACKGROUND) | |
368 | /* | |
369 | * We set the commit state to 'running required' to indicate | |
370 | * that we want it to complete as quickly as possible. | |
371 | */ | |
372 | c->cmt_state = COMMIT_RUNNING_REQUIRED; | |
373 | ||
374 | if (c->cmt_state == COMMIT_RUNNING_REQUIRED) { | |
375 | spin_unlock(&c->cs_lock); | |
376 | return wait_for_commit(c); | |
377 | } | |
378 | spin_unlock(&c->cs_lock); | |
379 | ||
380 | /* Ok, the commit is indeed needed */ | |
381 | ||
382 | down_write(&c->commit_sem); | |
383 | spin_lock(&c->cs_lock); | |
384 | /* | |
385 | * Since we unlocked 'c->cs_lock', the state may have changed, so | |
386 | * re-check it. | |
387 | */ | |
388 | if (c->cmt_state == COMMIT_BROKEN) { | |
389 | err = -EINVAL; | |
390 | goto out_cmt_unlock; | |
391 | } | |
392 | ||
393 | if (c->cmt_state == COMMIT_RUNNING_BACKGROUND) | |
394 | c->cmt_state = COMMIT_RUNNING_REQUIRED; | |
395 | ||
396 | if (c->cmt_state == COMMIT_RUNNING_REQUIRED) { | |
397 | up_write(&c->commit_sem); | |
398 | spin_unlock(&c->cs_lock); | |
399 | return wait_for_commit(c); | |
400 | } | |
401 | c->cmt_state = COMMIT_RUNNING_REQUIRED; | |
402 | spin_unlock(&c->cs_lock); | |
403 | ||
404 | err = do_commit(c); | |
405 | return err; | |
406 | ||
407 | out_cmt_unlock: | |
408 | up_write(&c->commit_sem); | |
409 | out: | |
410 | spin_unlock(&c->cs_lock); | |
411 | return err; | |
412 | } | |
413 | ||
414 | /** | |
415 | * ubifs_gc_should_commit - determine if it is time for GC to run commit. | |
416 | * @c: UBIFS file-system description object | |
417 | * | |
418 | * This function is called by garbage collection to determine if commit should | |
419 | * be run. If commit state is @COMMIT_BACKGROUND, which means that the journal | |
420 | * is full enough to start commit, this function returns true. It is not | |
421 | * absolutely necessary to commit yet, but it feels like this should be better | |
422 | * then to keep doing GC. This function returns %1 if GC has to initiate commit | |
423 | * and %0 if not. | |
424 | */ | |
425 | int ubifs_gc_should_commit(struct ubifs_info *c) | |
426 | { | |
427 | int ret = 0; | |
428 | ||
429 | spin_lock(&c->cs_lock); | |
430 | if (c->cmt_state == COMMIT_BACKGROUND) { | |
431 | dbg_cmt("commit required now"); | |
432 | c->cmt_state = COMMIT_REQUIRED; | |
433 | } else | |
434 | dbg_cmt("commit not requested"); | |
435 | if (c->cmt_state == COMMIT_REQUIRED) | |
436 | ret = 1; | |
437 | spin_unlock(&c->cs_lock); | |
438 | return ret; | |
439 | } | |
440 | ||
441 | #ifdef CONFIG_UBIFS_FS_DEBUG | |
442 | ||
443 | /** | |
444 | * struct idx_node - hold index nodes during index tree traversal. | |
445 | * @list: list | |
446 | * @iip: index in parent (slot number of this indexing node in the parent | |
447 | * indexing node) | |
448 | * @upper_key: all keys in this indexing node have to be less or equivalent to | |
449 | * this key | |
450 | * @idx: index node (8-byte aligned because all node structures must be 8-byte | |
451 | * aligned) | |
452 | */ | |
453 | struct idx_node { | |
454 | struct list_head list; | |
455 | int iip; | |
456 | union ubifs_key upper_key; | |
457 | struct ubifs_idx_node idx __attribute__((aligned(8))); | |
458 | }; | |
459 | ||
460 | /** | |
461 | * dbg_old_index_check_init - get information for the next old index check. | |
462 | * @c: UBIFS file-system description object | |
463 | * @zroot: root of the index | |
464 | * | |
465 | * This function records information about the index that will be needed for the | |
466 | * next old index check i.e. 'dbg_check_old_index()'. | |
467 | * | |
468 | * This function returns %0 on success and a negative error code on failure. | |
469 | */ | |
470 | int dbg_old_index_check_init(struct ubifs_info *c, struct ubifs_zbranch *zroot) | |
471 | { | |
472 | struct ubifs_idx_node *idx; | |
473 | int lnum, offs, len, err = 0; | |
17c2f9f8 | 474 | struct ubifs_debug_info *d = c->dbg; |
1e51764a | 475 | |
17c2f9f8 AB |
476 | d->old_zroot = *zroot; |
477 | lnum = d->old_zroot.lnum; | |
478 | offs = d->old_zroot.offs; | |
479 | len = d->old_zroot.len; | |
1e51764a AB |
480 | |
481 | idx = kmalloc(c->max_idx_node_sz, GFP_NOFS); | |
482 | if (!idx) | |
483 | return -ENOMEM; | |
484 | ||
485 | err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs); | |
486 | if (err) | |
487 | goto out; | |
488 | ||
17c2f9f8 AB |
489 | d->old_zroot_level = le16_to_cpu(idx->level); |
490 | d->old_zroot_sqnum = le64_to_cpu(idx->ch.sqnum); | |
1e51764a AB |
491 | out: |
492 | kfree(idx); | |
493 | return err; | |
494 | } | |
495 | ||
496 | /** | |
497 | * dbg_check_old_index - check the old copy of the index. | |
498 | * @c: UBIFS file-system description object | |
499 | * @zroot: root of the new index | |
500 | * | |
501 | * In order to be able to recover from an unclean unmount, a complete copy of | |
502 | * the index must exist on flash. This is the "old" index. The commit process | |
503 | * must write the "new" index to flash without overwriting or destroying any | |
504 | * part of the old index. This function is run at commit end in order to check | |
505 | * that the old index does indeed exist completely intact. | |
506 | * | |
507 | * This function returns %0 on success and a negative error code on failure. | |
508 | */ | |
509 | int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot) | |
510 | { | |
511 | int lnum, offs, len, err = 0, uninitialized_var(last_level), child_cnt; | |
512 | int first = 1, iip; | |
17c2f9f8 | 513 | struct ubifs_debug_info *d = c->dbg; |
83ef2ecd | 514 | union ubifs_key uninitialized_var(lower_key), upper_key, l_key, u_key; |
1e51764a AB |
515 | unsigned long long uninitialized_var(last_sqnum); |
516 | struct ubifs_idx_node *idx; | |
517 | struct list_head list; | |
518 | struct idx_node *i; | |
519 | size_t sz; | |
520 | ||
521 | if (!(ubifs_chk_flags & UBIFS_CHK_OLD_IDX)) | |
522 | goto out; | |
523 | ||
524 | INIT_LIST_HEAD(&list); | |
525 | ||
526 | sz = sizeof(struct idx_node) + ubifs_idx_node_sz(c, c->fanout) - | |
527 | UBIFS_IDX_NODE_SZ; | |
528 | ||
529 | /* Start at the old zroot */ | |
17c2f9f8 AB |
530 | lnum = d->old_zroot.lnum; |
531 | offs = d->old_zroot.offs; | |
532 | len = d->old_zroot.len; | |
1e51764a AB |
533 | iip = 0; |
534 | ||
535 | /* | |
536 | * Traverse the index tree preorder depth-first i.e. do a node and then | |
537 | * its subtrees from left to right. | |
538 | */ | |
539 | while (1) { | |
540 | struct ubifs_branch *br; | |
541 | ||
542 | /* Get the next index node */ | |
543 | i = kmalloc(sz, GFP_NOFS); | |
544 | if (!i) { | |
545 | err = -ENOMEM; | |
546 | goto out_free; | |
547 | } | |
548 | i->iip = iip; | |
549 | /* Keep the index nodes on our path in a linked list */ | |
550 | list_add_tail(&i->list, &list); | |
551 | /* Read the index node */ | |
552 | idx = &i->idx; | |
553 | err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs); | |
554 | if (err) | |
555 | goto out_free; | |
556 | /* Validate index node */ | |
557 | child_cnt = le16_to_cpu(idx->child_cnt); | |
558 | if (child_cnt < 1 || child_cnt > c->fanout) { | |
559 | err = 1; | |
560 | goto out_dump; | |
561 | } | |
562 | if (first) { | |
563 | first = 0; | |
564 | /* Check root level and sqnum */ | |
17c2f9f8 | 565 | if (le16_to_cpu(idx->level) != d->old_zroot_level) { |
1e51764a AB |
566 | err = 2; |
567 | goto out_dump; | |
568 | } | |
17c2f9f8 | 569 | if (le64_to_cpu(idx->ch.sqnum) != d->old_zroot_sqnum) { |
1e51764a AB |
570 | err = 3; |
571 | goto out_dump; | |
572 | } | |
573 | /* Set last values as though root had a parent */ | |
574 | last_level = le16_to_cpu(idx->level) + 1; | |
575 | last_sqnum = le64_to_cpu(idx->ch.sqnum) + 1; | |
576 | key_read(c, ubifs_idx_key(c, idx), &lower_key); | |
577 | highest_ino_key(c, &upper_key, INUM_WATERMARK); | |
578 | } | |
579 | key_copy(c, &upper_key, &i->upper_key); | |
580 | if (le16_to_cpu(idx->level) != last_level - 1) { | |
581 | err = 3; | |
582 | goto out_dump; | |
583 | } | |
584 | /* | |
585 | * The index is always written bottom up hence a child's sqnum | |
586 | * is always less than the parents. | |
587 | */ | |
588 | if (le64_to_cpu(idx->ch.sqnum) >= last_sqnum) { | |
589 | err = 4; | |
590 | goto out_dump; | |
591 | } | |
592 | /* Check key range */ | |
593 | key_read(c, ubifs_idx_key(c, idx), &l_key); | |
594 | br = ubifs_idx_branch(c, idx, child_cnt - 1); | |
595 | key_read(c, &br->key, &u_key); | |
596 | if (keys_cmp(c, &lower_key, &l_key) > 0) { | |
597 | err = 5; | |
598 | goto out_dump; | |
599 | } | |
600 | if (keys_cmp(c, &upper_key, &u_key) < 0) { | |
601 | err = 6; | |
602 | goto out_dump; | |
603 | } | |
604 | if (keys_cmp(c, &upper_key, &u_key) == 0) | |
605 | if (!is_hash_key(c, &u_key)) { | |
606 | err = 7; | |
607 | goto out_dump; | |
608 | } | |
609 | /* Go to next index node */ | |
610 | if (le16_to_cpu(idx->level) == 0) { | |
611 | /* At the bottom, so go up until can go right */ | |
612 | while (1) { | |
613 | /* Drop the bottom of the list */ | |
614 | list_del(&i->list); | |
615 | kfree(i); | |
616 | /* No more list means we are done */ | |
617 | if (list_empty(&list)) | |
618 | goto out; | |
619 | /* Look at the new bottom */ | |
620 | i = list_entry(list.prev, struct idx_node, | |
621 | list); | |
622 | idx = &i->idx; | |
623 | /* Can we go right */ | |
624 | if (iip + 1 < le16_to_cpu(idx->child_cnt)) { | |
625 | iip = iip + 1; | |
626 | break; | |
627 | } else | |
628 | /* Nope, so go up again */ | |
629 | iip = i->iip; | |
630 | } | |
631 | } else | |
632 | /* Go down left */ | |
633 | iip = 0; | |
634 | /* | |
635 | * We have the parent in 'idx' and now we set up for reading the | |
636 | * child pointed to by slot 'iip'. | |
637 | */ | |
638 | last_level = le16_to_cpu(idx->level); | |
639 | last_sqnum = le64_to_cpu(idx->ch.sqnum); | |
640 | br = ubifs_idx_branch(c, idx, iip); | |
641 | lnum = le32_to_cpu(br->lnum); | |
642 | offs = le32_to_cpu(br->offs); | |
643 | len = le32_to_cpu(br->len); | |
644 | key_read(c, &br->key, &lower_key); | |
645 | if (iip + 1 < le16_to_cpu(idx->child_cnt)) { | |
646 | br = ubifs_idx_branch(c, idx, iip + 1); | |
647 | key_read(c, &br->key, &upper_key); | |
648 | } else | |
649 | key_copy(c, &i->upper_key, &upper_key); | |
650 | } | |
651 | out: | |
652 | err = dbg_old_index_check_init(c, zroot); | |
653 | if (err) | |
654 | goto out_free; | |
655 | ||
656 | return 0; | |
657 | ||
658 | out_dump: | |
659 | dbg_err("dumping index node (iip=%d)", i->iip); | |
660 | dbg_dump_node(c, idx); | |
661 | list_del(&i->list); | |
662 | kfree(i); | |
663 | if (!list_empty(&list)) { | |
664 | i = list_entry(list.prev, struct idx_node, list); | |
665 | dbg_err("dumping parent index node"); | |
666 | dbg_dump_node(c, &i->idx); | |
667 | } | |
668 | out_free: | |
669 | while (!list_empty(&list)) { | |
670 | i = list_entry(list.next, struct idx_node, list); | |
671 | list_del(&i->list); | |
672 | kfree(i); | |
673 | } | |
674 | ubifs_err("failed, error %d", err); | |
675 | if (err > 0) | |
676 | err = -EINVAL; | |
677 | return err; | |
678 | } | |
679 | ||
680 | #endif /* CONFIG_UBIFS_FS_DEBUG */ |