Whamcloud - gitweb
- missed BKLs in ext3_ext_new_extent_cb()
[fs/lustre-release.git] / lustre / smfs / ioctl.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/smfs/ioctl.c
5  *  Lustre filesystem abstraction routines
6  *
7  *  Copyright (C) 2004 Cluster File Systems, Inc.
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24 #define DEBUG_SUBSYSTEM S_SM
25
26 #ifndef EXPORT_SYMTAB
27 #define EXPORT_SYMTAB
28 #endif
29
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/string.h>
33 #include <linux/slab.h>
34 #include <linux/stat.h>
35 #include <linux/unistd.h>
36 #include <linux/miscdevice.h>
37 #include <linux/obd_class.h>
38 #include <linux/obd_support.h>
39 #include <linux/lustre_lib.h>
40 #include <linux/lustre_idl.h>
41 #include <linux/lustre_fsfilt.h>
42 #include <linux/lustre_mds.h>
43 #include <linux/lustre_debug.h>
44 #include <linux/lustre_smfs.h>
45 #include <linux/lustre_snap.h>
46
47 #include "smfs_internal.h"
48
49 static struct super_block *smfs_get_sb_by_path(char *path, int len)
50 {
51         struct super_block *sb;
52         struct nameidata nd;
53
54         ENTRY;
55 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
56         if (path_init(path, LOOKUP_FOLLOW, &nd)) {
57                 error = path_walk(path, &nd);
58                 if (error) {
59                         path_release(&nd);
60                         RETURN(NULL);
61                 }
62         } else {
63                 RETURN(NULL);
64         }
65 #else
66         if (path_lookup(path, LOOKUP_FOLLOW, &nd))
67                 RETURN(NULL); 
68         
69 #endif
70         /* FIXME-WANGDI: add some check code here. */
71         sb = nd.dentry->d_sb;
72         path_release(&nd);
73         RETURN(sb);
74 }
75
76 struct smfs_control_device smfs_dev;
77
78 static int smfs_handle_ioctl(unsigned int cmd, unsigned long arg)
79 {
80         struct obd_ioctl_data *data = NULL;
81          struct super_block *sb = NULL;
82         char *buf = NULL;
83         int err = 0, len = 0;
84
85         if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
86                 CERROR("OBD ioctl: data error\n");
87                 GOTO(out, err = -EINVAL);
88         }
89         data = (struct obd_ioctl_data *)buf;
90
91         switch (cmd) {
92         case OBD_IOC_SMFS_SNAP_ADD:{
93                 char *name, *snapshot_name;
94                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
95                         CERROR("No mountpoint passed!\n");
96                         GOTO(out, err = -EINVAL);
97                 }
98                 if (!data->ioc_inllen2 || !data->ioc_inlbuf2) {
99                         CERROR("No snapshotname passed!\n");
100                         GOTO(out, err = -EINVAL);
101                 }
102                 name = (char*) data->ioc_inlbuf1;
103                 sb = smfs_get_sb_by_path(name,  data->ioc_inllen1);
104                 if (!sb) {
105                         CERROR("can not find superblock at %s\n", buf);
106                         GOTO(out, err = -EINVAL);
107                 }
108                 snapshot_name = (char *)data->ioc_inlbuf2;
109 #ifdef CONFIG_SNAPFS
110                 err = smfs_add_snap_item(sb, name, snapshot_name);
111 #endif         
112                 break;
113         }
114         default: {
115                 CERROR("The command passed in is Invalid\n");
116                 GOTO(out, err = -EINVAL);
117         }
118         }
119 out:
120         if (buf)
121                 obd_ioctl_freedata(buf, len);
122         RETURN(err);
123 }
124 #define SMFS_MINOR 250
125 static int smfs_psdev_ioctl(struct inode * inode, struct file * filp,
126                             unsigned int cmd, unsigned long arg)
127 {
128         int rc = 0;
129         rc = smfs_handle_ioctl(cmd, arg);
130         RETURN(rc);
131 }
132
133 /* called when opening /dev/device */
134 static int smfs_psdev_open(struct inode * inode, struct file * file)
135 {
136         int dev;
137         ENTRY;
138
139         if (!inode)
140                 RETURN(-EINVAL);
141         dev = MINOR(inode->i_rdev);
142         if (dev != SMFS_MINOR)
143                 RETURN(-ENODEV);
144
145         RETURN(0);
146 }
147
148 /* called when closing /dev/device */
149 static int smfs_psdev_release(struct inode * inode, struct file * file)
150 {
151         int dev;
152         ENTRY;
153
154         if (!inode)
155                 RETURN(-EINVAL);
156         dev = MINOR(inode->i_rdev);
157         if (dev != SMFS_MINOR)
158                 RETURN(-ENODEV);
159
160         RETURN(0);
161 }
162
163 /* declare character device */
164 static struct file_operations smfscontrol_fops = {
165         .owner   = THIS_MODULE,
166         .ioctl   = smfs_psdev_ioctl,            /* ioctl */
167         .open    = smfs_psdev_open,       /* open */
168         .release = smfs_psdev_release,    /* release */
169 };
170 static struct miscdevice smfscontrol_dev = {
171         minor:        SMFS_MINOR,
172         name:        "smfscontrol",
173         fops:        &smfscontrol_fops
174 };
175
176 int init_smfs_psdev(void)
177 {
178         printk(KERN_INFO "SMFS psdev driver  v0.01, braam@clusterfs.com\n");
179
180         misc_register(&smfscontrol_dev);
181
182         return 0;
183 }
184
185 void smfs_cleanup_psdev(void)
186 {
187         ENTRY;
188         misc_deregister(&smfscontrol_dev);
189         EXIT;
190 }