4 * Copyright (C) 1998 Peter J. Braam
5 * Copyright (C) 2000 Stelias Computing, Inc.
6 * Copyright (C) 2000 Red Hat, Inc.
7 * Copyright (C) 2000 Mountain View Data, Inc.
9 * Author: Peter J. Braam <braam@mountainviewdata.com>
11 #define DEBUG_SUBSYSTEM S_SNAP
13 #include <linux/module.h>
14 #include <linux/kmod.h>
15 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/loop.h>
20 #include <linux/jbd.h>
21 #include <linux/ext3_fs.h>
22 #include <linux/snap.h>
23 #include <linux/errno.h>
24 #include "snapfs_internal.h"
28 unsigned int snap_debug_failcode = 0;
31 extern struct snap_cache *snap_init_cache(void);
32 extern inline void snap_cache_add(struct snap_cache *, kdev_t);
33 extern inline void snap_init_cache_hash(void);
35 extern int snap_get_index_from_name (int tableno, char *name);
38 extern struct snapshot_operations ext3_snap_operations;
39 extern struct journal_ops snap_ext3_journal_ops;
41 static void put_filesystem(struct file_system_type *fs)
44 __MOD_DEC_USE_COUNT(fs->owner);
47 /* In get_opt we get options in opt, value in opt_value
48 * we must remember to free opt and opt_value*/
49 static char * snapfs_options(char *options, char **cache_type,
50 char **cow_type, char **snaptable)
52 struct option *opt_value;
55 while (!(get_opt(&opt_value, &pos))) {
56 if (!strcmp(opt_value->opt, "cache_type")) {
57 if (cache_type != NULL)
58 *cache_type = opt_value->value;
59 } else if (!strcmp(opt_value->opt, "cow_type")) {
61 *cow_type = opt_value->value;
62 } else if (!strcmp(opt_value->opt, "snap_table")) {
63 if (snaptable != NULL)
64 *snaptable = opt_value->value;
69 if (!*cache_type && cache_type)
71 if (!*cow_type && cow_type)
73 if (!*snaptable && snaptable)
77 int snapfs_remount(struct super_block * sb, int *flags, char *data)
79 struct super_operations *sops;
80 struct snap_cache *cache = NULL;
81 char *snapno = NULL, *pos = NULL;
82 char *cache_data = NULL;
86 CDEBUG(D_SUPER, "remount opts: %s\n", data ? (char *)data : "(none)");
88 if ((err = init_option(data))) {
91 cache = snap_find_cache(sb->s_dev);
93 CERROR("cannot find cache on remount\n");
94 GOTO(out_err, err = -ENODEV);
97 /* If an option has not yet been set, we allow it to be set on
98 * remount. If an option already has a value, we pass NULL for
99 * the option pointer, which means that the snapfs option
100 * will be parsed but discarded.
102 cache_data = snapfs_options(data, NULL, NULL, &snapno);
104 CDEBUG(D_SUPER, "cache_data at %p is: %s\n", cache_data, cache_data);
105 sops = filter_c2csops(cache->cache_filter);
106 if (sops->remount_fs)
107 err = sops->remount_fs(sb, flags, pos);
113 * snapfs super block read.
115 * Allocate a struct snap_cache, determine the underlying fs type,
116 * read the underlying fs superblock, save the underlying fs ops,
117 * and then replace them with snapfs ops.
119 * Remove the snapfs options before passing to underlying fs.
123 struct super_block *sb,
127 struct file_system_type *fstype;
128 struct snap_cache *cache = NULL;
129 char *cache_type = NULL, *cow_type = NULL;
130 char *snapno = NULL, *cache_data = NULL;
136 cache_data = snapfs_options(data, &cache_type, &cow_type, &snapno);
137 /* set up the cache */
138 cache = snap_init_cache();
140 CERROR("snapfs_read_super: failure allocating cache.\n");
141 GOTO(out_err, rc = -EINVAL);
143 /*get cache and cache filter type */
144 fstype = get_fs_type((const char *)cache_type);
146 if ( !fstype || !fstype->read_super) {
147 CERROR("Unrecognized cache type %s \n", cache_type);
148 GOTO(out_err, rc = -EINVAL);
150 cache->cache_filter = filter_get_filter_fs((const char *)cache_type);
151 if (!cache->cache_filter) {
152 CERROR("Unrecognized cache type %s \n", cache_type);
153 GOTO(out_err, rc = -EINVAL);
157 * Read the underlying file system superblock - ext2, ext3, reiser.
158 * This performs the underlying mount operation. The snapfs options
159 * have been removed from 'cache_data'.
161 * Note: It's assumed that sb is always returned.
163 if (fstype->read_super(sb, cache_data, silent) != sb) {
164 CERROR("snapfs: cache mount failure.\n");
165 GOTO(out_err, rc = -EINVAL);
168 * We now know the dev of the cache: hash the cache.
170 * 'cache' is the struct snap_cache allocated for this
173 snap_cache_add(cache, sb->s_dev);
175 tableno = simple_strtoul(snapno, NULL, 0);
176 cache->cache_snap_tableno = tableno;
177 CDEBUG(D_SUPER, "get tableno %d\n", cache->cache_snap_tableno);
180 * make sure we have our own super operations
182 * Initialize or re-initialize the cache->cache_ops shared
183 * struct snap_ops structure set based on the underlying
186 filter_setup_super_ops(cache->cache_filter, sb->s_op,
187 ¤tfs_super_ops);
188 sb->s_op = filter_c2usops(cache->cache_filter);
190 * Save pointers in the snap_cache structure to the
191 * snapfs and underlying file system superblocks.
193 cache->cache_sb = sb; /* Underlying file system superblock. */
195 /* set up snapshot ops, handle COMPAT_FEATUREs */
198 else if (strcmp (cache_type,"ext3") == 0 || !cache_type){
199 cache->cache_type = FILTER_FS_EXT3;
200 filter_setup_snapshot_ops(cache->cache_filter,
201 &ext3_snap_operations);
202 filter_setup_journal_ops(cache->cache_filter,
203 &snap_ext3_journal_ops);
204 if( !EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_SNAPFS) ){
205 if( strcmp(cow_type, "block")==0 ){
206 sb->u.ext3_sb.s_es->s_feature_compat |=
207 cpu_to_le32(EXT3_FEATURE_COMPAT_BLOCKCOW);
210 sb->u.ext3_sb.s_last_cowed_pri_ino = 0;
211 sb->u.ext3_sb.s_first_cowed_pri_ino = 0;
213 /* now get our own directory operations */
214 if ( sb->s_root && sb->s_root->d_inode ) {
215 filter_setup_dir_ops(cache->cache_filter,
217 ¤tfs_dir_iops, ¤tfs_dir_fops);
218 sb->s_root->d_inode->i_op =filter_c2udiops(cache->cache_filter);
220 CDEBUG(D_SUPER, "lookup at %p\n",
221 sb->s_root->d_inode->i_op->lookup);
222 /* XXX is this needed ?? ext3 do not have dentry operations*/
223 filter_setup_dentry_ops(cache->cache_filter,
225 ¤tfs_dentry_ops);
226 sb->s_root->d_op = filter_c2udops(cache->cache_filter);
227 init_filter_data(sb->s_root->d_inode, 0);
230 * Save a pointer to the snap_cache structure in the
231 * "snap_current" superblock.
233 (struct snap_cache *) sb->u.generic_sbp = cache;
235 snapfs_read_snaptable(cache, tableno);
237 CDEBUG(D_SUPER, "sb %lx, sb->u.generic_sbp: %lx\n",
238 (ulong) sb, (ulong) sb->u.generic_sbp);
241 /* Inc in get_fs_type, Dec in put_fs_type*/
243 put_filesystem(fstype);
249 static DECLARE_FSTYPE_DEV(snapfs_current_type,
250 "snap_current", snapfs_read_super);
252 /* Find the options for the clone. These consist of a cache device
253 and an index in the snaptable associated with that device.
255 static char *clonefs_options(char *options, char **devstr, char **namestr)
257 struct option *opt_value = NULL;
260 while (!(get_opt(&opt_value, &pos))) {
261 if (!strcmp(opt_value->opt, "dev")) {
263 *devstr = opt_value->value;
264 } else if (!strcmp(opt_value->opt, "name")) {
266 *namestr = opt_value->value;
273 static int snap_cache_lookup_ino_cb(struct snap_cache *cache, void *in, unsigned long *out)
275 ino_t ino = *((unsigned long*)in);
278 struct super_block *sb = cache->cache_sb;
279 kdev_t dev = sb->s_dev;
281 if (MAJOR(dev) != LOOP_MAJOR)
283 if (sb->s_bdev->bd_op && sb->s_bdev->bd_op->ioctl) {
284 struct inode *inode = sb->s_bdev->bd_inode;
285 struct loop_info loop_info;
287 sb->s_bdev->bd_op->ioctl(inode, NULL, LOOP_GET_INFO,
288 (unsigned long)&loop_info);
290 if(loop_info.lo_inode == ino) {
298 static int snapfs_path2dev(char *dev_path, kdev_t *dev)
300 struct dentry *dentry;
304 if (path_init(dev_path, LOOKUP_FOLLOW, &nd)) {
305 error = path_walk(dev_path, &nd);
313 if (!dentry->d_inode || is_bad_inode(dentry->d_inode) ||
314 (!S_ISBLK(dentry->d_inode->i_mode) &&
315 !S_ISREG(dentry->d_inode->i_mode))){
319 if (S_ISBLK(dentry->d_inode->i_mode)) {
320 *dev = kdev_t_to_nr(dentry->d_inode->i_rdev);
322 /*here we must walk through all the snap cache to
323 *find the loop device */
326 if (snap_cache_process(snap_cache_lookup_ino_cb,
327 &dentry->d_inode->i_ino,
328 (unsigned long*)&tmp))
336 extern struct super_operations clone_super_ops;
338 * We always need to remove the snapfs options before passing
343 struct super_block *sb,
347 struct snap_clone_info *clone_sb;
348 struct snap_cache *snap_cache = NULL;
349 struct inode *root_inode = NULL;
350 char *devstr = NULL, *namestr = NULL;
359 CDEBUG(D_SUPER, "mount opts: %s\n", data ? (char *)data : "(none)");
362 /* read and validate options */
363 cache_data = clonefs_options(data, &devstr, &namestr);
365 CERROR("clonefs: invalid mount option %s\n", (char*)data);
366 GOTO(out_err, err=-EINVAL);
368 if (!namestr || !devstr) {
369 CERROR("snapfs: mount options name and dev mandatory\n");
370 GOTO(out_err, err=-EINVAL);
373 err = snapfs_path2dev(devstr, &dev);
375 CERROR("snap: incorrect device option %s\n", devstr);
376 GOTO(out_err, err=-EINVAL);
379 snap_cache = snap_find_cache(dev);
381 CERROR("snap: incorrect device option %s\n", devstr);
382 GOTO(out_err, err=-EINVAL);
385 index = snap_get_index_from_name (snap_cache->cache_snap_tableno,
387 CDEBUG(D_SUPER, "tableno %d, name %s, get index %d\n",
388 snap_cache->cache_snap_tableno, namestr, index);
391 CERROR("No valid index for name %s passed to mount\n",namestr);
392 GOTO(out_err, err=-EINVAL);
396 * Force clone fs to be read-only.
398 * XXX - Is there a way to change the mount options too so
399 * the fs is listed as RO by mount?
401 sb->s_flags |= MS_RDONLY;
403 /* set up the super block */
404 clone_sb = (struct snap_clone_info *)&sb->u.generic_sbp;
405 list_add(&clone_sb->clone_list_entry, &snap_cache->cache_clone_list);
406 clone_sb->clone_cache = snap_cache;
407 clone_sb->clone_index = index;
408 sb->s_op = &clone_super_ops;
410 root_ino = snap_cache->cache_sb->s_root->d_inode->i_ino;
411 root_inode = iget(sb, root_ino);
413 CDEBUG(D_SUPER, "readinode %p, root ino %ld, root inode at %p\n",
414 sb->s_op->read_inode, root_ino, root_inode);
416 sb->s_root = d_alloc_root(root_inode);
419 list_del(&clone_sb->clone_list_entry);
420 GOTO(out_err, err=-EINVAL);
422 dget(snap_cache->cache_sb->s_root);
424 CDEBUG(D_SUPER, "sb %lx, &sb->u.generic_sbp: %lx\n",
425 (ulong) sb, (ulong) &sb->u.generic_sbp);
433 static DECLARE_FSTYPE(snapfs_clone_type, "snap_clone", clone_read_super, 0);
435 int init_snapfs(void)
439 snap_init_cache_hash();
440 init_filter_info_cache();
442 status = register_filesystem(&snapfs_current_type);
444 CERROR("snapfs: failed in register current filesystem!\n");
447 status = register_filesystem(&snapfs_clone_type);
449 unregister_filesystem(&snapfs_current_type);
450 CERROR("snapfs: failed in register clone filesystem!\n");
458 int cleanup_snapfs(void)
464 cleanup_filter_info_cache();
465 err = unregister_filesystem(&snapfs_clone_type);
467 CERROR("snapfs: failed to unregister clone filesystem\n");
469 err = unregister_filesystem(&snapfs_current_type);
471 CERROR("snapfs: failed to unregister filesystem\n");