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/jbd.h>
20 #include <linux/ext3_fs.h>
21 #include <linux/snap.h>
22 #include <linux/errno.h>
23 #include "snapfs_internal.h"
27 unsigned int snap_debug_failcode = 0;
30 extern struct snap_cache *snap_init_cache(void);
31 extern inline void snap_cache_add(struct snap_cache *, kdev_t);
32 extern inline void snap_init_cache_hash(void);
34 extern int snap_get_index_from_name (int tableno, char *name);
37 extern struct snapshot_operations ext3_snap_operations;
38 extern struct journal_ops snap_ext3_journal_ops;
40 static void put_filesystem(struct file_system_type *fs)
43 __MOD_DEC_USE_COUNT(fs->owner);
47 /* returns an allocated string, copied out from data if opt is found */
48 static char *read_opt(const char *opt, char *data)
53 CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
54 if ( strncmp(opt, data, strlen(opt)) )
57 if ( (value = strchr(data, '=')) == NULL )
61 SNAP_ALLOC(retval, strlen(value) + 1);
63 CERROR("snapfs: Out of memory!\n");
67 strcpy(retval, value);
68 CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
72 static inline void store_opt(char **dst, char *opt)
76 SNAP_FREE(*dst, strlen(*dst) + 1);
79 SNAP_FREE(opt, strlen(opt) + 1);
82 /* Find the options for snapfs in "options", saving them into the
83 * passed pointers. If the pointer is null, the option is discarded.
84 * Copy out all non-snapfs options into cache_data (to be passed
85 * to the read_super operation of the cache). The return value will
86 * be a pointer to the end of the cache_data.
88 static char *snapfs_options(char *options, char *cache_data,
89 char **cache_type, char **cow_type,
93 char *cache_data_end = cache_data;
95 /* set the defaults here */
96 if (cache_type && !*cache_type) {
97 SNAP_ALLOC(*cache_type, strlen("ext3") + 1);
98 strcpy(*cache_type, "ext3");
100 if (cow_type && !*cow_type) {
101 SNAP_ALLOC(*cow_type, strlen("block") + 1);
102 strcpy(*cow_type, "block");
104 if (snaptable && !*snaptable) {
105 SNAP_ALLOC(*snaptable, strlen("-1")+1);
106 strcpy(*snaptable, "-1");
109 if (!options || !cache_data)
110 return cache_data_end;
112 CDEBUG(D_SUPER, "parsing options\n");
113 for (this_char = strtok (options, ",");
115 this_char = strtok (NULL, ",")) {
117 CDEBUG(D_SUPER, "this_char %s\n", this_char);
119 if ( (opt = read_opt("cache_type", this_char)) ) {
120 store_opt(cache_type, opt);
123 if ( (opt = read_opt("cow_type", this_char)) ){
124 store_opt(cow_type, opt);
127 if ( (opt = read_opt("table", this_char)) ) {
128 store_opt(snaptable, opt);
132 cache_data_end += sprintf(cache_data_end, "%s%s",
133 cache_data_end != cache_data ? ",":"",
137 return cache_data_end;
140 int snapfs_remount(struct super_block * sb, int *flags, char *data)
142 char *cache_data = NULL;
144 char *cache_data_end;
145 struct snap_cache *cache = NULL;
146 struct super_operations *sops;
150 CDEBUG(D_SUPER, "remount opts: %s\n", data ? (char *)data : "(none)");
152 /* reserve space for the cache's data */
153 SNAP_ALLOC(cache_data, PAGE_SIZE);
155 GOTO(out_err, err = -ENOMEM);
158 cache = snap_find_cache(sb->s_dev);
160 CERROR("cannot find cache on remount\n");
161 GOTO(out_err, err = -ENODEV);
164 /* If an option has not yet been set, we allow it to be set on
165 * remount. If an option already has a value, we pass NULL for
166 * the option pointer, which means that the snapfs option
167 * will be parsed but discarded.
169 cache_data_end = snapfs_options(data, cache_data, NULL, NULL, &snapno);
172 if (cache_data_end == cache_data) {
173 SNAP_FREE(cache_data, PAGE_SIZE);
176 CDEBUG(D_SUPER, "cache_data at %p is: %s\n", cache_data,
182 sops = filter_c2csops(cache->cache_filter);
183 if (sops->remount_fs) {
184 err = sops->remount_fs(sb, flags, cache_data);
190 SNAP_FREE(cache_data, PAGE_SIZE);
194 /* XXXX remount: needed if snapfs was mounted RO at boot time
200 * snapfs super block read.
202 * Allocate a struct snap_cache, determine the underlying fs type,
203 * read the underlying fs superblock, save the underlying fs ops,
204 * and then replace them with snapfs ops.
206 * Remove the snapfs options before passing to underlying fs.
210 struct super_block *sb,
214 struct file_system_type *fstype;
215 struct snap_cache *cache = NULL;
216 char *cache_data = NULL;
217 char *cache_data_end;
218 char *cache_type = NULL;
219 char *cow_type = NULL;
226 /* reserve space for the cache's data */
227 SNAP_ALLOC(cache_data, PAGE_SIZE);
229 CERROR("snapfs_read_super: Cannot allocate data page.\n");
233 CDEBUG(D_SUPER, "mount opts: %s\n", data ? (char *)data : "(none)");
235 /* read and validate options */
236 cache_data_end = snapfs_options(data, cache_data, &cache_type, &cow_type, &snapno);
238 /* Need to free cache_type and snapno when it's not in use */
240 /* was there anything for the cache filesystem in the data? */
241 if (cache_data_end == cache_data) {
242 SNAP_FREE(cache_data, PAGE_SIZE);
245 CDEBUG(D_SUPER, "cache_data at %p is: %s\n", cache_data,
249 /* set up the cache */
250 cache = snap_init_cache();
252 CERROR("snapfs_read_super: failure allocating cache.\n");
256 fstype = get_fs_type(cache_type);
257 if ( !fstype || !fstype->read_super) {
261 cache->cache_filter = filter_get_filter_fs((const char *)cache_type);
263 if (!cache->cache_filter) {
264 CERROR("Unrecognized cache type %s \n", cache_type);
269 * Read the underlying file system superblock - ext2, ext3, reiser.
270 * This performs the underlying mount operation. The snapfs options
271 * have been removed from 'cache_data'.
273 * Note: It's assumed that sb is always returned.
275 if (fstype->read_super(sb, cache_data, silent) != sb) {
276 CERROR("snapfs: cache mount failure.\n");
280 /* this might have been freed above */
282 SNAP_FREE(cache_data, PAGE_SIZE);
288 * We now know the dev of the cache: hash the cache.
290 * 'cache' is the struct snap_cache allocated for this
293 snap_cache_add(cache, sb->s_dev);
295 tableno = simple_strtoul(snapno, &endptr, 0);
296 cache->cache_snap_tableno = tableno;
299 CDEBUG(D_SUPER, "get tableno %d\n", cache->cache_snap_tableno);
301 * make sure we have our own super operations
303 * Initialize or re-initialize the cache->cache_ops shared
304 * struct snap_ops structure set based on the underlying
307 filter_setup_super_ops(cache->cache_filter, sb->s_op,
308 ¤tfs_super_ops);
309 sb->s_op = filter_c2usops(cache->cache_filter);
311 * Save pointers in the snap_cache structure to the
312 * snapfs and underlying file system superblocks.
314 cache->cache_sb = sb; /* Underlying file system superblock. */
316 /* set up snapshot ops, handle COMPAT_FEATUREs */
319 else if ( strcmp (cache_type,"ext3") == 0 ){
320 cache->cache_type = FILTER_FS_EXT3;
321 filter_setup_snapshot_ops(cache->cache_filter,
322 &ext3_snap_operations);
323 filter_setup_journal_ops(cache->cache_filter,
324 &snap_ext3_journal_ops);
325 if( !EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_SNAPFS) ){
326 if( strcmp(cow_type, "block")==0 ){
327 sb->u.ext3_sb.s_es->s_feature_compat |=
328 cpu_to_le32(EXT3_FEATURE_COMPAT_BLOCKCOW);
331 sb->u.ext3_sb.s_last_cowed_pri_ino = 0;
332 sb->u.ext3_sb.s_first_cowed_pri_ino = 0;
334 /* now get our own directory operations */
335 if ( sb->s_root && sb->s_root->d_inode ) {
336 filter_setup_dir_ops(cache->cache_filter,
338 ¤tfs_dir_iops, ¤tfs_dir_fops);
339 sb->s_root->d_inode->i_op =filter_c2udiops(cache->cache_filter);
341 CDEBUG(D_SUPER, "lookup at %p\n",
342 sb->s_root->d_inode->i_op->lookup);
343 /* XXX is this needed ?? ext3 do not have dentry operations*/
344 filter_setup_dentry_ops(cache->cache_filter,
346 ¤tfs_dentry_ops);
347 sb->s_root->d_op = filter_c2udops(cache->cache_filter);
350 * Save a pointer to the snap_cache structure in the
351 * "snap_current" superblock.
353 (struct snap_cache *) sb->u.generic_sbp = cache;
355 snapfs_read_snaptable(cache, tableno);
357 CDEBUG(D_SUPER, "sb %lx, sb->u.generic_sbp: %lx\n",
358 (ulong) sb, (ulong) sb->u.generic_sbp);
360 /* we can free snapno and cache_type now, because it's not used */
362 SNAP_FREE(snapno, strlen(snapno) + 1);
366 SNAP_FREE(cache_type, strlen(cache_type) + 1);
370 SNAP_FREE(cow_type, strlen(cow_type) + 1);
373 /* Inc in get_fs_type, Dec in put_fs_type*/
375 put_filesystem(fstype);
380 CDEBUG(D_SUPER, "out_err called\n");
382 SNAP_FREE(cache, sizeof(struct snap_cache));
384 SNAP_FREE(cache_data, PAGE_SIZE);
386 SNAP_FREE(snapno, strlen(snapno) + 1);
388 SNAP_FREE(cache_type, strlen(cache_type) + 1);
390 SNAP_FREE(cow_type, strlen(cow_type) + 1);
392 put_filesystem(fstype);
397 static DECLARE_FSTYPE_DEV(snapfs_current_type, "snap_current", snapfs_read_super);
400 /* Find the options for the clone. These consist of a cache device
401 and an index in the snaptable associated with that device.
403 static char *clonefs_options(char *options, char *cache_data,
404 char **devstr, char **namestr)
407 char *cache_data_end = cache_data;
409 if (!options || !cache_data)
410 return cache_data_end;
412 CDEBUG(D_SUPER, "parsing options\n");
413 for (this_char = strtok (options, ",");
415 this_char = strtok (NULL, ",")) {
417 CDEBUG(D_SUPER, "this_char %s\n", this_char);
419 if ( (opt = read_opt("dev", this_char)) ) {
420 store_opt(devstr, opt);
423 if ( (opt = read_opt("name", this_char)) ) {
424 store_opt(namestr, opt);
428 cache_data_end += sprintf(cache_data_end, "%s%s",
429 cache_data_end != cache_data ? ",":"",
433 return cache_data_end;
436 static int snapfs_path2dev(char *dev_path, kdev_t *dev)
438 struct dentry *dentry;
442 if (path_init(dev_path, LOOKUP_FOLLOW, &nd)) {
443 error = path_walk(dev_path, &nd);
451 if (!dentry->d_inode || !S_ISBLK(dentry->d_inode->i_mode) ||
452 is_bad_inode(dentry->d_inode) ) {
457 *dev = dentry->d_inode->i_rdev;
463 extern struct super_operations clone_super_ops;
466 * We always need to remove the snapfs options before passing
471 struct super_block *sb,
475 struct snap_clone_info *clone_sb;
476 struct snap_cache *snap_cache = NULL;
478 char *cache_data = NULL;
479 char *cache_data_end;
482 char *namestr = NULL;
486 struct inode *root_inode;
491 /* reserve space for the cache's data */
492 SNAP_ALLOC(cache_data, PAGE_SIZE);
494 CERROR("clone_read_super: Cannot allocate data page.\n");
498 CDEBUG(D_SUPER, "mount opts: %s\n", data ? (char *)data : "(none)");
500 /* read and validate options */
501 cache_data_end = clonefs_options(data, cache_data, &devstr, &namestr);
503 /* was there anything for the cache filesystem in the data? */
504 if (cache_data_end == cache_data) {
505 SNAP_FREE(cache_data, PAGE_SIZE);
508 CERROR("clonefs: invalid mount option %s\n", cache_data);
512 if (!namestr || !devstr) {
513 CERROR("snapfs: mount options name and dev mandatory\n");
517 err = snapfs_path2dev(devstr, &dev);
519 CERROR("snap: incorrect device option %s\n", devstr);
523 snap_cache = snap_find_cache(dev);
525 CERROR("snap: incorrect device option %s\n", devstr);
529 /*index = simple_strtoul(indexstr, &endptr, 0);
530 if ( indexstr == endptr ) {
531 printk("No valid index passed to mount\n");
537 index = snap_get_index_from_name (snap_cache->cache_snap_tableno,
539 CDEBUG(D_SUPER, "tableno %d, name %s, get index %d\n",
540 snap_cache->cache_snap_tableno, namestr, index);
543 CERROR("No valid index for name %s passed to mount\n",namestr);
548 * Force clone fs to be read-only.
550 * XXX - Is there a way to change the mount options too so
551 * the fs is listed as RO by mount?
553 sb->s_flags |= MS_RDONLY;
555 /* set up the super block */
556 clone_sb = (struct snap_clone_info *)&sb->u.generic_sbp;
557 list_add(&clone_sb->clone_list_entry, &snap_cache->cache_clone_list);
558 clone_sb->clone_cache = snap_cache;
559 clone_sb->clone_index = index;
560 sb->s_op = &clone_super_ops;
562 root_ino = snap_cache->cache_sb->s_root->d_inode->i_ino;
563 root_inode = iget(sb, root_ino);
565 CDEBUG(D_SUPER, "readinode %p, root ino %ld, root inode at %p\n",
566 sb->s_op->read_inode, root_ino, root_inode);
568 sb->s_root = d_alloc_root(root_inode);
570 list_del(&clone_sb->clone_list_entry);
574 dget(snap_cache->cache_sb->s_root);
577 SNAP_FREE(cache_data, PAGE_SIZE);
579 SNAP_FREE(devstr, strlen(devstr) + 1);
581 SNAP_FREE(namestr, strlen(namestr) + 1);
582 CDEBUG(D_SUPER, "sb %lx, &sb->u.generic_sbp: %lx\n",
583 (ulong) sb, (ulong) &sb->u.generic_sbp);
589 static DECLARE_FSTYPE(snapfs_clone_type, "snap_clone", clone_read_super, 0);
591 int init_snapfs(void)
595 snap_init_cache_hash();
596 init_filter_info_cache();
598 status = register_filesystem(&snapfs_clone_type);
600 unregister_filesystem(&snapfs_current_type);
601 CERROR("snapfs: failed in register clone filesystem!\n");
604 status = register_filesystem(&snapfs_current_type);
606 CERROR("snapfs: failed in register current filesystem!\n");
613 int cleanup_snapfs(void)
619 cleanup_filter_info_cache();
620 err = unregister_filesystem(&snapfs_clone_type);
622 CERROR("snapfs: failed to unregister clone filesystem\n");
624 err = unregister_filesystem(&snapfs_current_type);
626 CERROR("snapfs: failed to unregister filesystem\n");