JFS: Free sbi memory in error path
[safe/jmp/linux-2.6] / fs / ubifs / super.c
index 336073e..4d2f215 100644 (file)
@@ -283,7 +283,7 @@ static void ubifs_destroy_inode(struct inode *inode)
 /*
  * Note, Linux write-back code calls this without 'i_mutex'.
  */
-static int ubifs_write_inode(struct inode *inode, int wait)
+static int ubifs_write_inode(struct inode *inode, struct writeback_control *wbc)
 {
        int err = 0;
        struct ubifs_info *c = inode->i_sb->s_fs_info;
@@ -317,6 +317,8 @@ static int ubifs_write_inode(struct inode *inode, int wait)
                if (err)
                        ubifs_err("can't write inode %lu, error %d",
                                  inode->i_ino, err);
+               else
+                       err = dbg_check_inode_size(c, inode, ui->ui_size);
        }
 
        ui->dirty = 0;
@@ -360,6 +362,11 @@ static void ubifs_delete_inode(struct inode *inode)
 out:
        if (ui->dirty)
                ubifs_release_dirty_inode_budget(c, ui);
+       else {
+               /* We've deleted something - clean the "no space" flags */
+               c->nospace = c->nospace_rp = 0;
+               smp_wmb();
+       }
        clear_inode(inode);
 }
 
@@ -421,8 +428,8 @@ static int ubifs_show_options(struct seq_file *s, struct vfsmount *mnt)
                seq_printf(s, ",no_chk_data_crc");
 
        if (c->mount_opts.override_compr) {
-               seq_printf(s, ",compr=");
-               seq_printf(s, ubifs_compr_name(c->mount_opts.compr_type));
+               seq_printf(s, ",compr=%s",
+                          ubifs_compr_name(c->mount_opts.compr_type));
        }
 
        return 0;
@@ -432,12 +439,6 @@ static int ubifs_sync_fs(struct super_block *sb, int wait)
 {
        int i, err;
        struct ubifs_info *c = sb->s_fs_info;
-       struct writeback_control wbc = {
-               .sync_mode   = WB_SYNC_ALL,
-               .range_start = 0,
-               .range_end   = LLONG_MAX,
-               .nr_to_write = LONG_MAX,
-       };
 
        /*
         * Zero @wait is just an advisory thing to help the file system shove
@@ -447,9 +448,6 @@ static int ubifs_sync_fs(struct super_block *sb, int wait)
        if (!wait)
                return 0;
 
-       if (sb->s_flags & MS_RDONLY)
-               return 0;
-
        /*
         * Synchronize write buffers, because 'ubifs_run_commit()' does not
         * do this if it waits for an already running commit.
@@ -461,16 +459,12 @@ static int ubifs_sync_fs(struct super_block *sb, int wait)
        }
 
        /*
-        * VFS calls '->sync_fs()' before synchronizing all dirty inodes and
-        * pages, so synchronize them first, then commit the journal. Strictly
-        * speaking, it is not necessary to commit the journal here,
+        * Strictly speaking, it is not necessary to commit the journal here,
         * synchronizing write-buffers would be enough. But committing makes
         * UBIFS free space predictions much more accurate, so we want to let
         * the user be able to get more accurate results of 'statfs()' after
         * they synchronize the file system.
         */
-       generic_sync_sb_inodes(sb, &wbc);
-
        err = ubifs_run_commit(c);
        if (err)
                return err;
@@ -700,6 +694,8 @@ static int init_constants_sb(struct ubifs_info *c)
        if (err)
                return err;
 
+       /* Initialize effective LEB size used in budgeting calculations */
+       c->idx_leb_size = c->leb_size - c->max_idx_node_sz;
        return 0;
 }
 
@@ -716,6 +712,7 @@ static void init_constants_master(struct ubifs_info *c)
        long long tmp64;
 
        c->min_idx_lebs = ubifs_calc_min_idx_lebs(c);
