2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
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.
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
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
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
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.
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.
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
46 #include <linux/freezer.h>
47 #include <linux/kthread.h>
48 #include <linux/slab.h>
52 * nothing_to_commit - check if there is nothing to commit.
53 * @c: UBIFS file-system description object
55 * This is a helper function which checks if there is anything to commit. It is
56 * used as an optimization to avoid starting the commit if it is not really
57 * necessary. Indeed, the commit operation always assumes flash I/O (e.g.,
58 * writing the commit start node to the log), and it is better to avoid doing
59 * this unnecessarily. E.g., 'ubifs_sync_fs()' runs the commit, but if there is
60 * nothing to commit, it is more optimal to avoid any flash I/O.
62 * This function has to be called with @c->commit_sem locked for writing -
63 * this function does not take LPT/TNC locks because the @c->commit_sem
64 * guarantees that we have exclusive access to the TNC and LPT data structures.
66 * This function returns %1 if there is nothing to commit and %0 otherwise.
68 static int nothing_to_commit(struct ubifs_info
*c
)
71 * During mounting or remounting from R/O mode to R/W mode we may
72 * commit for various recovery-related reasons.
74 if (c
->mounting
|| c
->remounting_rw
)
78 * If the root TNC node is dirty, we definitely have something to
81 if (c
->zroot
.znode
&& ubifs_zn_dirty(c
->zroot
.znode
))
85 * Even though the TNC is clean, the LPT tree may have dirty nodes. For
86 * example, this may happen if the budgeting subsystem invoked GC to
87 * make some free space, and the GC found an LEB with only dirty and
88 * free space. In this case GC would just change the lprops of this
89 * LEB (by turning all space into free space) and unmap it.
91 if (c
->nroot
&& test_bit(DIRTY_CNODE
, &c
->nroot
->flags
))
94 ubifs_assert(atomic_long_read(&c
->dirty_zn_cnt
) == 0);
95 ubifs_assert(c
->dirty_pn_cnt
== 0);
96 ubifs_assert(c
->dirty_nn_cnt
== 0);
102 * do_commit - commit the journal.
103 * @c: UBIFS file-system description object
105 * This function implements UBIFS commit. It has to be called with commit lock
106 * locked. Returns zero in case of success and a negative error code in case of
109 static int do_commit(struct ubifs_info
*c
)
111 int err
, new_ltail_lnum
, old_ltail_lnum
, i
;
112 struct ubifs_zbranch zroot
;
113 struct ubifs_lp_stats lst
;
116 ubifs_assert(!c
->ro_media
&& !c
->ro_mount
);
123 if (nothing_to_commit(c
)) {
124 up_write(&c
->commit_sem
);
129 /* Sync all write buffers (necessary for recovery) */
130 for (i
= 0; i
< c
->jhead_cnt
; i
++) {
131 err
= ubifs_wbuf_sync(&c
->jheads
[i
].wbuf
);
137 err
= ubifs_gc_start_commit(c
);
140 err
= dbg_check_lprops(c
);
143 err
= ubifs_log_start_commit(c
, &new_ltail_lnum
);
146 err
= ubifs_tnc_start_commit(c
, &zroot
);
149 err
= ubifs_lpt_start_commit(c
);
152 err
= ubifs_orphan_start_commit(c
);
156 ubifs_get_lp_stats(c
, &lst
);
158 up_write(&c
->commit_sem
);
160 err
= ubifs_tnc_end_commit(c
);
163 err
= ubifs_lpt_end_commit(c
);
166 err
= ubifs_orphan_end_commit(c
);
169 old_ltail_lnum
= c
->ltail_lnum
;
170 err
= ubifs_log_end_commit(c
, new_ltail_lnum
);
173 err
= dbg_check_old_index(c
, &zroot
);
177 c
->mst_node
->cmt_no
= cpu_to_le64(c
->cmt_no
);
178 c
->mst_node
->log_lnum
= cpu_to_le32(new_ltail_lnum
);
179 c
->mst_node
->root_lnum
= cpu_to_le32(zroot
.lnum
);
180 c
->mst_node
->root_offs
= cpu_to_le32(zroot
.offs
);
181 c
->mst_node
->root_len
= cpu_to_le32(zroot
.len
);
182 c
->mst_node
->ihead_lnum
= cpu_to_le32(c
->ihead_lnum
);
183 c
->mst_node
->ihead_offs
= cpu_to_le32(c
->ihead_offs
);
184 c
->mst_node
->index_size
= cpu_to_le64(c
->bi
.old_idx_sz
);
185 c
->mst_node
->lpt_lnum
= cpu_to_le32(c
->lpt_lnum
);
186 c
->mst_node
->lpt_offs
= cpu_to_le32(c
->lpt_offs
);
187 c
->mst_node
->nhead_lnum
= cpu_to_le32(c
->nhead_lnum
);
188 c
->mst_node
->nhead_offs
= cpu_to_le32(c
->nhead_offs
);
189 c
->mst_node
->ltab_lnum
= cpu_to_le32(c
->ltab_lnum
);
190 c
->mst_node
->ltab_offs
= cpu_to_le32(c
->ltab_offs
);
191 c
->mst_node
->lsave_lnum
= cpu_to_le32(c
->lsave_lnum
);
192 c
->mst_node
->lsave_offs
= cpu_to_le32(c
->lsave_offs
);
193 c
->mst_node
->lscan_lnum
= cpu_to_le32(c
->lscan_lnum
);
194 c
->mst_node
->empty_lebs
= cpu_to_le32(lst
.empty_lebs
);
195 c
->mst_node
->idx_lebs
= cpu_to_le32(lst
.idx_lebs
);
196 c
->mst_node
->total_free
= cpu_to_le64(lst
.total_free
);
197 c
->mst_node
->total_dirty
= cpu_to_le64(lst
.total_dirty
);
198 c
->mst_node
->total_used
= cpu_to_le64(lst
.total_used
);
199 c
->mst_node
->total_dead
= cpu_to_le64(lst
.total_dead
);
200 c
->mst_node
->total_dark
= cpu_to_le64(lst
.total_dark
);
202 c
->mst_node
->flags
|= cpu_to_le32(UBIFS_MST_NO_ORPHS
);
204 c
->mst_node
->flags
&= ~cpu_to_le32(UBIFS_MST_NO_ORPHS
);
205 err
= ubifs_write_master(c
);
209 err
= ubifs_log_post_commit(c
, old_ltail_lnum
);
212 err
= ubifs_gc_end_commit(c
);
215 err
= ubifs_lpt_post_commit(c
);
220 spin_lock(&c
->cs_lock
);
221 c
->cmt_state
= COMMIT_RESTING
;
223 dbg_cmt("commit end");
224 spin_unlock(&c
->cs_lock
);
228 up_write(&c
->commit_sem
);
230 ubifs_err("commit failed, error %d", err
);
231 spin_lock(&c
->cs_lock
);
232 c
->cmt_state
= COMMIT_BROKEN
;
234 spin_unlock(&c
->cs_lock
);
235 ubifs_ro_mode(c
, err
);
240 * run_bg_commit - run background commit if it is needed.
241 * @c: UBIFS file-system description object
243 * This function runs background commit if it is needed. Returns zero in case
244 * of success and a negative error code in case of failure.
246 static int run_bg_commit(struct ubifs_info
*c
)
248 spin_lock(&c
->cs_lock
);
250 * Run background commit only if background commit was requested or if
251 * commit is required.
253 if (c
->cmt_state
!= COMMIT_BACKGROUND
&&
254 c
->cmt_state
!= COMMIT_REQUIRED
)
256 spin_unlock(&c
->cs_lock
);
258 down_write(&c
->commit_sem
);
259 spin_lock(&c
->cs_lock
);
260 if (c
->cmt_state
== COMMIT_REQUIRED
)
261 c
->cmt_state
= COMMIT_RUNNING_REQUIRED
;
262 else if (c
->cmt_state
== COMMIT_BACKGROUND
)
263 c
->cmt_state
= COMMIT_RUNNING_BACKGROUND
;
266 spin_unlock(&c
->cs_lock
);
271 up_write(&c
->commit_sem
);
273 spin_unlock(&c
->cs_lock
);
278 * ubifs_bg_thread - UBIFS background thread function.
279 * @info: points to the file-system description object
281 * This function implements various file-system background activities:
282 * o when a write-buffer timer expires it synchronizes the appropriate
284 * o when the journal is about to be full, it starts in-advance commit.
286 * Note, other stuff like background garbage collection may be added here in
289 int ubifs_bg_thread(void *info
)
292 struct ubifs_info
*c
= info
;
294 ubifs_msg("background thread \"%s\" started, PID %d",
295 c
->bgt_name
, current
->pid
);
299 if (kthread_should_stop())
305 set_current_state(TASK_INTERRUPTIBLE
);
306 /* Check if there is something to do */
309 * Nothing prevents us from going sleep now and
310 * be never woken up and block the task which
311 * could wait in 'kthread_stop()' forever.
313 if (kthread_should_stop())
318 __set_current_state(TASK_RUNNING
);
321 err
= ubifs_bg_wbufs_sync(c
);
323 ubifs_ro_mode(c
, err
);
329 ubifs_msg("background thread \"%s\" stops", c
->bgt_name
);
334 * ubifs_commit_required - set commit state to "required".
335 * @c: UBIFS file-system description object
337 * This function is called if a commit is required but cannot be done from the
338 * calling function, so it is just flagged instead.
340 void ubifs_commit_required(struct ubifs_info
*c
)
342 spin_lock(&c
->cs_lock
);
343 switch (c
->cmt_state
) {
345 case COMMIT_BACKGROUND
:
346 dbg_cmt("old: %s, new: %s", dbg_cstate(c
->cmt_state
),
347 dbg_cstate(COMMIT_REQUIRED
));
348 c
->cmt_state
= COMMIT_REQUIRED
;
350 case COMMIT_RUNNING_BACKGROUND
:
351 dbg_cmt("old: %s, new: %s", dbg_cstate(c
->cmt_state
),
352 dbg_cstate(COMMIT_RUNNING_REQUIRED
));
353 c
->cmt_state
= COMMIT_RUNNING_REQUIRED
;
355 case COMMIT_REQUIRED
:
356 case COMMIT_RUNNING_REQUIRED
:
360 spin_unlock(&c
->cs_lock
);
364 * ubifs_request_bg_commit - notify the background thread to do a commit.
365 * @c: UBIFS file-system description object
367 * This function is called if the journal is full enough to make a commit
368 * worthwhile, so background thread is kicked to start it.
370 void ubifs_request_bg_commit(struct ubifs_info
*c
)
372 spin_lock(&c
->cs_lock
);
373 if (c
->cmt_state
== COMMIT_RESTING
) {
374 dbg_cmt("old: %s, new: %s", dbg_cstate(c
->cmt_state
),
375 dbg_cstate(COMMIT_BACKGROUND
));
376 c
->cmt_state
= COMMIT_BACKGROUND
;
377 spin_unlock(&c
->cs_lock
);
378 ubifs_wake_up_bgt(c
);
380 spin_unlock(&c
->cs_lock
);
384 * wait_for_commit - wait for commit.
385 * @c: UBIFS file-system description object
387 * This function sleeps until the commit operation is no longer running.
389 static int wait_for_commit(struct ubifs_info
*c
)
391 dbg_cmt("pid %d goes sleep", current
->pid
);
394 * The following sleeps if the condition is false, and will be woken
395 * when the commit ends. It is possible, although very unlikely, that we
396 * will wake up and see the subsequent commit running, rather than the
397 * one we were waiting for, and go back to sleep. However, we will be
398 * woken again, so there is no danger of sleeping forever.
400 wait_event(c
->cmt_wq
, c
->cmt_state
!= COMMIT_RUNNING_BACKGROUND
&&
401 c
->cmt_state
!= COMMIT_RUNNING_REQUIRED
);
402 dbg_cmt("commit finished, pid %d woke up", current
->pid
);
407 * ubifs_run_commit - run or wait for commit.
408 * @c: UBIFS file-system description object
410 * This function runs commit and returns zero in case of success and a negative
411 * error code in case of failure.
413 int ubifs_run_commit(struct ubifs_info
*c
)
417 spin_lock(&c
->cs_lock
);
418 if (c
->cmt_state
== COMMIT_BROKEN
) {
423 if (c
->cmt_state
== COMMIT_RUNNING_BACKGROUND
)
425 * We set the commit state to 'running required' to indicate
426 * that we want it to complete as quickly as possible.
428 c
->cmt_state
= COMMIT_RUNNING_REQUIRED
;
430 if (c
->cmt_state
== COMMIT_RUNNING_REQUIRED
) {
431 spin_unlock(&c
->cs_lock
);
432 return wait_for_commit(c
);
434 spin_unlock(&c
->cs_lock
);
436 /* Ok, the commit is indeed needed */
438 down_write(&c
->commit_sem
);
439 spin_lock(&c
->cs_lock
);
441 * Since we unlocked 'c->cs_lock', the state may have changed, so
444 if (c
->cmt_state
== COMMIT_BROKEN
) {
449 if (c
->cmt_state
== COMMIT_RUNNING_BACKGROUND
)
450 c
->cmt_state
= COMMIT_RUNNING_REQUIRED
;
452 if (c
->cmt_state
== COMMIT_RUNNING_REQUIRED
) {
453 up_write(&c
->commit_sem
);
454 spin_unlock(&c
->cs_lock
);
455 return wait_for_commit(c
);
457 c
->cmt_state
= COMMIT_RUNNING_REQUIRED
;
458 spin_unlock(&c
->cs_lock
);
464 up_write(&c
->commit_sem
);
466 spin_unlock(&c
->cs_lock
);
471 * ubifs_gc_should_commit - determine if it is time for GC to run commit.
472 * @c: UBIFS file-system description object
474 * This function is called by garbage collection to determine if commit should
475 * be run. If commit state is @COMMIT_BACKGROUND, which means that the journal
476 * is full enough to start commit, this function returns true. It is not
477 * absolutely necessary to commit yet, but it feels like this should be better
478 * then to keep doing GC. This function returns %1 if GC has to initiate commit
481 int ubifs_gc_should_commit(struct ubifs_info
*c
)
485 spin_lock(&c
->cs_lock
);
486 if (c
->cmt_state
== COMMIT_BACKGROUND
) {
487 dbg_cmt("commit required now");
488 c
->cmt_state
= COMMIT_REQUIRED
;
490 dbg_cmt("commit not requested");
491 if (c
->cmt_state
== COMMIT_REQUIRED
)
493 spin_unlock(&c
->cs_lock
);
498 * Everything below is related to debugging.
502 * struct idx_node - hold index nodes during index tree traversal.
504 * @iip: index in parent (slot number of this indexing node in the parent
506 * @upper_key: all keys in this indexing node have to be less or equivalent to
508 * @idx: index node (8-byte aligned because all node structures must be 8-byte
512 struct list_head list
;
514 union ubifs_key upper_key
;
515 struct ubifs_idx_node idx
__aligned(8);
519 * dbg_old_index_check_init - get information for the next old index check.
520 * @c: UBIFS file-system description object
521 * @zroot: root of the index
523 * This function records information about the index that will be needed for the
524 * next old index check i.e. 'dbg_check_old_index()'.
526 * This function returns %0 on success and a negative error code on failure.
528 int dbg_old_index_check_init(struct ubifs_info
*c
, struct ubifs_zbranch
*zroot
)
530 struct ubifs_idx_node
*idx
;
531 int lnum
, offs
, len
, err
= 0;
532 struct ubifs_debug_info
*d
= c
->dbg
;
534 d
->old_zroot
= *zroot
;
535 lnum
= d
->old_zroot
.lnum
;
536 offs
= d
->old_zroot
.offs
;
537 len
= d
->old_zroot
.len
;
539 idx
= kmalloc(c
->max_idx_node_sz
, GFP_NOFS
);
543 err
= ubifs_read_node(c
, idx
, UBIFS_IDX_NODE
, len
, lnum
, offs
);
547 d
->old_zroot_level
= le16_to_cpu(idx
->level
);
548 d
->old_zroot_sqnum
= le64_to_cpu(idx
->ch
.sqnum
);
555 * dbg_check_old_index - check the old copy of the index.
556 * @c: UBIFS file-system description object
557 * @zroot: root of the new index
559 * In order to be able to recover from an unclean unmount, a complete copy of
560 * the index must exist on flash. This is the "old" index. The commit process
561 * must write the "new" index to flash without overwriting or destroying any
562 * part of the old index. This function is run at commit end in order to check
563 * that the old index does indeed exist completely intact.
565 * This function returns %0 on success and a negative error code on failure.
567 int dbg_check_old_index(struct ubifs_info
*c
, struct ubifs_zbranch
*zroot
)
569 int lnum
, offs
, len
, err
= 0, uninitialized_var(last_level
), child_cnt
;
571 struct ubifs_debug_info
*d
= c
->dbg
;
572 union ubifs_key
uninitialized_var(lower_key
), upper_key
, l_key
, u_key
;
573 unsigned long long uninitialized_var(last_sqnum
);
574 struct ubifs_idx_node
*idx
;
575 struct list_head list
;
579 if (!dbg_is_chk_index(c
))
582 INIT_LIST_HEAD(&list
);
584 sz
= sizeof(struct idx_node
) + ubifs_idx_node_sz(c
, c
->fanout
) -
587 /* Start at the old zroot */
588 lnum
= d
->old_zroot
.lnum
;
589 offs
= d
->old_zroot
.offs
;
590 len
= d
->old_zroot
.len
;
594 * Traverse the index tree preorder depth-first i.e. do a node and then
595 * its subtrees from left to right.
598 struct ubifs_branch
*br
;
600 /* Get the next index node */
601 i
= kmalloc(sz
, GFP_NOFS
);
607 /* Keep the index nodes on our path in a linked list */
608 list_add_tail(&i
->list
, &list
);
609 /* Read the index node */
611 err
= ubifs_read_node(c
, idx
, UBIFS_IDX_NODE
, len
, lnum
, offs
);
614 /* Validate index node */
615 child_cnt
= le16_to_cpu(idx
->child_cnt
);
616 if (child_cnt
< 1 || child_cnt
> c
->fanout
) {
622 /* Check root level and sqnum */
623 if (le16_to_cpu(idx
->level
) != d
->old_zroot_level
) {
627 if (le64_to_cpu(idx
->ch
.sqnum
) != d
->old_zroot_sqnum
) {
631 /* Set last values as though root had a parent */
632 last_level
= le16_to_cpu(idx
->level
) + 1;
633 last_sqnum
= le64_to_cpu(idx
->ch
.sqnum
) + 1;
634 key_read(c
, ubifs_idx_key(c
, idx
), &lower_key
);
635 highest_ino_key(c
, &upper_key
, INUM_WATERMARK
);
637 key_copy(c
, &upper_key
, &i
->upper_key
);
638 if (le16_to_cpu(idx
->level
) != last_level
- 1) {
643 * The index is always written bottom up hence a child's sqnum
644 * is always less than the parents.
646 if (le64_to_cpu(idx
->ch
.sqnum
) >= last_sqnum
) {
650 /* Check key range */
651 key_read(c
, ubifs_idx_key(c
, idx
), &l_key
);
652 br
= ubifs_idx_branch(c
, idx
, child_cnt
- 1);
653 key_read(c
, &br
->key
, &u_key
);
654 if (keys_cmp(c
, &lower_key
, &l_key
) > 0) {
658 if (keys_cmp(c
, &upper_key
, &u_key
) < 0) {
662 if (keys_cmp(c
, &upper_key
, &u_key
) == 0)
663 if (!is_hash_key(c
, &u_key
)) {
667 /* Go to next index node */
668 if (le16_to_cpu(idx
->level
) == 0) {
669 /* At the bottom, so go up until can go right */
671 /* Drop the bottom of the list */
674 /* No more list means we are done */
675 if (list_empty(&list
))
677 /* Look at the new bottom */
678 i
= list_entry(list
.prev
, struct idx_node
,
681 /* Can we go right */
682 if (iip
+ 1 < le16_to_cpu(idx
->child_cnt
)) {
686 /* Nope, so go up again */
693 * We have the parent in 'idx' and now we set up for reading the
694 * child pointed to by slot 'iip'.
696 last_level
= le16_to_cpu(idx
->level
);
697 last_sqnum
= le64_to_cpu(idx
->ch
.sqnum
);
698 br
= ubifs_idx_branch(c
, idx
, iip
);
699 lnum
= le32_to_cpu(br
->lnum
);
700 offs
= le32_to_cpu(br
->offs
);
701 len
= le32_to_cpu(br
->len
);
702 key_read(c
, &br
->key
, &lower_key
);
703 if (iip
+ 1 < le16_to_cpu(idx
->child_cnt
)) {
704 br
= ubifs_idx_branch(c
, idx
, iip
+ 1);
705 key_read(c
, &br
->key
, &upper_key
);
707 key_copy(c
, &i
->upper_key
, &upper_key
);
710 err
= dbg_old_index_check_init(c
, zroot
);
717 ubifs_err("dumping index node (iip=%d)", i
->iip
);
718 ubifs_dump_node(c
, idx
);
721 if (!list_empty(&list
)) {
722 i
= list_entry(list
.prev
, struct idx_node
, list
);
723 ubifs_err("dumping parent index node");
724 ubifs_dump_node(c
, &i
->idx
);
727 while (!list_empty(&list
)) {
728 i
= list_entry(list
.next
, struct idx_node
, list
);
732 ubifs_err("failed, error %d", err
);