jffs2: remove lock_super
[linux-3.10.git] / fs / jffs2 / super.c
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright © 2001-2007 Red Hat, Inc.
5  *
6  * Created by David Woodhouse <dwmw2@infradead.org>
7  *
8  * For licensing information, see the file 'LICENCE' in this directory.
9  *
10  */
11
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/init.h>
18 #include <linux/list.h>
19 #include <linux/fs.h>
20 #include <linux/err.h>
21 #include <linux/mount.h>
22 #include <linux/parser.h>
23 #include <linux/jffs2.h>
24 #include <linux/pagemap.h>
25 #include <linux/mtd/super.h>
26 #include <linux/ctype.h>
27 #include <linux/namei.h>
28 #include <linux/seq_file.h>
29 #include <linux/exportfs.h>
30 #include "compr.h"
31 #include "nodelist.h"
32
33 static void jffs2_put_super(struct super_block *);
34
35 static struct kmem_cache *jffs2_inode_cachep;
36
37 static struct inode *jffs2_alloc_inode(struct super_block *sb)
38 {
39         struct jffs2_inode_info *f;
40
41         f = kmem_cache_alloc(jffs2_inode_cachep, GFP_KERNEL);
42         if (!f)
43                 return NULL;
44         return &f->vfs_inode;
45 }
46
47 static void jffs2_i_callback(struct rcu_head *head)
48 {
49         struct inode *inode = container_of(head, struct inode, i_rcu);
50         kmem_cache_free(jffs2_inode_cachep, JFFS2_INODE_INFO(inode));
51 }
52
53 static void jffs2_destroy_inode(struct inode *inode)
54 {
55         call_rcu(&inode->i_rcu, jffs2_i_callback);
56 }
57
58 static void jffs2_i_init_once(void *foo)
59 {
60         struct jffs2_inode_info *f = foo;
61
62         mutex_init(&f->sem);
63         inode_init_once(&f->vfs_inode);
64 }
65
66 static void jffs2_write_super(struct super_block *sb)
67 {
68         struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
69
70         sb->s_dirt = 0;
71
72         if (!(sb->s_flags & MS_RDONLY)) {
73                 jffs2_dbg(1, "%s()\n", __func__);
74                 jffs2_flush_wbuf_gc(c, 0);
75         }
76 }
77
78 static const char *jffs2_compr_name(unsigned int compr)
79 {
80         switch (compr) {
81         case JFFS2_COMPR_MODE_NONE:
82                 return "none";
83 #ifdef CONFIG_JFFS2_LZO
84         case JFFS2_COMPR_MODE_FORCELZO:
85                 return "lzo";
86 #endif
87 #ifdef CONFIG_JFFS2_ZLIB
88         case JFFS2_COMPR_MODE_FORCEZLIB:
89                 return "zlib";
90 #endif
91         default:
92                 /* should never happen; programmer error */
93                 WARN_ON(1);
94                 return "";
95         }
96 }
97
98 static int jffs2_show_options(struct seq_file *s, struct dentry *root)
99 {
100         struct jffs2_sb_info *c = JFFS2_SB_INFO(root->d_sb);
101         struct jffs2_mount_opts *opts = &c->mount_opts;
102
103         if (opts->override_compr)
104                 seq_printf(s, ",compr=%s", jffs2_compr_name(opts->compr));
105
106         return 0;
107 }
108
109 static int jffs2_sync_fs(struct super_block *sb, int wait)
110 {
111         struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
112
113         jffs2_write_super(sb);
114
115         mutex_lock(&c->alloc_sem);
116         jffs2_flush_wbuf_pad(c);
117         mutex_unlock(&c->alloc_sem);
118         return 0;
119 }
120
121 static struct inode *jffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
122                                          uint32_t generation)
123 {
124         /* We don't care about i_generation. We'll destroy the flash
125            before we start re-using inode numbers anyway. And even
126            if that wasn't true, we'd have other problems...*/
127         return jffs2_iget(sb, ino);
128 }
129
130 static struct dentry *jffs2_fh_to_dentry(struct super_block *sb, struct fid *fid,
131                                          int fh_len, int fh_type)
132 {
133         return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
134                                     jffs2_nfs_get_inode);
135 }
136
137 static struct dentry *jffs2_fh_to_parent(struct super_block *sb, struct fid *fid,
138                                          int fh_len, int fh_type)
139 {
140         return generic_fh_to_parent(sb, fid, fh_len, fh_type,
141                                     jffs2_nfs_get_inode);
142 }
143
144 static struct dentry *jffs2_get_parent(struct dentry *child)
145 {
146         struct jffs2_inode_info *f;
147         uint32_t pino;
148
149         BUG_ON(!S_ISDIR(child->d_inode->i_mode));
150
151         f = JFFS2_INODE_INFO(child->d_inode);
152
153         pino = f->inocache->pino_nlink;
154
155         JFFS2_DEBUG("Parent of directory ino #%u is #%u\n",
156                     f->inocache->ino, pino);
157
158         return d_obtain_alias(jffs2_iget(child->d_inode->i_sb, pino));
159 }
160
161 static const struct export_operations jffs2_export_ops = {
162         .get_parent = jffs2_get_parent,
163         .fh_to_dentry = jffs2_fh_to_dentry,
164         .fh_to_parent = jffs2_fh_to_parent,
165 };
166
167 /*
168  * JFFS2 mount options.
169  *
170  * Opt_override_compr: override default compressor
171  * Opt_err: just end of array marker
172  */
173 enum {
174         Opt_override_compr,
175         Opt_err,
176 };
177
178 static const match_table_t tokens = {
179         {Opt_override_compr, "compr=%s"},
180         {Opt_err, NULL},
181 };
182
183 static int jffs2_parse_options(struct jffs2_sb_info *c, char *data)
184 {
185         substring_t args[MAX_OPT_ARGS];
186         char *p, *name;
187
188         if (!data)
189                 return 0;
190
191         while ((p = strsep(&data, ","))) {
192                 int token;
193
194                 if (!*p)
195                         continue;
196
197                 token = match_token(p, tokens, args);
198                 switch (token) {
199                 case Opt_override_compr:
200                         name = match_strdup(&args[0]);
201
202                         if (!name)
203                                 return -ENOMEM;
204                         if (!strcmp(name, "none"))
205                                 c->mount_opts.compr = JFFS2_COMPR_MODE_NONE;
206 #ifdef CONFIG_JFFS2_LZO
207                         else if (!strcmp(name, "lzo"))
208                                 c->mount_opts.compr = JFFS2_COMPR_MODE_FORCELZO;
209 #endif
210 #ifdef CONFIG_JFFS2_ZLIB
211                         else if (!strcmp(name, "zlib"))
212                                 c->mount_opts.compr =
213                                                 JFFS2_COMPR_MODE_FORCEZLIB;
214 #endif
215                         else {
216                                 pr_err("Error: unknown compressor \"%s\"\n",
217                                        name);
218                                 kfree(name);
219                                 return -EINVAL;
220                         }
221                         kfree(name);
222                         c->mount_opts.override_compr = true;
223                         break;
224                 default:
225                         pr_err("Error: unrecognized mount option '%s' or missing value\n",
226                                p);
227                         return -EINVAL;
228                 }
229         }
230
231         return 0;
232 }
233
234 static int jffs2_remount_fs(struct super_block *sb, int *flags, char *data)
235 {
236         struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
237         int err;
238
239         err = jffs2_parse_options(c, data);
240         if (err)
241                 return -EINVAL;
242
243         return jffs2_do_remount_fs(sb, flags, data);
244 }
245
246 static const struct super_operations jffs2_super_operations =
247 {
248         .alloc_inode =  jffs2_alloc_inode,
249         .destroy_inode =jffs2_destroy_inode,
250         .put_super =    jffs2_put_super,
251         .write_super =  jffs2_write_super,
252         .statfs =       jffs2_statfs,
253         .remount_fs =   jffs2_remount_fs,
254         .evict_inode =  jffs2_evict_inode,
255         .dirty_inode =  jffs2_dirty_inode,
256         .show_options = jffs2_show_options,
257         .sync_fs =      jffs2_sync_fs,
258 };
259
260 /*
261  * fill in the superblock
262  */
263 static int jffs2_fill_super(struct super_block *sb, void *data, int silent)
264 {
265         struct jffs2_sb_info *c;
266         int ret;
267
268         jffs2_dbg(1, "jffs2_get_sb_mtd():"
269                   " New superblock for device %d (\"%s\")\n",
270                   sb->s_mtd->index, sb->s_mtd->name);
271
272         c = kzalloc(sizeof(*c), GFP_KERNEL);
273         if (!c)
274                 return -ENOMEM;
275
276         c->mtd = sb->s_mtd;
277         c->os_priv = sb;
278         sb->s_fs_info = c;
279
280         ret = jffs2_parse_options(c, data);
281         if (ret) {
282                 kfree(c);
283                 return -EINVAL;
284         }
285
286         /* Initialize JFFS2 superblock locks, the further initialization will
287          * be done later */
288         mutex_init(&c->alloc_sem);
289         mutex_init(&c->erase_free_sem);
290         init_waitqueue_head(&c->erase_wait);
291         init_waitqueue_head(&c->inocache_wq);
292         spin_lock_init(&c->erase_completion_lock);
293         spin_lock_init(&c->inocache_lock);
294
295         sb->s_op = &jffs2_super_operations;
296         sb->s_export_op = &jffs2_export_ops;
297         sb->s_flags = sb->s_flags | MS_NOATIME;
298         sb->s_xattr = jffs2_xattr_handlers;
299 #ifdef CONFIG_JFFS2_FS_POSIX_ACL
300         sb->s_flags |= MS_POSIXACL;
301 #endif
302         ret = jffs2_do_fill_super(sb, data, silent);
303         return ret;
304 }
305
306 static struct dentry *jffs2_mount(struct file_system_type *fs_type,
307                         int flags, const char *dev_name,
308                         void *data)
309 {
310         return mount_mtd(fs_type, flags, dev_name, data, jffs2_fill_super);
311 }
312
313 static void jffs2_put_super (struct super_block *sb)
314 {
315         struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
316
317         jffs2_dbg(2, "%s()\n", __func__);
318
319         if (sb->s_dirt)
320                 jffs2_write_super(sb);
321
322         mutex_lock(&c->alloc_sem);
323         jffs2_flush_wbuf_pad(c);
324         mutex_unlock(&c->alloc_sem);
325
326         jffs2_sum_exit(c);
327
328         jffs2_free_ino_caches(c);
329         jffs2_free_raw_node_refs(c);
330         if (jffs2_blocks_use_vmalloc(c))
331                 vfree(c->blocks);
332         else
333                 kfree(c->blocks);
334         jffs2_flash_cleanup(c);
335         kfree(c->inocache_list);
336         jffs2_clear_xattr_subsystem(c);
337         mtd_sync(c->mtd);
338         jffs2_dbg(1, "%s(): returning\n", __func__);
339 }
340
341 static void jffs2_kill_sb(struct super_block *sb)
342 {
343         struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
344         if (!(sb->s_flags & MS_RDONLY))
345                 jffs2_stop_garbage_collect_thread(c);
346         kill_mtd_super(sb);
347         kfree(c);
348 }
349
350 static struct file_system_type jffs2_fs_type = {
351         .owner =        THIS_MODULE,
352         .name =         "jffs2",
353         .mount =        jffs2_mount,
354         .kill_sb =      jffs2_kill_sb,
355 };
356
357 static int __init init_jffs2_fs(void)
358 {
359         int ret;
360
361         /* Paranoia checks for on-medium structures. If we ask GCC
362            to pack them with __attribute__((packed)) then it _also_
363            assumes that they're not aligned -- so it emits crappy
364            code on some architectures. Ideally we want an attribute
365            which means just 'no padding', without the alignment
366            thing. But GCC doesn't have that -- we have to just
367            hope the structs are the right sizes, instead. */
368         BUILD_BUG_ON(sizeof(struct jffs2_unknown_node) != 12);
369         BUILD_BUG_ON(sizeof(struct jffs2_raw_dirent) != 40);
370         BUILD_BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
371         BUILD_BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
372
373         pr_info("version 2.2."
374 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
375                " (NAND)"
376 #endif
377 #ifdef CONFIG_JFFS2_SUMMARY
378                " (SUMMARY) "
379 #endif
380                " © 2001-2006 Red Hat, Inc.\n");
381
382         jffs2_inode_cachep = kmem_cache_create("jffs2_i",
383                                              sizeof(struct jffs2_inode_info),
384                                              0, (SLAB_RECLAIM_ACCOUNT|
385                                                 SLAB_MEM_SPREAD),
386                                              jffs2_i_init_once);
387         if (!jffs2_inode_cachep) {
388                 pr_err("error: Failed to initialise inode cache\n");
389                 return -ENOMEM;
390         }
391         ret = jffs2_compressors_init();
392         if (ret) {
393                 pr_err("error: Failed to initialise compressors\n");
394                 goto out;
395         }
396         ret = jffs2_create_slab_caches();
397         if (ret) {
398                 pr_err("error: Failed to initialise slab caches\n");
399                 goto out_compressors;
400         }
401         ret = register_filesystem(&jffs2_fs_type);
402         if (ret) {
403                 pr_err("error: Failed to register filesystem\n");
404                 goto out_slab;
405         }
406         return 0;
407
408  out_slab:
409         jffs2_destroy_slab_caches();
410  out_compressors:
411         jffs2_compressors_exit();
412  out:
413         kmem_cache_destroy(jffs2_inode_cachep);
414         return ret;
415 }
416
417 static void __exit exit_jffs2_fs(void)
418 {
419         unregister_filesystem(&jffs2_fs_type);
420         jffs2_destroy_slab_caches();
421         jffs2_compressors_exit();
422         kmem_cache_destroy(jffs2_inode_cachep);
423 }
424
425 module_init(init_jffs2_fs);
426 module_exit(exit_jffs2_fs);
427
428 MODULE_DESCRIPTION("The Journalling Flash File System, v2");
429 MODULE_AUTHOR("Red Hat, Inc.");
430 MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
431                        // the sake of this tag. It's Free Software.