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_SM
13 #include <linux/config.h>
14 #include <linux/module.h>
15 #include <linux/kmod.h>
16 #include <linux/init.h>
18 #include <linux/string.h>
20 #include <linux/utime.h>
21 #include <linux/file.h>
22 #include <linux/slab.h>
23 #include <linux/loop.h>
24 #include <linux/errno.h>
25 #include "smfs_internal.h"
27 /* Find the options for the clone. These consist of a cache device
28 and an index in the snaptable associated with that device.
30 static char *smfs_options(char *options, char **devstr, char **namestr)
32 struct option *opt_value = NULL;
35 while (!(get_opt(&opt_value, &pos))) {
36 if (!strcmp(opt_value->opt, "dev")) {
38 *devstr = opt_value->value;
39 } else if (!strcmp(opt_value->opt, "type")) {
41 *namestr = opt_value->value;
48 static int close_fd(int fd)
50 struct files_struct *files = current->files;
52 write_lock(&files->file_lock);
55 __put_unused_fd(files, fd);
57 write_unlock(&files->file_lock);
60 static int set_loop_fd(char *dev_path, char *loop_dev)
62 struct loop_info loopinfo;
64 struct dentry *dentry;
65 struct block_device_operations *bd_ops;
67 int fd = 0, error = 0;
71 if (!fd) RETURN(-EINVAL);
73 filp = filp_open(dev_path, FMODE_WRITE, 0);
74 if (!filp || !S_ISREG(filp->f_dentry->d_inode->i_mode))
79 if (path_init(loop_dev, LOOKUP_FOLLOW, &nd)) {
80 error = path_walk(loop_dev, &nd);
83 filp_close(filp, current->files);
88 filp_close(filp, current->files);
92 bd_ops = get_blkfops(LOOP_MAJOR);
94 error = bd_ops->ioctl(dentry->d_inode, filp, LOOP_SET_FD,
98 filp_close(filp, current->files);
101 memset(&loopinfo, 0, sizeof(struct loop_info));
103 error = bd_ops->ioctl(dentry->d_inode, filp, LOOP_SET_STATUS,
104 (unsigned long)(&loopinfo));
109 #define SIZE(a) (sizeof(a)/sizeof(a[0]))
110 static char *find_unused_and_set_loop_device(char *dev_path)
112 char *loop_formats[] = { "/dev/loop/%d", "/dev/loop%d"};
113 struct loop_info loopinfo;
115 struct dentry *dentry;
119 for (j = 0; j < SIZE(loop_formats); j++) {
120 SM_ALLOC(dev, strlen(loop_formats[i]) + 1);
121 for(i = 0; i < 256; i++) {
122 struct block_device_operations *bd_ops;
124 sprintf(dev, loop_formats[j], i);
126 if (path_init(dev, LOOKUP_FOLLOW, &nd)) {
127 error = path_walk(dev, &nd);
128 if (error && error != -ENOENT) {
130 SM_FREE(dev, strlen(loop_formats[i]) + 1);
134 SM_FREE(dev, strlen(loop_formats[i]) + 1);
138 bd_ops = get_blkfops(LOOP_MAJOR);
139 error = bd_ops->ioctl(dentry->d_inode, NULL, LOOP_GET_STATUS,
140 (unsigned long)&loopinfo);
143 if (error == -ENXIO) {
144 /*find unused loop and set dev_path to loopdev*/
145 error = set_loop_fd(dev_path, dev);
147 SM_FREE(dev, strlen(loop_formats[i]) + 1);
150 return dev;/* probably free */
153 SM_FREE(dev, strlen(loop_formats[i]) + 1);
158 #define MAX_LOOP_DEVICES 256
159 static char *parse_path2dev(struct super_block *sb, char *dev_path)
161 struct dentry *dentry;
166 if (path_init(dev_path, LOOKUP_FOLLOW, &nd)) {
167 error = path_walk(dev_path, &nd);
177 if (!dentry->d_inode || is_bad_inode(dentry->d_inode) ||
178 (!S_ISBLK(dentry->d_inode->i_mode) &&
179 !S_ISREG(dentry->d_inode->i_mode))){
184 if (S_ISREG(dentry->d_inode->i_mode)) {
185 name = find_unused_and_set_loop_device(dev_path);
189 SM_ALLOC(name, strlen(dev_path) + 1);
190 memcpy(name, dev_path, strlen(dev_path) + 1);
193 static void duplicate_sb(struct super_block *csb,
194 struct super_block *sb)
196 sb->s_blocksize = csb->s_blocksize;
197 sb->s_magic = csb->s_magic;
198 sb->s_blocksize_bits = csb->s_blocksize_bits;
199 sb->s_maxbytes = csb->s_maxbytes;
200 sb->s_flags = csb->s_flags;
202 extern struct super_operations smfs_super_ops;
204 static int sm_mount_cache(struct super_block *sb,
208 struct vfsmount *mnt;
209 struct smfs_super_info *smb;
210 char *dev_name = NULL;
214 dev_name = parse_path2dev(sb, devstr);
216 GOTO(err_out, err = -ENOMEM);
218 if (!(page = __get_free_page(GFP_KERNEL))) {
219 GOTO(err_out, err = -ENOMEM);
221 memset((void *)page, 0, PAGE_SIZE);
222 sprintf((char *)page, "iopen_nopriv");
224 mnt = do_kern_mount(typestr, 0, dev_name, (void *)page);
228 CERROR("do_kern_mount failed: rc = %d\n", err);
232 smb->smsi_sb = mnt->mnt_sb;
235 duplicate_sb(mnt->mnt_sb, sb);
236 sm_set_sb_ops(mnt->mnt_sb, sb);
239 SM_FREE(dev_name, strlen(dev_name) + 2);
243 static int sm_umount_cache(struct super_block *sb)
245 struct smfs_super_info *smb = S2SMI(sb);
247 mntput(smb->smsi_mnt);
251 void smfs_put_super(struct super_block *sb)
260 struct super_block *sb,
264 struct inode *root_inode = NULL;
265 char *devstr = NULL, *typestr = NULL;
272 CDEBUG(D_SUPER, "mount opts: %s\n", data ? (char *)data : "(none)");
275 /* read and validate options */
276 cache_data = smfs_options(data, &devstr, &typestr);
278 CERROR("invalid mount option %s\n", (char*)data);
279 GOTO(out_err, err=-EINVAL);
281 if (!typestr || !devstr) {
282 CERROR("mount options name and dev mandatory\n");
283 GOTO(out_err, err=-EINVAL);
286 err = sm_mount_cache(sb, devstr, typestr);
288 CERROR("Can not mount %s as %s\n", devstr, typestr);
292 root_ino = S2CSB(sb)->s_root->d_inode->i_ino;
293 root_inode = iget(sb, root_ino);
295 CDEBUG(D_SUPER, "readinode %p, root ino %ld, root inode at %p\n",
296 sb->s_op->read_inode, root_ino, root_inode);
298 sb->s_root = d_alloc_root(root_inode);
301 GOTO(out_err, err=-EINVAL);
304 CDEBUG(D_SUPER, "sb %lx, &sb->u.generic_sbp: %lx\n",
305 (ulong) sb, (ulong) &sb->u.generic_sbp);
314 static DECLARE_FSTYPE(smfs_type, "smfs", smfs_read_super, 0);
320 err = register_filesystem(&smfs_type);
322 CERROR("smfs: failed in register Storage Management filesystem!\n");
328 int cleanup_smfs(void)
333 err = unregister_filesystem(&smfs_type);
335 CERROR("smfs: failed to unregister Storage Management filesystem!\n");
337 cleanup_smfs_cache();