4 * Copyright (C) 2005-2009
5 * Neil Brown <neilb@suse.de>
6 * Released under the GPL, version 2
10 #include <linux/namei.h>
11 #include <linux/crc32.h>
12 #include <linux/slab.h>
15 * Mounting a snapshot is very different from mounting a new
17 * The 'dev' passed is really a path to the mountpoint of the
18 * original - or atleast a file within the original.
19 * We find that filesystem, make sure it is a LaFS, find the
20 * named snapshot, and mount it read_only
27 static int parse_opts(struct options *op, const char *dev_name, char *data)
29 /* dev_name must start with '/',
30 * data must have snapshot=....
34 if (dev_name[0] != '/')
37 while ((p = strsep(&data, ",")) != NULL) {
40 if (strncmp(p, "snapshot=", 9) == 0) {
44 "LaFS: Unrecognised mount option \"%s\"\n", p);
48 if (op->snapshot == NULL || op->snapshot[0] == 0) {
50 "LaFS: No snaphot name given.\n");
56 /* s_fs_info for snapshots contains the snapshot number as well */
62 static int snap_test(struct super_block *sb, void *data)
64 struct snap_key *ptn = data;
65 struct snap_key *sk = container_of(sb->s_fs_info, struct snap_key, k);
66 return sk->k.fs == ptn->k.fs && sk->ssnum == ptn->ssnum;
69 static int snap_set(struct super_block *sb, void *data)
71 struct snap_key *sk = data;
72 sb->s_fs_info = &sk->k;
73 return set_anon_super(sb, NULL);
77 lafs_snap_get_sb(struct file_system_type *fstype,
78 int flags, const char *dev_name, void *data,
81 /* The 'dev_name' is a path to an object in the
82 * parent filesystem (typically the mountpoint).
83 * "data" contains 'snapshot=name' giving the name
84 * of the snapshot to be found and mounted
89 struct super_block *sb;
97 err = parse_opts(&op, dev_name, cdata);
101 p = alloc_page(GFP_KERNEL);
105 err = path_lookup(dev_name, LOOKUP_FOLLOW, &nd);
110 sb = nd.path.dentry->d_sb;
112 if (sb->s_type != &lafs_fs_type)
116 for (s = 1; s < fs->maxsnapshot; s++) {
119 if (fs->ss[s].root_addr == 0)
121 if (fs->ss[s].root) {
122 struct lafs_inode *li = LAFSI(fs->ss[s].root);
123 if (li->md.fs.name &&
124 strcmp(li->md.fs.name, op.snapshot) == 0)
128 err = lafs_load_page(fs, p, fs->ss[s].root_addr, 1);
131 lai = (struct la_inode *)page_address(p);
132 nlen = le16_to_cpu(lai->metadata_size) -
133 offsetof(struct la_inode, metadata[0].fs.name);
134 printk("ss %d is %.*s\n", s, nlen, lai->metadata[0].fs.name);
135 if (strncmp(lai->metadata[0].fs.name, op.snapshot, nlen) != 0)
137 /* FIXME more checks? */
141 if (s == fs->maxsnapshot) {
145 /* Ok, we have the right snapshot... now we need a superblock */
146 sk = kzalloc(sizeof(*sk), GFP_KERNEL);
154 sb = sget(&lafs_snap_fs_type, snap_test, snap_set, NULL);
161 /* already existed */
164 struct inode *rootino, *rootdir;
166 sb->s_flags = flags | MS_RDONLY;
168 atomic_inc(&fs->prime_sb->s_active);
170 rootino = iget_locked(sb, 0);
171 fs->ss[s].root = sk->k.root = rootino;
172 LAFSI(rootino)->filesys = rootino;
173 b = lafs_get_block(rootino, 0, NULL, GFP_KERNEL,
175 b->b.physaddr = fs->ss[s].root_addr;
176 set_bit(B_PhysValid, &b->b.flags);
177 LAFS_BUG(test_bit(B_Valid, &b->b.flags), &b->b);
178 printk("ss root at %llu\n", b->b.physaddr);
179 err = lafs_load_block(&b->b, NULL);
181 err = lafs_wait_block(&b->b);
183 err = lafs_import_inode(fs->ss[s].root, b);
184 putdref(b, MKREF(snap));
185 unlock_new_inode(rootino);
187 deactivate_locked_super(sb);
190 rootdir = lafs_iget(rootino, 2, SYNC);
191 sb->s_root = d_alloc_root(rootdir);
192 sb->s_op = fs->prime_sb->s_op;
193 sb->s_flags |= MS_ACTIVE;
195 up_write(&sb->s_umount);
198 simple_set_mnt(mnt, sb);
207 static void lafs_snap_kill_sb(struct super_block *sb)
209 struct fs *fs = fs_from_sb(sb);
211 printk("Generic_shutdown_super being called....\n");
213 kfree(sb->s_fs_info);
214 printk("Generic_shutdown_super called\n");
215 deactivate_super(fs->prime_sb);
218 struct file_system_type lafs_snap_fs_type = {
219 .owner = THIS_MODULE,
221 .get_sb = lafs_snap_get_sb,
222 .kill_sb = lafs_snap_kill_sb,