+       c->report_rp_size = ubifs_reported_space(c, c->rp_size);
 
        /*
         * Calculate total amount of FS blocks. This number is not used
@@ -791,7 +788,7 @@ static int alloc_wbufs(struct ubifs_info *c)
         * does not need to be synchronized by timer.
         */
        c->jheads[GCHD].wbuf.dtype = UBI_LONGTERM;
-       c->jheads[GCHD].wbuf.timeout = 0;
+       c->jheads[GCHD].wbuf.no_timer = 1;
 
        return 0;
 }
@@ -932,6 +929,27 @@ static const match_table_t tokens = {
 };
 
 /**
+ * parse_standard_option - parse a standard mount option.
+ * @option: the option to parse
+ *
+ * Normally, standard mount options like "sync" are passed to file-systems as
+ * flags. However, when a "rootflags=" kernel boot parameter is used, they may
+ * be present in the options string. This function tries to deal with this
+ * situation and parse standard options. Returns 0 if the option was not
+ * recognized, and the corresponding integer flag if it was.
+ *
+ * UBIFS is only interested in the "sync" option, so do not check for anything
+ * else.
+ */
+static int parse_standard_option(const char *option)
+{
+       ubifs_msg("parse %s", option);
+       if (!strcmp(option, "sync"))
+               return MS_SYNCHRONOUS;
+       return 0;
+}
+
+/**
  * ubifs_parse_options - parse mount parameters.
  * @c: UBIFS file-system description object
  * @options: parameters to parse
@@ -957,13 +975,16 @@ static int ubifs_parse_options(struct ubifs_info *c, char *options,
 
                token = match_token(p, tokens, args);
                switch (token) {
+               /*
+                * %Opt_fast_unmount and %Opt_norm_unmount options are ignored.
+                * We accept them in order to be backward-compatible. But this
+                * should be removed at some point.
+                */
                case Opt_fast_unmount:
                        c->mount_opts.unmount_mode = 2;
-                       c->fast_unmount = 1;
                        break;
                case Opt_norm_unmount:
                        c->mount_opts.unmount_mode = 1;
-                       c->fast_unmount = 0;
                        break;
                case Opt_bulk_read:
                        c->mount_opts.bulk_read = 2;
@@ -1004,9 +1025,19 @@ static int ubifs_parse_options(struct ubifs_info *c, char *options,
                        break;
                }
                default:
-                       ubifs_err("unrecognized mount option \"%s\" "
-                                 "or missing value", p);
-                       return -EINVAL;
+               {
+                       unsigned long flag;
+                       struct super_block *sb = c->vfs_sb;
+
+                       flag = parse_standard_option(p);
+                       if (!flag) {
+                               ubifs_err("unrecognized mount option \"%s\" "
+                                         "or missing value", p);
+                               return -EINVAL;
+                       }
+                       sb->s_flags |= flag;
+                       break;
+               }
                }
        }
 
@@ -1085,12 +1116,7 @@ static int check_free_space(struct ubifs_info *c)
                ubifs_err("insufficient free space to mount in read/write mode");
                dbg_dump_budg(c);
                dbg_dump_lprops(c);
-               /*
-                * We return %-EINVAL instead of %-ENOSPC because it seems to
-                * be the closest error code mentioned in the mount function
-                * documentation.
-                */
-               return -EINVAL;
+               return -ENOSPC;
        }
        return 0;
 }
@@ -1181,6 +1207,7 @@ static int mount_ubifs(struct ubifs_info *c)
        if (!ubifs_compr_present(c->default_compr)) {
                ubifs_err("'compressor \"%s\" is not compiled in",
                          ubifs_compr_name(c->default_compr));
+               err = -ENOTSUPP;
                goto out_free;
        }
 
@@ -1203,7 +1230,7 @@ static int mount_ubifs(struct ubifs_info *c)
                        goto out_cbuf;
 
                /* Create background thread */
