Btrfs: align offsets for btrfs_ordered_update_i_size
[safe/jmp/linux-2.6] / fs / jbd2 / journal.c
index 5814410..fed8538 100644 (file)
 #include <linux/debugfs.h>
 #include <linux/seq_file.h>
 #include <linux/math64.h>
+#include <linux/hash.h>
+
+#define CREATE_TRACE_POINTS
+#include <trace/events/jbd2.h>
 
 #include <asm/uaccess.h>
 #include <asm/page.h>
@@ -132,10 +136,6 @@ static int kjournald2(void *arg)
        journal->j_task = current;
        wake_up(&journal->j_wait_done_commit);
 
-       printk(KERN_INFO "kjournald2 starting: pid %d, dev %s, "
-              "commit interval %ld seconds\n", current->pid,
-              journal->j_devname, journal->j_commit_interval / HZ);
-
        /*
         * And now, wait forever for commit wakeup events.
         */
@@ -219,7 +219,8 @@ static int jbd2_journal_start_thread(journal_t *journal)
 {
        struct task_struct *t;
 
-       t = kthread_run(kjournald2, journal, "kjournald2");
+       t = kthread_run(kjournald2, journal, "jbd2/%s",
+                       journal->j_devname);
        if (IS_ERR(t))
                return PTR_ERR(t);
 
@@ -293,6 +294,7 @@ int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
        unsigned int new_offset;
        struct buffer_head *bh_in = jh2bh(jh_in);
        struct jbd2_buffer_trigger_type *triggers;
+       journal_t *journal = transaction->t_journal;
 
        /*
         * The buffer really shouldn't be locked: only the current committing
@@ -306,6 +308,11 @@ int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
        J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
 
        new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
+       /* keep subsequent assertions sane */
+       new_bh->b_state = 0;
+       init_buffer(new_bh, NULL, NULL);
+       atomic_set(&new_bh->b_count, 1);
+       new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
 
        /*
         * If a new transaction has already done a buffer copy-out, then
@@ -384,14 +391,6 @@ repeat:
                kunmap_atomic(mapped_data, KM_USER0);
        }
 
-       /* keep subsequent assertions sane */
-       new_bh->b_state = 0;
-       init_buffer(new_bh, NULL, NULL);
-       atomic_set(&new_bh->b_count, 1);
-       jbd_unlock_bh_state(bh_in);
-
-       new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
-
        set_bh_page(new_bh, new_page, new_offset);
        new_jh->b_transaction = NULL;
        new_bh->b_size = jh2bh(jh_in)->b_size;
@@ -408,7 +407,11 @@ repeat:
         * copying is moved to the transaction's shadow queue.
         */
        JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
-       jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
+       spin_lock(&journal->j_list_lock);
+       __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
+       spin_unlock(&journal->j_list_lock);
+       jbd_unlock_bh_state(bh_in);
+
        JBUFFER_TRACE(new_jh, "file as BJ_IO");
        jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
 
@@ -673,153 +676,6 @@ struct jbd2_stats_proc_session {
        int max;
 };
 
-static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
-                                       struct transaction_stats_s *ts,
-                                       int first)
-{
-       if (ts == s->stats + s->max)
-               ts = s->stats;
-       if (!first && ts == s->stats + s->start)
-               return NULL;
-       while (ts->ts_type == 0) {
-               ts++;
-               if (ts == s->stats + s->max)
-                       ts = s->stats;
-               if (ts == s->stats + s->start)
-                       return NULL;
-       }
-       return ts;
-
-}
-
-static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
-{
-       struct jbd2_stats_proc_session *s = seq->private;
-       struct transaction_stats_s *ts;
-       int l = *pos;
-
-       if (l == 0)
-               return SEQ_START_TOKEN;
-       ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
-       if (!ts)
-               return NULL;
-       l--;
-       while (l) {
-               ts = jbd2_history_skip_empty(s, ++ts, 0);
-               if (!ts)
-                       break;
-               l--;
-       }
-       return ts;
-}
-
-static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
-{
-       struct jbd2_stats_proc_session *s = seq->private;
-       struct transaction_stats_s *ts = v;
-
-       ++*pos;
-       if (v == SEQ_START_TOKEN)
-               return jbd2_history_skip_empty(s, s->stats + s->start, 1);
-       else
-               return jbd2_history_skip_empty(s, ++ts, 0);
-}
-
-static int jbd2_seq_history_show(struct seq_file *seq, void *v)
-{
-       struct transaction_stats_s *ts = v;
-       if (v == SEQ_START_TOKEN) {
-               seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
-                               "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
-                               "wait", "run", "lock", "flush", "log", "hndls",
-                               "block", "inlog", "ctime", "write", "drop",
-                               "close");
-               return 0;
-       }
-       if (ts->ts_type == JBD2_STATS_RUN)
-               seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u "
-                               "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
-                               jiffies_to_msecs(ts->u.run.rs_wait),
-                               jiffies_to_msecs(ts->u.run.rs_running),
-                               jiffies_to_msecs(ts->u.run.rs_locked),
-                               jiffies_to_msecs(ts->u.run.rs_flushing),
-                               jiffies_to_msecs(ts->u.run.rs_logging),
-                               ts->u.run.rs_handle_count,
-                               ts->u.run.rs_blocks,
-                               ts->u.run.rs_blocks_logged);
-       else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
-               seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n",
-                               "C", ts->ts_tid, " ",
-                               jiffies_to_msecs(ts->u.chp.cs_chp_time),
-                               ts->u.chp.cs_written, ts->u.chp.cs_dropped,
-                               ts->u.chp.cs_forced_to_close);
-       else
-               J_ASSERT(0);
-       return 0;
-}
-
-static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
-{
-}
-
-static struct seq_operations jbd2_seq_history_ops = {
-       .start  = jbd2_seq_history_start,
-       .next   = jbd2_seq_history_next,
-       .stop   = jbd2_seq_history_stop,
-       .show   = jbd2_seq_history_show,
-};
-
-static int jbd2_seq_history_open(struct inode *inode, struct file *file)
-{
-       journal_t *journal = PDE(inode)->data;
-       struct jbd2_stats_proc_session *s;
-       int rc, size;
-
-       s = kmalloc(sizeof(*s), GFP_KERNEL);
-       if (s == NULL)
-               return -ENOMEM;
-       size = sizeof(struct transaction_stats_s) * journal->j_history_max;
-       s->stats = kmalloc(size, GFP_KERNEL);
-       if (s->stats == NULL) {
-               kfree(s);
-               return -ENOMEM;
-       }
-       spin_lock(&journal->j_history_lock);
-       memcpy(s->stats, journal->j_history, size);
-       s->max = journal->j_history_max;
-       s->start = journal->j_history_cur % s->max;
-       spin_unlock(&journal->j_history_lock);
-
-       rc = seq_open(file, &jbd2_seq_history_ops);
-       if (rc == 0) {
-               struct seq_file *m = file->private_data;
-               m->private = s;
-       } else {
-               kfree(s->stats);
-               kfree(s);
-       }
-       return rc;
-
-}
-
-static int jbd2_seq_history_release(struct inode *inode, struct file *file)
-{
-       struct seq_file *seq = file->private_data;
-       struct jbd2_stats_proc_session *s = seq->private;
-
-       kfree(s->stats);
-       kfree(s);
-       return seq_release(inode, file);
-}
-
-static struct file_operations jbd2_seq_history_fops = {
-       .owner          = THIS_MODULE,
-       .open           = jbd2_seq_history_open,
-       .read           = seq_read,
-       .llseek         = seq_lseek,
-       .release        = jbd2_seq_history_release,
-};
-
 static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
 {
        return *pos ? NULL : SEQ_START_TOKEN;
@@ -836,29 +692,29 @@ static int jbd2_seq_info_show(struct seq_file *seq, void *v)
 
        if (v != SEQ_START_TOKEN)
                return 0;
-       seq_printf(seq, "%lu transaction, each upto %u blocks\n",
+       seq_printf(seq, "%lu transaction, each up to %u blocks\n",
                        s->stats->ts_tid,
                        s->journal->j_max_transaction_buffers);
        if (s->stats->ts_tid == 0)
                return 0;
        seq_printf(seq, "average: \n  %ums waiting for transaction\n",
-           jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid));
+           jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
        seq_printf(seq, "  %ums running transaction\n",
-           jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid));
+           jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
        seq_printf(seq, "  %ums transaction was being locked\n",
-           jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid));
+           jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
        seq_printf(seq, "  %ums flushing data (in ordered mode)\n",
-           jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid));
+           jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
        seq_printf(seq, "  %ums logging transaction\n",
-           jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid));
+           jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
        seq_printf(seq, "  %lluus average transaction commit time\n",
                   div_u64(s->journal->j_average_commit_time, 1000));
        seq_printf(seq, "  %lu handles per transaction\n",
-           s->stats->u.run.rs_handle_count / s->stats->ts_tid);
+           s->stats->run.rs_handle_count / s->stats->ts_tid);
        seq_printf(seq, "  %lu blocks per transaction\n",
-           s->stats->u.run.rs_blocks / s->stats->ts_tid);
+           s->stats->run.rs_blocks / s->stats->ts_tid);
        seq_printf(seq, "  %lu logged blocks per transaction\n",
-           s->stats->u.run.rs_blocks_logged / s->stats->ts_tid);
+           s->stats->run.rs_blocks_logged / s->stats->ts_tid);
        return 0;
 }
 
