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 * do_commit - commit the journal.
53 * @c: UBIFS file-system description object
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
59 static int do_commit(struct ubifs_info
*c
)
61 int err
, new_ltail_lnum
, old_ltail_lnum
, i
;
62 struct ubifs_zbranch zroot
;
63 struct ubifs_lp_stats lst
;
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
);
79 err
= ubifs_gc_start_commit(c
);
82 err
= dbg_check_lprops(c
);
85 err
= ubifs_log_start_commit(c
, &new_ltail_lnum
);
88 err
= ubifs_tnc_start_commit(c
, &zroot
);
91 err
= ubifs_lpt_start_commit(c
);
94 err
= ubifs_orphan_start_commit(c
);
98 ubifs_get_lp_stats(c
, &lst
);
100 up_write(&c
->commit_sem
);
102 err
= ubifs_tnc_end_commit(c
);
105 err
= ubifs_lpt_end_commit(c
);
108 err
= ubifs_orphan_end_commit(c
);
111 old_ltail_lnum
= c
->ltail_lnum
;
112 err
= ubifs_log_end_commit(c
, new_ltail_lnum
);
115 err
= dbg_check_old_index(c
, &zroot
);
119 mutex_lock(&c
->mst_mutex
);
120 c
->mst_node
->cmt_no
= cpu_to_le64(c
->cmt_no
);
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
);
145 c
->mst_node
->flags
|= cpu_to_le32(UBIFS_MST_NO_ORPHS
);
147 c
->mst_node
->flags
&= ~cpu_to_le32(UBIFS_MST_NO_ORPHS
);
148 err
= ubifs_write_master(c
);
149 mutex_unlock(&c
->mst_mutex
);
153 err
= ubifs_log_post_commit(c
, old_ltail_lnum
);
156 err
= ubifs_gc_end_commit(c
);
159 err
= ubifs_lpt_post_commit(c
);
163 spin_lock(&c
->cs_lock
);
164 c
->cmt_state
= COMMIT_RESTING
;
166 dbg_cmt("commit end");
167 spin_unlock(&c
->cs_lock
);
172 up_write(&c
->commit_sem
);
174 ubifs_err("commit failed, error %d", err
);
175 spin_lock(&c
->cs_lock
);
176 c
->cmt_state
= COMMIT_BROKEN
;
178 spin_unlock(&c
->cs_lock
);
179 ubifs_ro_mode(c
, err
);
184 * run_bg_commit - run background commit if it is needed.
185 * @c: UBIFS file-system description object
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.
190 static int run_bg_commit(struct ubifs_info
*c
)
192 spin_lock(&c
->cs_lock
);
194 * Run background commit only if background commit was requested or if
195 * commit is required.
197 if (c
->cmt_state
!= COMMIT_BACKGROUND
&&
198 c
->cmt_state
!= COMMIT_REQUIRED
)
200 spin_unlock(&c
->cs_lock
);
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
;
210 spin_unlock(&c
->cs_lock
);
215 up_write(&c
->commit_sem
);
217 spin_unlock(&c
->cs_lock
);
222 * ubifs_bg_thread - UBIFS background thread function.
223 * @info: points to the file-system description object
225 * This function implements various file-system background activities:
226 * o when a write-buffer timer expires it synchronizes the appropriate
228 * o when the journal is about to be full, it starts in-advance commit.
230 * Note, other stuff like background garbage collection may be added here in
233 int ubifs_bg_thread(void *info
)
236 struct ubifs_info
*c
= info
;
238 dbg_msg("background thread \"%s\" started, PID %d",
239 c
->bgt_name
, current
->pid
);
243 if (kthread_should_stop())
249 set_current_state(TASK_INTERRUPTIBLE
);
250 /* Check if there is something to do */
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.
257 if (kthread_should_stop())
262 __set_current_state(TASK_RUNNING
);
265 err
= ubifs_bg_wbufs_sync(c
);
267 ubifs_ro_mode(c
, err
);
273 dbg_msg("background thread \"%s\" stops", c
->bgt_name
);
278 * ubifs_commit_required - set commit state to "required".
279 * @c: UBIFS file-system description object
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.
284 void ubifs_commit_required(struct ubifs_info
*c
)
286 spin_lock(&c
->cs_lock
);
287 switch (c
->cmt_state
) {
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
;
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
;
299 case COMMIT_REQUIRED
:
300 case COMMIT_RUNNING_REQUIRED
:
304 spin_unlock(&c
->cs_lock
);
308 * ubifs_request_bg_commit - notify the background thread to do a commit.
309 * @c: UBIFS file-system description object
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.
314 void ubifs_request_bg_commit(struct ubifs_info
*c
)
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
);
324 spin_unlock(&c
->cs_lock
);
328 * wait_for_commit - wait for commit.
329 * @c: UBIFS file-system description object
331 * This function sleeps until the commit operation is no longer running.
333 static int wait_for_commit(struct ubifs_info
*c
)
335 dbg_cmt("pid %d goes sleep", current
->pid
);
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.
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
);
351 * ubifs_run_commit - run or wait for commit.
352 * @c: UBIFS file-system description object
354 * This function runs commit and returns zero in case of success and a negative
355 * error code in case of failure.
357 int ubifs_run_commit(struct ubifs_info
*c
)
361 spin_lock(&c
->cs_lock
);
362 if (c
->cmt_state
== COMMIT_BROKEN
) {
367 if (c
->cmt_state
== COMMIT_RUNNING_BACKGROUND
)
369 * We set the commit state to 'running required' to indicate
370 * that we want it to complete as quickly as possible.
372 c
->cmt_state
= COMMIT_RUNNING_REQUIRED
;
374 if (c
->cmt_state
== COMMIT_RUNNING_REQUIRED
) {
375 spin_unlock(&c
->cs_lock
);
376 return wait_for_commit(c
);
378 spin_unlock(&c
->cs_lock
);
380 /* Ok, the commit is indeed needed */
382 down_write(&c
->commit_sem
);
383 spin_lock(&c
->cs_lock
);
385 * Since we unlocked 'c->cs_lock', the state may have changed, so
388 if (c
->cmt_state
== COMMIT_BROKEN
) {
393 if (c
->cmt_state
== COMMIT_RUNNING_BACKGROUND
)
394 c
->cmt_state
= COMMIT_RUNNING_REQUIRED
;
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
);
401 c
->cmt_state
= COMMIT_RUNNING_REQUIRED
;
402 spin_unlock(&c
->cs_lock
);
408 up_write(&c
->commit_sem
);
410 spin_unlock(&c
->cs_lock
);
415 * ubifs_gc_should_commit - determine if it is time for GC to run commit.
416 * @c: UBIFS file-system description object
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
425 int ubifs_gc_should_commit(struct ubifs_info
*c
)
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
;
434 dbg_cmt("commit not requested");
435 if (c
->cmt_state
== COMMIT_REQUIRED
)
437 spin_unlock(&c
->cs_lock
);
441 #ifdef CONFIG_UBIFS_FS_DEBUG
444 * struct idx_node - hold index nodes during index tree traversal.
446 * @iip: index in parent (slot number of this indexing node in the parent
448 * @upper_key: all keys in this indexing node have to be less or equivalent to
450 * @idx: index node (8-byte aligned because all node structures must be 8-byte
454 struct list_head list
;
456 union ubifs_key upper_key
;
457 struct ubifs_idx_node idx
__attribute__((aligned(8)));
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
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()'.
468 * This function returns %0 on success and a negative error code on failure.
470 int dbg_old_index_check_init(struct ubifs_info
*c
, struct ubifs_zbranch
*zroot
)
472 struct ubifs_idx_node
*idx
;
473 int lnum
, offs
, len
, err
= 0;
474 struct ubifs_debug_info
*d
= c
->dbg
;
476 d
->old_zroot
= *zroot
;
477 lnum
= d
->old_zroot
.lnum
;
478 offs
= d
->old_zroot
.offs
;
479 len
= d
->old_zroot
.len
;
481 idx
= kmalloc(c
->max_idx_node_sz
, GFP_NOFS
);
485 err
= ubifs_read_node(c
, idx
, UBIFS_IDX_NODE
, len
, lnum
, offs
);
489 d
->old_zroot_level
= le16_to_cpu(idx
->level
);
490 d
->old_zroot_sqnum
= le64_to_cpu(idx
->ch
.sqnum
);
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
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.
507 * This function returns %0 on success and a negative error code on failure.
509 int dbg_check_old_index(struct ubifs_info
*c
, struct ubifs_zbranch
*zroot
)
511 int lnum
, offs
, len
, err
= 0, uninitialized_var(last_level
), child_cnt
;
513 struct ubifs_debug_info
*d
= c
->dbg
;
514 union ubifs_key
uninitialized_var(lower_key
), upper_key
, l_key
, u_key
;
515 unsigned long long uninitialized_var(last_sqnum
);
516 struct ubifs_idx_node
*idx
;
517 struct list_head list
;
521 if (!(ubifs_chk_flags
& UBIFS_CHK_OLD_IDX
))
524 INIT_LIST_HEAD(&list
);
526 sz
= sizeof(struct idx_node
) + ubifs_idx_node_sz(c
, c
->fanout
) -
529 /* Start at the old zroot */
530 lnum
= d
->old_zroot
.lnum
;
531 offs
= d
->old_zroot
.offs
;
532 len
= d
->old_zroot
.len
;
536 * Traverse the index tree preorder depth-first i.e. do a node and then
537 * its subtrees from left to right.
540 struct ubifs_branch
*br
;
542 /* Get the next index node */
543 i
= kmalloc(sz
, GFP_NOFS
);
549 /* Keep the index nodes on our path in a linked list */
550 list_add_tail(&i
->list
, &list
);
551 /* Read the index node */
553 err
= ubifs_read_node(c
, idx
, UBIFS_IDX_NODE
, len
, lnum
, offs
);
556 /* Validate index node */
557 child_cnt
= le16_to_cpu(idx
->child_cnt
);
558 if (child_cnt
< 1 || child_cnt
> c
->fanout
) {
564 /* Check root level and sqnum */
565 if (le16_to_cpu(idx
->level
) != d
->old_zroot_level
) {
569 if (le64_to_cpu(idx
->ch
.sqnum
) != d
->old_zroot_sqnum
) {
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
);
579 key_copy(c
, &upper_key
, &i
->upper_key
);
580 if (le16_to_cpu(idx
->level
) != last_level
- 1) {
585 * The index is always written bottom up hence a child's sqnum
586 * is always less than the parents.
588 if (le64_to_cpu(idx
->ch
.sqnum
) >= last_sqnum
) {
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) {
600 if (keys_cmp(c
, &upper_key
, &u_key
) < 0) {
604 if (keys_cmp(c
, &upper_key
, &u_key
) == 0)
605 if (!is_hash_key(c
, &u_key
)) {
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 */
613 /* Drop the bottom of the list */
616 /* No more list means we are done */
617 if (list_empty(&list
))
619 /* Look at the new bottom */
620 i
= list_entry(list
.prev
, struct idx_node
,
623 /* Can we go right */
624 if (iip
+ 1 < le16_to_cpu(idx
->child_cnt
)) {
628 /* Nope, so go up again */
635 * We have the parent in 'idx' and now we set up for reading the
636 * child pointed to by slot 'iip'.
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
);
649 key_copy(c
, &i
->upper_key
, &upper_key
);
652 err
= dbg_old_index_check_init(c
, zroot
);
659 dbg_err("dumping index node (iip=%d)", i
->iip
);
660 dbg_dump_node(c
, idx
);
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
);
669 while (!list_empty(&list
)) {
670 i
= list_entry(list
.next
, struct idx_node
, list
);
674 ubifs_err("failed, error %d", err
);
680 #endif /* CONFIG_UBIFS_FS_DEBUG */