davinci: clock: add support for setting sysclk rate
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ubifs / commit.c
blob37fa7ed062d8624fdfe6810aaacda6a6503bcd6a
1 /*
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
13 * more details.
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
43 * cache.
46 #include <linux/freezer.h>
47 #include <linux/kthread.h>
48 #include <linux/slab.h>
49 #include "ubifs.h"
51 /**
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
57 * failure.
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;
65 dbg_cmt("start");
66 if (c->ro_media) {
67 err = -EROFS;
68 goto out_up;
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;
78 c->cmt_no += 1;
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;
98 ubifs_get_lp_stats(c, &lst);
100 up_write(&c->commit_sem);
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;
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);
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;
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;
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);
169 return 0;
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;
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)
199 goto out;
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;
208 else
209 goto out_cmt_unlock;
210 spin_unlock(&c->cs_lock);
212 return do_commit(c);
214 out_cmt_unlock:
215 up_write(&c->commit_sem);
216 out:
217 spin_unlock(&c->cs_lock);
218 return 0;
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
227 * write-buffer;
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
231 * future.
233 int ubifs_bg_thread(void *info)
235 int err;
236 struct ubifs_info *c = info;
238 dbg_msg("background thread \"%s\" started, PID %d",
239 c->bgt_name, current->pid);
240 set_freezable();
242 while (1) {
243 if (kthread_should_stop())
244 break;
246 if (try_to_freeze())
247 continue;
249 set_current_state(TASK_INTERRUPTIBLE);
250 /* Check if there is something to do */
251 if (!c->need_bgt) {
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())
258 break;
259 schedule();
260 continue;
261 } else
262 __set_current_state(TASK_RUNNING);
264 c->need_bgt = 0;
265 err = ubifs_bg_wbufs_sync(c);
266 if (err)
267 ubifs_ro_mode(c, err);
269 run_bg_commit(c);
270 cond_resched();
273 dbg_msg("background thread \"%s\" stops", c->bgt_name);
274 return 0;
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) {
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;
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);
323 } else
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);
347 return 0;
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)
359 int err = 0;
361 spin_lock(&c->cs_lock);
362 if (c->cmt_state == COMMIT_BROKEN) {
363 err = -EINVAL;
364 goto out;
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
386 * re-check it.
388 if (c->cmt_state == COMMIT_BROKEN) {
389 err = -EINVAL;
390 goto out_cmt_unlock;
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);
404 err = do_commit(c);
405 return err;
407 out_cmt_unlock:
408 up_write(&c->commit_sem);
409 out:
410 spin_unlock(&c->cs_lock);
411 return err;
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
423 * and %0 if not.
425 int ubifs_gc_should_commit(struct ubifs_info *c)
427 int ret = 0;
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;
441 #ifdef CONFIG_UBIFS_FS_DEBUG
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)
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)));
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);
482 if (!idx)
483 return -ENOMEM;
485 err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs);
486 if (err)
487 goto out;
489 d->old_zroot_level = le16_to_cpu(idx->level);
490 d->old_zroot_sqnum = le64_to_cpu(idx->ch.sqnum);
491 out:
492 kfree(idx);
493 return err;
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;
512 int first = 1, iip;
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;
518 struct idx_node *i;
519 size_t sz;
521 if (!(ubifs_chk_flags & UBIFS_CHK_OLD_IDX))
522 goto out;
524 INIT_LIST_HEAD(&list);
526 sz = sizeof(struct idx_node) + ubifs_idx_node_sz(c, c->fanout) -
527 UBIFS_IDX_NODE_SZ;
529 /* Start at the old zroot */
530 lnum = d->old_zroot.lnum;
531 offs = d->old_zroot.offs;
532 len = d->old_zroot.len;
533 iip = 0;
536 * Traverse the index tree preorder depth-first i.e. do a node and then
537 * its subtrees from left to right.
539 while (1) {
540 struct ubifs_branch *br;
542 /* Get the next index node */
543 i = kmalloc(sz, GFP_NOFS);
544 if (!i) {
545 err = -ENOMEM;
546 goto out_free;
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;
562 if (first) {
563 first = 0;
564 /* Check root level and sqnum */
565 if (le16_to_cpu(idx->level) != d->old_zroot_level) {
566 err = 2;
567 goto out_dump;
569 if (le64_to_cpu(idx->ch.sqnum) != d->old_zroot_sqnum) {
570 err = 3;
571 goto out_dump;
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) {
581 err = 3;
582 goto out_dump;
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) {
589 err = 4;
590 goto out_dump;
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;
600 if (keys_cmp(c, &upper_key, &u_key) < 0) {
601 err = 6;
602 goto out_dump;
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;
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;
631 } else
632 /* Go down left */
633 iip = 0;
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);
648 } else
649 key_copy(c, &i->upper_key, &upper_key);
651 out:
652 err = dbg_old_index_check_init(c, zroot);
653 if (err)
654 goto out_free;
656 return 0;
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);
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);
674 ubifs_err("failed, error %d", err);
675 if (err > 0)
676 err = -EINVAL;
677 return err;
680 #endif /* CONFIG_UBIFS_FS_DEBUG */