Whamcloud - gitweb
Cleanup : no more "sim" now "ext2"
[fs/lustre-release.git] / lustre / obdfs / super.c
1 /*
2  * OBDFS Super operations
3  *
4  * Copryright (C) 1996 Peter J. Braam <braam@stelias.com>
5  * Copryright (C) 1999 Stelias Computing Inc. <braam@stelias.com>
6  * Copryright (C) 1999 Seagate Technology Inc.
7  *
8  */
9
10 #define EXPORT_SYMTAB
11
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 #include <linux/string.h>
17 #include <linux/stat.h>
18 #include <linux/errno.h>
19 #include <linux/locks.h>
20 #include <linux/unistd.h>
21
22 #include <asm/system.h>
23 #include <asm/uaccess.h>
24
25 #include <linux/fs.h>
26 #include <linux/stat.h>
27 #include <asm/uaccess.h>
28 #include <linux/vmalloc.h>
29 #include <asm/segment.h>
30
31 #include <linux/obd_support.h>
32 #include <linux/obd_class.h>
33 #include <linux/obdfs.h>
34
35 /* VFS super_block ops */
36 static struct super_block *obdfs_read_super(struct super_block *, void *, int);
37 static int  obdfs_notify_change(struct dentry *dentry, struct iattr *attr);
38 static void obdfs_write_inode(struct inode *);
39 static void obdfs_delete_inode(struct inode *);
40 static void obdfs_put_super(struct super_block *);
41 static int obdfs_statfs(struct super_block *sb, struct statfs *buf, 
42                        int bufsiz);
43
44 /* exported operations */
45 struct super_operations obdfs_super_operations =
46 {
47         obdfs_read_inode,       /* read_inode */
48         obdfs_write_inode,      /* write_inode */
49         NULL,                   /* put_inode */
50         obdfs_delete_inode,     /* delete_inode */
51         obdfs_notify_change,    /* notify_change */
52         obdfs_put_super,        /* put_super */
53         NULL,                   /* write_super */
54         obdfs_statfs,           /* statfs */
55         NULL                    /* remount_fs */
56 };
57
58 static char *obdfs_read_opt(const char *opt, char *data)
59 {
60         char *value;
61         char *retval;
62
63         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
64         if ( strncmp(opt, data, strlen(opt)) )
65                 return NULL;
66
67         if ( (value = strchr(data, '=')) == NULL )
68                 return NULL;
69
70         value++;
71         OBD_ALLOC(retval, char *, strlen(value) + 1);
72         if ( !retval ) {
73                 printk("OBDFS: Out of memory!\n");
74                 return NULL;
75         }
76         
77         memcpy(retval, value, strlen(value)+1);
78         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
79         return retval;
80 }
81
82 void obdfs_options(char *options, char **dev, char **vers)
83 {
84         char *this_char;
85
86         if (!options)
87                 goto out;
88
89         for (this_char = strtok (options, ",");
90              this_char != NULL;
91              this_char = strtok (NULL, ",")) {
92                 CDEBUG(D_SUPER, "this_char %s\n", this_char);
93                 if ( (!*dev && (*dev = obdfs_read_opt("device", this_char))) ||
94                      (!*vers && (*vers = obdfs_read_opt("version", this_char))) )
95                         continue;
96                 
97         }
98
99  out:
100         if (!*dev) {
101                 *dev = "/dev/obd0";
102         }
103 }
104
105 static int obdfs_getdev(char *devpath, int *dev)
106 {
107         struct dentry *dentry;
108         kdev_t devno;
109
110         dentry = lookup_dentry(devpath, NULL, 0);
111         if (IS_ERR(dentry))
112                 return PTR_ERR(dentry);
113         
114         if (!S_ISCHR(dentry->d_inode->i_mode))
115                 return -ENODEV;
116
117         devno = dentry->d_inode->i_rdev;
118         if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) 
119                 return -ENODEV;
120         
121         if ( MINOR(devno) >= MAX_OBD_DEVICES ) 
122                 return -ENODEV;
123
124         *dev = devno;
125         return 0;
126 }
127
128 static struct super_block * obdfs_read_super(struct super_block *sb, 
129                                             void *data, int silent)
130 {
131         struct inode *root = 0; 
132         struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)(&sb->u.generic_sbp);
133         struct obd_device *obddev;
134         int error = 0;
135         char *device = NULL;
136         char *version = NULL;
137         int devno;
138         int err;
139         unsigned long blocksize;
140         unsigned long blocksize_bits;
141         unsigned long root_ino;
142         int scratch;
143         
144
145         ENTRY;
146         MOD_INC_USE_COUNT; 
147         
148         memset(sbi, 0, sizeof(*sbi));
149         
150         obdfs_options(data, &device, &version);
151         if ( !device ) {
152                 printk("No device\n");
153                 MOD_DEC_USE_COUNT;
154                 return NULL;
155         }
156
157         if ( (err = obdfs_getdev(device, &devno)) ) {
158                 printk("Cannot get devno of %s, error %d\n", device, err);
159                 MOD_DEC_USE_COUNT;
160                 return NULL;
161         }
162
163         if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) {
164                 printk("Wrong major number!\n");
165                 MOD_DEC_USE_COUNT;
166                 return NULL;
167         }
168                 
169         if ( MINOR(devno) >= MAX_OBD_DEVICES ) {
170                 printk("Minor of %s too high (%d)\n", device, MINOR(devno));
171                 MOD_DEC_USE_COUNT;
172                 return NULL;
173         } 
174
175         obddev = &obd_dev[MINOR(devno)];
176
177         if ( ! (obddev->obd_flags & OBD_ATTACHED) || 
178              ! (obddev->obd_flags & OBD_SET_UP) ){
179                 MOD_DEC_USE_COUNT;
180                 return NULL;
181         } 
182
183         sbi->osi_obd = obddev;
184         sbi->osi_ops = sbi->osi_obd->obd_type->typ_ops;
185         
186         error  = sbi->osi_ops->o_connect(sbi->osi_obd, &sbi->osi_conn_info);
187         if ( error ) {
188                 printk("OBDFS: cannot connect to %s\n", device);
189                 goto error;
190         }
191
192         
193
194         sbi->osi_super = sb;
195
196         error = sbi->osi_ops->o_get_info(sbi->osi_conn_info.conn_id, 
197                                          strlen("blocksize"), 
198                                          "blocksize", 
199                                          &scratch, (void *)&blocksize);
200         if ( error ) {
201                 printk("Getinfo call to drive failed (blocksize)\n");
202                 goto error;
203         }
204
205         error = sbi->osi_ops->o_get_info(sbi->osi_conn_info.conn_id, 
206                                          strlen("blocksize_bits"), 
207                                          "blocksize_bits", 
208                                          &scratch, (void *)&blocksize_bits);
209         if ( error ) {
210                 printk("Getinfo call to drive failed (blocksize_bits)\n");
211                 goto error;
212         }
213
214         error = sbi->osi_ops->o_get_info(sbi->osi_conn_info.conn_id, 
215                                          strlen("root_ino"), 
216                                          "root_ino", 
217                                          &scratch, (void *)&root_ino);
218         if ( error ) {
219                 printk("Getinfo call to drive failed (root_ino)\n");
220                 goto error;
221         }
222         
223
224
225         lock_super(sb);
226         
227         sb->s_blocksize = blocksize;
228         sb->s_blocksize_bits = (unsigned char)blocksize_bits;
229         sb->s_magic = OBDFS_SUPER_MAGIC;
230         sb->s_op = &obdfs_super_operations;
231
232         /* make root inode */
233         root = iget(sb, root_ino);
234         if (!root || is_bad_inode(root)) {
235             printk("OBDFS: bad iget for root\n");
236             sb->s_dev = 0;
237             error = ENOENT;
238             unlock_super(sb);
239             goto error;
240         } 
241         
242
243         printk("obdfs_read_super: sbdev %d, rootino: %ld, dev %s, "
244                "minor: %d, blocksize: %ld, blocksize bits %ld\n", 
245                sb->s_dev, root->i_ino, device, MINOR(devno), 
246                blocksize, blocksize_bits);
247         sb->s_root = d_alloc_root(root);
248         unlock_super(sb);
249         EXIT;  
250         return sb;
251
252  error:
253         EXIT;  
254         MOD_DEC_USE_COUNT;
255         if (sbi) {
256                 sbi->osi_super = NULL;
257         }
258         if (root) {
259                 iput(root);
260         }
261         sb->s_dev = 0;
262         return NULL;
263 }
264
265 static void obdfs_put_super(struct super_block *sb)
266 {
267         struct obdfs_sb_info *sbi;
268
269         ENTRY;
270
271
272         sb->s_dev = 0;
273         
274         /* XXX flush stuff */
275         sbi = (struct obdfs_sb_info *) &sb->u.generic_sbp;
276
277         OPS(sb,disconnect)(ID(sb));
278
279         memset(sbi, 0, sizeof(* sbi));
280         
281         printk("OBDFS: Bye bye.\n");
282
283         MOD_DEC_USE_COUNT;
284         EXIT;
285 }
286
287 extern struct inode_operations obdfs_inode_ops;
288
289 /* all filling in of inodes postponed until lookup */
290 void obdfs_read_inode(struct inode *inode)
291 {
292         int error;
293         ENTRY;
294
295         error = IOPS(inode, getattr)(IID(inode), inode->i_ino, inode);
296         if (error) {
297                 printk("obdfs_read_inode: obd_getattr fails (%d)\n", error);
298                 return;
299         }
300
301         IDEBUG(inode);
302         inode->i_op = &obdfs_inode_ops;
303         return;
304 }
305
306 static void obdfs_write_inode(struct inode *inode) 
307 {
308         int error;
309         
310         error = IOPS(inode, setattr)(IID(inode), inode->i_ino, inode);
311         if (error) {
312                 printk("obdfs_write_inode: obd_setattr fails (%d)\n", error);
313                 return;
314         }
315
316         return;
317 }
318
319 static void obdfs_delete_inode(struct inode *inode)
320 {
321         int error;
322         ENTRY;
323
324         error = IOPS(inode, destroy)(IID(inode), inode->i_ino);
325         if (error) {
326                 printk("obdfs_delete_node: ibd_destroy fails (%d)\n", error);
327                 return;
328         }
329
330         EXIT;
331 }
332
333 static int  obdfs_notify_change(struct dentry *de, struct iattr *iattr)
334 {
335         struct inode *inode = de->d_inode;
336         struct iattr saved_copy;
337         int error;
338
339         ENTRY;
340         inode_to_iattr(inode, &saved_copy);
341
342         inode_setattr(inode, iattr);
343         error = IOPS(inode, setattr)(IID(inode), inode->i_ino, inode);
344         if ( error ) {
345                 inode_setattr(inode, &saved_copy);
346                 printk("obdfs_notify_change: obd_setattr fails (%d)\n", error);
347                 return error;
348         }
349         EXIT;
350         return error;
351 }
352
353
354 static int obdfs_statfs(struct super_block *sb, struct statfs *buf, 
355                        int bufsize)
356 {
357         struct statfs tmp;
358         int error;
359
360         ENTRY;
361
362         error = OPS(sb,statfs)(ID(sb), &tmp);
363         if ( error ) { 
364                 printk("obdfs_notify_change: obd_statfs fails (%d)\n", error);
365                 return error;
366         }
367         copy_to_user(buf, &tmp, (bufsize<sizeof(tmp)) ? bufsize : sizeof(tmp));
368
369         EXIT;
370
371         return error; 
372 }
373
374 struct file_system_type obdfs_fs_type = {
375    "obdfs", 0, obdfs_read_super, NULL
376 };
377
378 int init_obdfs(void)
379 {
380         printk(KERN_INFO "OBDFS v0.1, braam@stelias.com\n");
381
382         obdfs_sysctl_init();
383
384         return register_filesystem(&obdfs_fs_type);
385 }
386
387
388 #ifdef MODULE
389 int init_module(void)
390 {
391         return init_obdfs();
392 }
393
394 void cleanup_module(void)
395 {
396         ENTRY;
397
398         obdfs_sysctl_clean();
399         unregister_filesystem(&obdfs_fs_type);
400 }
401
402 #endif