Whamcloud - gitweb
doc/obd-howto.sgml: updated for initial public release
[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                 return;
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
100 static int obdfs_getdev(char *devpath, int *dev)
101 {
102         struct dentry *dentry;
103         kdev_t devno;
104
105         dentry = lookup_dentry(devpath, NULL, 0);
106         if (IS_ERR(dentry))
107                 return PTR_ERR(dentry);
108         
109         if (!S_ISCHR(dentry->d_inode->i_mode))
110                 return -ENODEV;
111
112         devno = dentry->d_inode->i_rdev;
113         if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) 
114                 return -ENODEV;
115         
116         if ( MINOR(devno) >= MAX_OBD_DEVICES ) 
117                 return -ENODEV;
118
119         *dev = devno;
120         return 0;
121 }
122
123 static struct super_block * obdfs_read_super(struct super_block *sb, 
124                                             void *data, int silent)
125 {
126         struct inode *root = 0; 
127         struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)(&sb->u.generic_sbp);
128         struct obd_device *obddev;
129         int error = 0;
130         char *device = NULL;
131         char *version = NULL;
132         int devno;
133         int err;
134         unsigned long blocksize;
135         unsigned long blocksize_bits;
136         unsigned long root_ino;
137         int scratch;
138         
139
140         ENTRY;
141         MOD_INC_USE_COUNT; 
142         
143         memset(sbi, 0, sizeof(*sbi));
144         
145         obdfs_options(data, &device, &version);
146         if ( !device ) {
147                 printk("No device\n");
148                 MOD_DEC_USE_COUNT;
149                 EXIT;
150                 return NULL;
151         }
152
153         if ( (err = obdfs_getdev(device, &devno)) ) {
154                 printk("Cannot get devno of %s, error %d\n", device, err);
155                 MOD_DEC_USE_COUNT;
156                 EXIT;
157                 return NULL;
158         }
159
160         if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) {
161                 printk("Wrong major number!\n");
162                 MOD_DEC_USE_COUNT;
163                 EXIT;
164                 return NULL;
165         }
166                 
167         if ( MINOR(devno) >= MAX_OBD_DEVICES ) {
168                 printk("Minor of %s too high (%d)\n", device, MINOR(devno));
169                 MOD_DEC_USE_COUNT;
170                 EXIT;
171                 return NULL;
172         } 
173
174         obddev = &obd_dev[MINOR(devno)];
175
176         if ( ! (obddev->obd_flags & OBD_ATTACHED) || 
177              ! (obddev->obd_flags & OBD_SET_UP) ){
178                 printk("Device %s not attached or not set up (%d)\n", 
179                        device, MINOR(devno));
180                 MOD_DEC_USE_COUNT;
181                 EXIT;
182                 return NULL;
183         } 
184
185         sbi->osi_obd = obddev;
186         sbi->osi_ops = sbi->osi_obd->obd_type->typ_ops;
187         
188         sbi->osi_conn.oc_dev = obddev;
189         error  = sbi->osi_ops->o_connect(&sbi->osi_conn);
190         if ( error ) {
191                 printk("OBDFS: cannot connect to %s\n", device);
192                 goto error;
193         }
194
195         
196
197         sbi->osi_super = sb;
198
199         error = sbi->osi_ops->o_get_info(&sbi->osi_conn,
200                                          strlen("blocksize"), 
201                                          "blocksize", 
202                                          &scratch, (void *)&blocksize);
203         if ( error ) {
204                 printk("Getinfo call to drive failed (blocksize)\n");
205                 goto error;
206         }
207
208         error = sbi->osi_ops->o_get_info(&sbi->osi_conn,
209                                          strlen("blocksize_bits"), 
210                                          "blocksize_bits", 
211                                          &scratch, (void *)&blocksize_bits);
212         if ( error ) {
213                 printk("Getinfo call to drive failed (blocksize_bits)\n");
214                 goto error;
215         }
216
217         error = sbi->osi_ops->o_get_info(&sbi->osi_conn,
218                                          strlen("root_ino"), 
219                                          "root_ino", 
220                                          &scratch, (void *)&root_ino);
221         if ( error ) {
222                 printk("Getinfo call to drive failed (root_ino)\n");
223                 goto error;
224         }
225         
226
227
228         lock_super(sb);
229         
230         sb->s_blocksize = blocksize;
231         sb->s_blocksize_bits = (unsigned char)blocksize_bits;
232         sb->s_magic = OBDFS_SUPER_MAGIC;
233         sb->s_op = &obdfs_super_operations;
234
235         /* make root inode */
236         root = iget(sb, root_ino);
237         if (!root || is_bad_inode(root)) {
238             printk("OBDFS: bad iget for root\n");
239             sb->s_dev = 0;
240             error = ENOENT;
241             unlock_super(sb);
242             goto error;
243         } 
244         
245
246         printk("obdfs_read_super: sbdev %d, rootino: %ld, dev %s, "
247                "minor: %d, blocksize: %ld, blocksize bits %ld\n", 
248                sb->s_dev, root->i_ino, device, MINOR(devno), 
249                blocksize, blocksize_bits);
250         sb->s_root = d_alloc_root(root);
251         unlock_super(sb);
252         EXIT;  
253         return sb;
254
255  error:
256         EXIT;  
257         MOD_DEC_USE_COUNT;
258         if (sbi) {
259                 sbi->osi_super = NULL;
260         }
261         if (root) {
262                 iput(root);
263         }
264         sb->s_dev = 0;
265         return NULL;
266 }
267
268 static void obdfs_put_super(struct super_block *sb)
269 {
270         struct obdfs_sb_info *sbi;
271
272         ENTRY;
273
274
275         sb->s_dev = 0;
276         
277         /* XXX flush stuff */
278         sbi = (struct obdfs_sb_info *) &sb->u.generic_sbp;
279
280         OPS(sb,disconnect)(ID(sb));
281
282         memset(sbi, 0, sizeof(* sbi));
283         
284         printk("OBDFS: Bye bye.\n");
285
286         MOD_DEC_USE_COUNT;
287         EXIT;
288 }
289
290 /* all filling in of inodes postponed until lookup */
291 void obdfs_read_inode(struct inode *inode)
292 {
293         int error;
294         ENTRY;
295
296         error = IOPS(inode, getattr)(IID(inode), inode);
297         if (error) {
298                 printk("obdfs_read_inode: obd_getattr fails (%d)\n", error);
299                 return;
300         }
301         CDEBUG(D_INODE, "ino %ld, mode: %o\n", inode->i_ino, inode->i_mode);
302         IDEBUG(inode);
303         if (S_ISREG(inode->i_mode))
304                 inode->i_op = &obdfs_file_inode_operations;
305         else if (S_ISDIR(inode->i_mode))
306                 inode->i_op = &obdfs_dir_inode_operations;
307         else if (S_ISLNK(inode->i_mode))
308                 inode->i_op = &obdfs_symlink_inode_operations;
309         else
310                 /* XXX what do we pass here??? */
311                 init_special_inode(inode, inode->i_mode, 0 /* XXX XXX */ );
312         return;
313 }
314
315 static void obdfs_write_inode(struct inode *inode) 
316 {
317         int error;
318         
319         error = IOPS(inode, setattr)(IID(inode), inode);
320         if (error) {
321                 printk("obdfs_write_inode: obd_setattr fails (%d)\n", error);
322                 return;
323         }
324         
325         return;
326 }
327
328 static void obdfs_delete_inode(struct inode *inode)
329 {
330         int error;
331         ENTRY;
332
333         error = IOPS(inode, destroy)(IID(inode), inode);
334         if (error) {
335                 printk("obdfs_delete_node: ibd_destroy fails (%d)\n", error);
336                 return;
337         }
338
339         EXIT;
340 }
341
342 static int  obdfs_notify_change(struct dentry *de, struct iattr *iattr)
343 {
344         struct inode *inode = de->d_inode;
345         struct iattr saved_copy;
346         int error;
347
348         ENTRY;
349         inode_to_iattr(inode, &saved_copy);
350
351         inode_setattr(inode, iattr);
352         error = IOPS(inode, setattr)(IID(inode), inode);
353         if ( error ) {
354                 inode_setattr(inode, &saved_copy);
355                 printk("obdfs_notify_change: obd_setattr fails (%d)\n", error);
356                 return error;
357         }
358
359         CDEBUG(D_INODE, "inode blocks now %ld\n", inode->i_blocks);
360         EXIT;
361         return error;
362 }
363
364
365 static int obdfs_statfs(struct super_block *sb, struct statfs *buf, 
366                        int bufsize)
367 {
368         struct statfs tmp;
369         int error;
370
371         ENTRY;
372
373         error = OPS(sb,statfs)(ID(sb), &tmp);
374         if ( error ) { 
375                 printk("obdfs_notify_change: obd_statfs fails (%d)\n", error);
376                 return error;
377         }
378         copy_to_user(buf, &tmp, (bufsize<sizeof(tmp)) ? bufsize : sizeof(tmp));
379
380         EXIT;
381
382         return error; 
383 }
384
385 struct file_system_type obdfs_fs_type = {
386    "obdfs", 0, obdfs_read_super, NULL
387 };
388
389 int init_obdfs(void)
390 {
391         printk(KERN_INFO "OBDFS v0.1, braam@stelias.com\n");
392
393         obdfs_sysctl_init();
394
395         return register_filesystem(&obdfs_fs_type);
396 }
397
398
399 #ifdef MODULE
400 int init_module(void)
401 {
402         return init_obdfs();
403 }
404
405 void cleanup_module(void)
406 {
407         ENTRY;
408
409         obdfs_sysctl_clean();
410         unregister_filesystem(&obdfs_fs_type);
411 }
412
413 #endif