@@ -866,7 +722,7 @@ static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
 {
 }
 
-static struct seq_operations jbd2_seq_info_ops = {
+static const struct seq_operations jbd2_seq_info_ops = {
        .start  = jbd2_seq_info_start,
        .next   = jbd2_seq_info_next,
        .stop   = jbd2_seq_info_stop,
@@ -914,7 +770,7 @@ static int jbd2_seq_info_release(struct inode *inode, struct file *file)
        return seq_release(inode, file);
 }
 
-static struct file_operations jbd2_seq_info_fops = {
+static const struct file_operations jbd2_seq_info_fops = {
        .owner          = THIS_MODULE,
        .open           = jbd2_seq_info_open,
        .read           = seq_read,
@@ -928,8 +784,6 @@ static void jbd2_stats_proc_init(journal_t *journal)
 {
        journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
        if (journal->j_proc_entry) {
-               proc_create_data("history", S_IRUGO, journal->j_proc_entry,
-                                &jbd2_seq_history_fops, journal);
                proc_create_data("info", S_IRUGO, journal->j_proc_entry,
                                 &jbd2_seq_info_fops, journal);
        }
@@ -938,27 +792,9 @@ static void jbd2_stats_proc_init(journal_t *journal)
 static void jbd2_stats_proc_exit(journal_t *journal)
 {
        remove_proc_entry("info", journal->j_proc_entry);
-       remove_proc_entry("history", journal->j_proc_entry);
        remove_proc_entry(journal->j_devname, proc_jbd2_stats);
 }
 
-static void journal_init_stats(journal_t *journal)
-{
-       int size;
-
-       if (!proc_jbd2_stats)
-               return;
-
-       journal->j_history_max = 100;
-       size = sizeof(struct transaction_stats_s) * journal->j_history_max;
-       journal->j_history = kzalloc(size, GFP_KERNEL);
-       if (!journal->j_history) {
-               journal->j_history_max = 0;
-               return;
-       }
-       spin_lock_init(&journal->j_history_lock);
-}
-
 /*
  * Management for journal control blocks: functions to create and
  * destroy journal_t structures, and to initialise and read existing
@@ -1003,7 +839,7 @@ static journal_t * journal_init_common (void)
                goto fail;
        }
 
-       journal_init_stats(journal);
+       spin_lock_init(&journal->j_history_lock);
 
        return journal;
 fail:
@@ -1077,6 +913,7 @@ journal_t * jbd2_journal_init_dev(struct block_device *bdev,
 
        return journal;
 out_err:
+       kfree(journal->j_wbuf);
        jbd2_stats_proc_exit(journal);
        kfree(journal);
        return NULL;
@@ -1109,7 +946,7 @@ journal_t * jbd2_journal_init_inode (struct inode *inode)
        while ((p = strchr(p, '/')))
                *p = '!';
        p = journal->j_devname + strlen(journal->j_devname);
-       sprintf(p, ":%lu", journal->j_inode->i_ino);
+       sprintf(p, "-%lu", journal->j_inode->i_ino);
        jbd_debug(1,
                  "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
                  journal, inode->i_sb->s_id, inode->i_ino,
@@ -1150,6 +987,7 @@ journal_t * jbd2_journal_init_inode (struct inode *inode)
 
        return journal;
 out_err:
+       kfree(journal->j_wbuf);
        jbd2_stats_proc_exit(journal);
        kfree(journal);
        return NULL;
@@ -1181,6 +1019,12 @@ static int journal_reset(journal_t *journal)
 
        first = be32_to_cpu(sb->s_first);
        last = be32_to_cpu(sb->s_maxlen);
+       if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
+               printk(KERN_ERR "JBD: Journal too short (blocks %llu-%llu).\n",
+                      first, last);
+               journal_fail_superblock(journal);
+               return -EINVAL;
+       }
 
        journal->j_first = first;
        journal->j_last = last;
@@ -1781,7 +1625,7 @@ int jbd2_journal_wipe(journal_t *journal, int write)
  * Journal abort has very specific semantics, which we describe
  * for journal abort.
  *
- * Two internal function, which provide abort to te jbd layer
+ * Two internal functions, which provide abort to the jbd layer
  * itself are here.
  */
 
@@ -1879,7 +1723,7 @@ void jbd2_journal_abort(journal_t *journal, int errno)
  * int jbd2_journal_errno () - returns the journal's error state.
  * @journal: journal to examine.
  *
- * This is the errno numbet set with jbd2_journal_abort(), the last
+ * This is the errno number set with jbd2_journal_abort(), the last
  * time the journal was mounted - if the journal was stopped
  * without calling abort this will be 0.
  *
@@ -1903,7 +1747,7 @@ int jbd2_journal_errno(journal_t *journal)
  * int jbd2_journal_clear_err () - clears the journal's error state
  * @journal: journal to act on.
  *
- * An error must be cleared or Acked to take a FS out of readonly
+ * An error must be cleared or acked to take a FS out of readonly
  * mode.
  */
 int jbd2_journal_clear_err(journal_t *journal)
@@ -1923,7 +1767,7 @@ int jbd2_journal_clear_err(journal_t *journal)
  * void jbd2_journal_ack_err() - Ack journal err.
  * @journal: journal to act on.
  *
- * An error must be cleared or Acked to take a FS out of readonly
+ * An error must be cleared or acked to take a FS out of readonly
  * mode.
  */
 void jbd2_journal_ack_err(journal_t *journal)
@@ -2377,6 +2221,72 @@ static void __exit journal_exit(void)
        jbd2_journal_destroy_caches();
 }
 
+/* 
+ * jbd2_dev_to_name is a utility function used by the jbd2 and ext4 
+ * tracing infrastructure to map a dev_t to a device name.
+ *
+ * The caller should use rcu_read_lock() in order to make sure the
+ * device name stays valid until its done with it.  We use
+ * rcu_read_lock() as well to make sure we're safe in case the caller
+ * gets sloppy, and because rcu_read_lock() is cheap and can be safely
+ * nested.
+ */
+struct devname_cache {
+       struct rcu_head rcu;
+       dev_t           device;
+       char            devname[BDEVNAME_SIZE];
+};
+#define CACHE_SIZE_BITS 6
+static struct devname_cache *devcache[1 << CACHE_SIZE_BITS];
+static DEFINE_SPINLOCK(devname_cache_lock);
+
+static void free_devcache(struct rcu_head *rcu)
+{
+       kfree(rcu);
+}
+
+const char *jbd2_dev_to_name(dev_t device)
+{
+       int     i = hash_32(device, CACHE_SIZE_BITS);
+       char    *ret;
+       struct block_device *bd;
+       static struct devname_cache *new_dev;
+
+       rcu_read_lock();
+       if (devcache[i] && devcache[i]->device == device) {
+               ret = devcache[i]->devname;
+               rcu_read_unlock();
+               return ret;
+       }
+       rcu_read_unlock();
+
+       new_dev = kmalloc(sizeof(struct devname_cache), GFP_KERNEL);
+       if (!new_dev)
+               return "NODEV-ALLOCFAILURE"; /* Something non-NULL */
+       spin_lock(&devname_cache_lock);
+       if (devcache[i]) {
+               if (devcache[i]->device == device) {
+                       kfree(new_dev);
+                       ret = devcache[i]->devname;
+                       spin_unlock(&devname_cache_lock);
+                       return ret;
+               }
+               call_rcu(&devcache[i]->rcu, free_devcache);
+       }
+       devcache[i] = new_dev;
+       devcache[i]->device = device;
+       bd = bdget(device);
+       if (bd) {
+               bdevname(bd, devcache[i]->devname);
+               bdput(bd);
+       } else
+               __bdevname(device, devcache[i]->devname);
+       ret = devcache[i]->devname;
+       spin_unlock(&devname_cache_lock);
+       return ret;
+}
+EXPORT_SYMBOL(jbd2_dev_to_name);
+
 MODULE_LICENSE("GPL");
 module_init(journal_init);
 module_exit(journal_exit);