-               c->bgt = kthread_create(ubifs_bg_thread, c, c->bgt_name);
+               c->bgt = kthread_create(ubifs_bg_thread, c, "%s", c->bgt_name);
                if (IS_ERR(c->bgt)) {
                        err = PTR_ERR(c->bgt);
                        c->bgt = NULL;
@@ -1251,6 +1278,9 @@ static int mount_ubifs(struct ubifs_info *c)
        if (err)
                goto out_journal;
 
+       /* Calculate 'min_idx_lebs' after journal replay */
+       c->min_idx_lebs = ubifs_calc_min_idx_lebs(c);
+
        err = ubifs_mount_orphans(c, c->need_recovery, mounted_read_only);
        if (err)
                goto out_orphans;
@@ -1320,20 +1350,25 @@ static int mount_ubifs(struct ubifs_info *c)
                else {
                        c->need_recovery = 0;
                        ubifs_msg("recovery completed");
+                       /*
+                        * GC LEB has to be empty and taken at this point. But
+                        * the journal head LEBs may also be accounted as
+                        * "empty taken" if they are empty.
+                        */
+                       ubifs_assert(c->lst.taken_empty_lebs > 0);
                }
-       }
+       } else
+               ubifs_assert(c->lst.taken_empty_lebs > 0);
 
-       err = dbg_debugfs_init_fs(c);
+       err = dbg_check_filesystem(c);
        if (err)
                goto out_infos;
 
-       err = dbg_check_filesystem(c);
+       err = dbg_debugfs_init_fs(c);
        if (err)
                goto out_infos;
 
        c->always_chk_crc = 0;
-       /* GC LEB has to be empty and taken at this point */
-       ubifs_assert(c->lst.taken_empty_lebs == 1);
 
        ubifs_msg("mounted UBI device %d, volume %d, name \"%s\"",
                  c->vi.ubi_num, c->vi.vol_id, c->vi.name);
@@ -1345,8 +1380,9 @@ static int mount_ubifs(struct ubifs_info *c)
        x = (long long)c->log_lebs * c->leb_size + c->max_bud_bytes;
        ubifs_msg("journal size:       %lld bytes (%lld KiB, %lld MiB, %d "
                  "LEBs)", x, x >> 10, x >> 20, c->log_lebs + c->max_bud_cnt);
-       ubifs_msg("media format:       %d (latest is %d)",
-                 c->fmt_version, UBIFS_FORMAT_VERSION);
+       ubifs_msg("media format:       w%d/r%d (latest is w%d/r%d)",
+                 c->fmt_version, c->ro_compat_version,
+                 UBIFS_FORMAT_VERSION, UBIFS_RO_COMPAT_VERSION);
        ubifs_msg("default compressor: %s", ubifs_compr_name(c->default_compr));
        ubifs_msg("reserved for root:  %llu bytes (%llu KiB)",
                c->report_rp_size, c->report_rp_size >> 10);
@@ -1357,13 +1393,7 @@ static int mount_ubifs(struct ubifs_info *c)
                c->leb_size, c->leb_size >> 10);
        dbg_msg("data journal heads:  %d",
                c->jhead_cnt - NONDATA_JHEADS_CNT);
-       dbg_msg("UUID:                %02X%02X%02X%02X-%02X%02X"
-              "-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
-              c->uuid[0], c->uuid[1], c->uuid[2], c->uuid[3],
-              c->uuid[4], c->uuid[5], c->uuid[6], c->uuid[7],
-              c->uuid[8], c->uuid[9], c->uuid[10], c->uuid[11],
-              c->uuid[12], c->uuid[13], c->uuid[14], c->uuid[15]);
-       dbg_msg("fast unmount:        %d", c->fast_unmount);
+       dbg_msg("UUID:                %pUB", c->uuid);
        dbg_msg("big_lpt              %d", c->big_lpt);
        dbg_msg("log LEBs:            %d (%d - %d)",
                c->log_lebs, UBIFS_LOG_LNUM, c->log_last);
@@ -1487,6 +1517,15 @@ static int ubifs_remount_rw(struct ubifs_info *c)
 {
        int err, lnum;
 
+       if (c->rw_incompat) {
+               ubifs_err("the file-system is not R/W-compatible");
+               ubifs_msg("on-flash format version is w%d/r%d, but software "
+                         "only supports up to version w%d/r%d", c->fmt_version,
+                         c->ro_compat_version, UBIFS_FORMAT_VERSION,
+                         UBIFS_RO_COMPAT_VERSION);
+               return -EROFS;
+       }
+
        mutex_lock(&c->umount_mutex);
        dbg_save_space_info(c);
        c->remounting_rw = 1;
@@ -1524,6 +1563,12 @@ static int ubifs_remount_rw(struct ubifs_info *c)
                err = ubifs_recover_inl_heads(c, c->sbuf);
                if (err)
                        goto out;
+       } else {
+               /* A readonly mount is not allowed to have orphans */
+               ubifs_assert(c->tot_orphans == 0);
+               err = ubifs_clear_orphans(c);
+               if (err)
+                       goto out;
        }
 
        if (!(c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY))) {
@@ -1550,7 +1595,7 @@ static int ubifs_remount_rw(struct ubifs_info *c)
        ubifs_create_buds_lists(c);
 
        /* Create background thread */
-       c->bgt = kthread_create(ubifs_bg_thread, c, c->bgt_name);
+       c->bgt = kthread_create(ubifs_bg_thread, c, "%s", c->bgt_name);
        if (IS_ERR(c->bgt)) {
                err = PTR_ERR(c->bgt);
                c->bgt = NULL;
@@ -1614,38 +1659,6 @@ out:
 }
 
 /**
- * commit_on_unmount - commit the journal when un-mounting.
- * @c: UBIFS file-system description object
- *
- * This function is called during un-mounting and re-mounting, and it commits
- * the journal unless the "fast unmount" mode is enabled.
- */
-static void commit_on_unmount(struct ubifs_info *c)
-{
-       long long bud_bytes;
-
-       if (!c->fast_unmount) {
-               dbg_gen("skip committing - fast unmount enabled");
-               return;
-       }
-
-       /*
-        * This function is called before the background thread is stopped, so
-        * we may race with ongoing commit, which means we have to take
-        * @c->bud_lock to access @c->bud_bytes.
-        */
-       spin_lock(&c->buds_lock);
-       bud_bytes = c->bud_bytes;
-       spin_unlock(&c->buds_lock);
-
-       if (bud_bytes) {
-               dbg_gen("run commit");
-               ubifs_run_commit(c);
-       } else
-               dbg_gen("journal is empty, do not run commit");
-}
-
-/**
  * ubifs_remount_ro - re-mount in read-only mode.
  * @c: UBIFS file-system description object
  *
@@ -1657,9 +1670,8 @@ static void ubifs_remount_ro(struct ubifs_info *c)
        int i, err;
 
        ubifs_assert(!c->need_recovery);
-       ubifs_assert(!c->ro_media);
+       ubifs_assert(!(c->vfs_sb->s_flags & MS_RDONLY));
 
-       commit_on_unmount(c);
        mutex_lock(&c->umount_mutex);
        if (c->bgt) {
                kthread_stop(c->bgt);
@@ -1670,7 +1682,7 @@ static void ubifs_remount_ro(struct ubifs_info *c)
 
        for (i = 0; i < c->jhead_cnt; i++) {
                ubifs_wbuf_sync(&c->jheads[i].wbuf);
-               del_timer_sync(&c->jheads[i].wbuf.timer);
+               hrtimer_cancel(&c->jheads[i].wbuf.timer);
        }
 
        c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_DIRTY);
@@ -1680,10 +1692,6 @@ static void ubifs_remount_ro(struct ubifs_info *c)
        if (err)
                ubifs_ro_mode(c, err);
 
-       err = ubifs_destroy_idx_gc(c);
-       if (err)
-               ubifs_ro_mode(c, err);
-
        free_wbufs(c);
        vfree(c->orph_buf);
        c->orph_buf = NULL;
@@ -1703,6 +1711,7 @@ static void ubifs_put_super(struct super_block *sb)
 
        ubifs_msg("un-mount UBI device %d, volume %d", c->vi.ubi_num,
                  c->vi.vol_id);
+
        /*
         * The following asserts are only valid if there has not been a failure
         * of the media. For example, there will be dirty inodes if we failed
@@ -1732,10 +1741,8 @@ static void ubifs_put_super(struct super_block *sb)
 
                /* Synchronize write-buffers */
                if (c->jheads)
-                       for (i = 0; i < c->jhead_cnt; i++) {
+                       for (i = 0; i < c->jhead_cnt; i++)
                                ubifs_wbuf_sync(&c->jheads[i].wbuf);
-                               del_timer_sync(&c->jheads[i].wbuf.timer);
-                       }
 
                /*
                 * On fatal errors c->ro_media is set to 1, in which case we do
@@ -1786,15 +1793,19 @@ static int ubifs_remount_fs(struct super_block *sb, int *flags, char *data)
 
        if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
                if (c->ro_media) {
-                       ubifs_msg("cannot re-mount R/W, UBIFS is working in "
-                                 "R/O mode");
-                       return -EINVAL;
+                       ubifs_msg("cannot re-mount due to prior errors");
+                       return -EROFS;
                }
                err = ubifs_remount_rw(c);
                if (err)
                        return err;
-       } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY))
+       } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY)) {
+               if (c->ro_media) {
+                       ubifs_msg("cannot re-mount due to prior errors");
+                       return -EROFS;
+               }
                ubifs_remount_ro(c);
+       }
 
        if (c->bulk_read == 1)
                bu_init(c);
@@ -1804,7 +1815,7 @@ static int ubifs_remount_fs(struct super_block *sb, int *flags, char *data)
                c->bu.buf = NULL;
        }
 
-       ubifs_assert(c->lst.taken_empty_lebs == 1);
+       ubifs_assert(c->lst.taken_empty_lebs > 0);
        return 0;
 }
 
@@ -1826,22 +1837,32 @@ const struct super_operations ubifs_super_operations = {
  * @name: UBI volume name
  * @mode: UBI volume open mode
  *
- * There are several ways to specify UBI volumes when mounting UBIFS:
- * o ubiX_Y    - UBI device number X, volume Y;
- * o ubiY      - UBI device number 0, volume Y;
+ * The primary method of mounting UBIFS is by specifying the UBI volume
+ * character device node path. However, UBIFS may also be mounted withoug any
+ * character device node using one of the following methods:
+ *
+ * o ubiX_Y    - mount UBI device number X, volume Y;
+ * o ubiY      - mount UBI device number 0, volume Y;
  * o ubiX:NAME - mount UBI device X, volume with name NAME;
  * o ubi:NAME  - mount UBI device 0, volume with name NAME.
  *
  * Alternative '!' separator may be used instead of ':' (because some shells
  * like busybox may interpret ':' as an NFS host name separator). This function
- * returns ubi volume object in case of success and a negative error code in
- * case of failure.
+ * returns UBI volume description object in case of success and a negative
+ * error code in case of failure.
  */
 static struct ubi_volume_desc *open_ubi(const char *name, int mode)
 {
+       struct ubi_volume_desc *ubi;
        int dev, vol;
        char *endptr;
 
+       /* First, try to open using the device node path method */
+       ubi = ubi_open_volume_path(name, mode);
+       if (!IS_ERR(ubi))
+               return ubi;
+
+       /* Try the "nodev" method */
        if (name[0] != 'u' || name[1] != 'b' || name[2] != 'i')
                return ERR_PTR(-EINVAL);
 
@@ -1914,6 +1935,7 @@ static int ubifs_fill_super(struct super_block *sb, void *data, int silent)
        INIT_LIST_HEAD(&c->orph_list);
        INIT_LIST_HEAD(&c->orph_new);
 
+       c->vfs_sb = sb;
        c->highest_inum = UBIFS_FIRST_INO;
        c->lhead_lnum = c->ltail_lnum = UBIFS_LOG_LNUM;
 
@@ -1935,23 +1957,26 @@ static int ubifs_fill_super(struct super_block *sb, void *data, int silent)
         *
         * Read-ahead will be disabled because @c->bdi.ra_pages is 0.
         */
+       c->bdi.name = "ubifs",
        c->bdi.capabilities = BDI_CAP_MAP_COPY;
        c->bdi.unplug_io_fn = default_unplug_io_fn;
        err  = bdi_init(&c->bdi);
        if (err)
                goto out_close;
+       err = bdi_register(&c->bdi, NULL, "ubifs_%d_%d",
+                          c->vi.ubi_num, c->vi.vol_id);
+       if (err)
+               goto out_bdi;
 
        err = ubifs_parse_options(c, data, 0);
        if (err)
                goto out_bdi;
 
-       c->vfs_sb = sb;
-
+       sb->s_bdi = &c->bdi;
        sb->s_fs_info = c;
        sb->s_magic = UBIFS_SUPER_MAGIC;
        sb->s_blocksize = UBIFS_BLOCK_SIZE;
        sb->s_blocksize_bits = UBIFS_BLOCK_SHIFT;
-       sb->s_dev = c->vi.cdev;
        sb->s_maxbytes = c->max_inode_sz = key_max_inode_size(c);
        if (c->max_inode_sz > MAX_LFS_FILESIZE)
                sb->s_maxbytes = c->max_inode_sz = MAX_LFS_FILESIZE;
@@ -1996,16 +2021,9 @@ out_free:
 static int sb_test(struct super_block *sb, void *data)
 {
        dev_t *dev = data;
+       struct ubifs_info *c = sb->s_fs_info;
 
-       return sb->s_dev == *dev;
-}
-
-static int sb_set(struct super_block *sb, void *data)
-{
-       dev_t *dev = data;
-
-       sb->s_dev = *dev;
-       return 0;
+       return c->vi.cdev == *dev;
 }
 
 static int ubifs_get_sb(struct file_system_type *fs_type, int flags,
@@ -2033,7 +2051,7 @@ static int ubifs_get_sb(struct file_system_type *fs_type, int flags,
 
        dbg_gen("opened ubi%d_%d", vi.ubi_num, vi.vol_id);
 
-       sb = sget(fs_type, &sb_test, &sb_set, &vi.cdev);
+       sb = sget(fs_type, &sb_test, &set_anon_super, &vi.cdev);
        if (IS_ERR(sb)) {
                err = PTR_ERR(sb);
                goto out_close;
@@ -2063,35 +2081,21 @@ static int ubifs_get_sb(struct file_system_type *fs_type, int flags,
        /* 'fill_super()' opens ubi again so we must close it here */
        ubi_close_volume(ubi);
 
-       return simple_set_mnt(mnt, sb);
+       simple_set_mnt(mnt, sb);
+       return 0;
 
 out_deact:
-       up_write(&sb->s_umount);
-       deactivate_super(sb);
+       deactivate_locked_super(sb);
 out_close:
        ubi_close_volume(ubi);
        return err;
 }
 
-static void ubifs_kill_sb(struct super_block *sb)
-{
-       struct ubifs_info *c = sb->s_fs_info;
-
-       /*
-        * We do 'commit_on_unmount()' here instead of 'ubifs_put_super()'
-        * in order to be outside BKL.
-        */
-       if (sb->s_root && !(sb->s_flags & MS_RDONLY))
-               commit_on_unmount(c);
-       /* The un-mount routine is actually done in put_super() */
-       generic_shutdown_super(sb);
-}
-
 static struct file_system_type ubifs_fs_type = {
        .name    = "ubifs",
        .owner   = THIS_MODULE,
        .get_sb  = ubifs_get_sb,
-       .kill_sb = ubifs_kill_sb
+       .kill_sb = kill_anon_super,
 };
 
 /*