Whamcloud - gitweb
Working on add_to_page_cache oops.
[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 struct list_head obdfs_super_list;
59
60 static char *obdfs_read_opt(const char *opt, char *data)
61 {
62         char *value;
63         char *retval;
64
65         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
66         if ( strncmp(opt, data, strlen(opt)) )
67                 return NULL;
68
69         if ( (value = strchr(data, '=')) == NULL )
70                 return NULL;
71
72         value++;
73         OBD_ALLOC(retval, char *, strlen(value) + 1);
74         if ( !retval ) {
75                 printk("OBDFS: Out of memory!\n");
76                 return NULL;
77         }
78         
79         memcpy(retval, value, strlen(value)+1);
80         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
81         return retval;
82 }
83
84 void obdfs_options(char *options, char **dev, char **vers)
85 {
86         char *this_char;
87
88         if (!options)
89                 return;
90
91         for (this_char = strtok (options, ",");
92              this_char != NULL;
93              this_char = strtok (NULL, ",")) {
94                 CDEBUG(D_SUPER, "this_char %s\n", this_char);
95                 if ( (!*dev && (*dev = obdfs_read_opt("device", this_char)))||
96                      (!*vers && (*vers = obdfs_read_opt("version", this_char))) )
97                         continue;
98                 
99         }
100 }
101
102 static int obdfs_getdev(char *devpath, int *dev)
103 {
104         struct dentry *dentry;
105         kdev_t devno;
106
107         dentry = lookup_dentry(devpath, NULL, 0);
108         if (IS_ERR(dentry))
109                 return PTR_ERR(dentry);
110         
111         if (!S_ISCHR(dentry->d_inode->i_mode))
112                 return -ENODEV;
113
114         devno = dentry->d_inode->i_rdev;
115         if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) 
116                 return -ENODEV;
117         
118         if ( MINOR(devno) >= MAX_OBD_DEVICES ) 
119                 return -ENODEV;
120
121         *dev = devno;
122         return 0;
123 }
124
125
126 /* XXX allocate a super_entry, and add the super to the obdfs_super_list */
127 static struct super_block * obdfs_read_super(struct super_block *sb, 
128                                             void *data, int silent)
129 {
130         struct inode *root = 0; 
131         struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)(&sb->u.generic_sbp);
132         struct obd_device *obddev;
133         int error = 0;
134         char *device = NULL;
135         char *version = NULL;
136         int devno;
137         int err;
138         unsigned long blocksize;
139         unsigned long blocksize_bits;
140         unsigned long root_ino;
141         int scratch;
142         
143
144         ENTRY;
145         MOD_INC_USE_COUNT; 
146         
147         memset(sbi, 0, sizeof(*sbi));
148         
149         obdfs_options(data, &device, &version);
150         if ( !device ) {
151                 printk("No device\n");
152                 MOD_DEC_USE_COUNT;
153                 EXIT;
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                 EXIT;
161                 return NULL;
162         }
163
164         if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) {
165                 printk("Wrong major number!\n");
166                 MOD_DEC_USE_COUNT;
167                 EXIT;
168                 return NULL;
169         }
170                 
171         if ( MINOR(devno) >= MAX_OBD_DEVICES ) {
172                 printk("Minor of %s too high (%d)\n", device, MINOR(devno));
173                 MOD_DEC_USE_COUNT;
174                 EXIT;
175                 return NULL;
176         } 
177
178         obddev = &obd_dev[MINOR(devno)];
179
180         if ( ! (obddev->obd_flags & OBD_ATTACHED) || 
181              ! (obddev->obd_flags & OBD_SET_UP) ){
182                 printk("Device %s not attached or not set up (%d)\n", 
183                        device, MINOR(devno));
184                 MOD_DEC_USE_COUNT;
185                 EXIT;
186                 return NULL;
187         } 
188
189         sbi->osi_obd = obddev;
190         sbi->osi_ops = sbi->osi_obd->obd_type->typ_ops;
191         
192         sbi->osi_conn.oc_dev = obddev;
193         error  = sbi->osi_ops->o_connect(&sbi->osi_conn);
194         if ( error ) {
195                 printk("OBDFS: cannot connect to %s\n", device);
196                 goto error;
197         }
198
199         INIT_LIST_HEAD(&sbi->osi_list);
200
201         sbi->osi_super = sb;
202
203         error = sbi->osi_ops->o_get_info(&sbi->osi_conn,
204                                          strlen("blocksize"), 
205                                          "blocksize", 
206                                          &scratch, (void *)&blocksize);
207         if ( error ) {
208                 printk("Getinfo call to drive failed (blocksize)\n");
209                 goto error;
210         }
211
212         error = sbi->osi_ops->o_get_info(&sbi->osi_conn,
213                                          strlen("blocksize_bits"), 
214                                          "blocksize_bits", 
215                                          &scratch, (void *)&blocksize_bits);
216         if ( error ) {
217                 printk("Getinfo call to drive failed (blocksize_bits)\n");
218                 goto error;
219         }
220
221         error = sbi->osi_ops->o_get_info(&sbi->osi_conn,
222                                          strlen("root_ino"), 
223                                          "root_ino", 
224                                          &scratch, (void *)&root_ino);
225         if ( error ) {
226                 printk("Getinfo call to drive failed (root_ino)\n");
227                 goto error;
228         }
229         
230
231
232         lock_super(sb);
233         
234         sb->s_blocksize = blocksize;
235         sb->s_blocksize_bits = (unsigned char)blocksize_bits;
236         sb->s_magic = OBDFS_SUPER_MAGIC;
237         sb->s_op = &obdfs_super_operations;
238
239         /* make root inode */
240         root = iget(sb, root_ino);
241         if (!root || is_bad_inode(root)) {
242             printk("OBDFS: bad iget for root\n");
243             sb->s_dev = 0;
244             error = ENOENT;
245             unlock_super(sb);
246             goto error;
247         } 
248         
249
250         printk("obdfs_read_super: sbdev %d, rootino: %ld, dev %s, "
251                "minor: %d, blocksize: %ld, blocksize bits %ld\n", 
252                sb->s_dev, root->i_ino, device, MINOR(devno), 
253                blocksize, blocksize_bits);
254         sb->s_root = d_alloc_root(root);
255         unlock_super(sb);
256         EXIT;  
257         return sb;
258
259  error:
260         EXIT;  
261         MOD_DEC_USE_COUNT;
262         if (sbi) {
263                 sbi->osi_super = NULL;
264         }
265         if (root) {
266                 iput(root);
267         }
268         sb->s_dev = 0;
269         return NULL;
270 }
271
272 /* XXX remove the super to the obdfs_super_list */
273 static void obdfs_put_super(struct super_block *sb)
274 {
275         struct obdfs_sb_info *sbi;
276
277         ENTRY;
278
279
280         sb->s_dev = 0;
281         
282         /* XXX flush stuff */
283         sbi = (struct obdfs_sb_info *) &sb->u.generic_sbp;
284
285         OPS(sb,disconnect)(ID(sb));
286
287         memset(sbi, 0, sizeof(* sbi));
288         
289         printk("OBDFS: Bye bye.\n");
290
291         MOD_DEC_USE_COUNT;
292         EXIT;
293 }
294
295 inline int obdfs_has_inline(struct inode *inode)
296 {
297         struct obdfs_inode_info *oinfo = OBD_INFO(inode);
298         
299         return (oinfo->oi_flags & OBD_FL_INLINEDATA);
300 }
301
302 void inline obdfs_from_inode(struct obdo *oa, struct inode *inode)
303 {
304         obdo_from_inode(oa, inode);
305         if (obdfs_has_inline(inode)) {
306                 struct obdfs_inode_info *oinfo = OBD_INFO(inode);
307
308                 memcpy(oa->o_inline, oinfo->oi_inline, OBD_INLINESZ);
309                 oa->o_flags |= OBD_FL_INLINEDATA;
310         }
311 }
312
313 void inline obdfs_to_inode(struct inode *inode, struct obdo *oa)
314 {
315         obdo_to_inode(inode, oa);
316         if (obdo_has_inline(oa)) {
317                 struct obdfs_inode_info *oinfo = OBD_INFO(inode);
318
319                 memcpy(oinfo->oi_inline, oa->o_inline, OBD_INLINESZ);
320                 oinfo->oi_flags |= OBD_FL_INLINEDATA;
321         }
322 }
323
324 /* all filling in of inodes postponed until lookup */
325 void obdfs_read_inode(struct inode *inode)
326 {
327         struct obdo *oa;
328         int err;
329
330         ENTRY;
331         oa = obdo_alloc();
332         if (!oa) {
333                 printk("obdfs_read_inode: obdo_alloc failed\n");
334                 EXIT;
335                 return;
336         }
337         oa->o_valid = ~OBD_MD_FLOBDMD;
338         oa->o_id = inode->i_ino;
339         err = IOPS(inode, getattr)(IID(inode), oa);
340         if (err) {
341                 printk("obdfs_read_inode: obd_getattr fails (%d)\n", err);
342                 obdo_free(oa);
343                 EXIT;
344                 return;
345         }
346
347         ODEBUG(oa);
348         obdfs_to_inode(inode, oa);
349         obdo_free(oa);
350         IDEBUG(inode);
351
352         if (S_ISREG(inode->i_mode))
353                 inode->i_op = &obdfs_file_inode_operations;
354         else if (S_ISDIR(inode->i_mode))
355                 inode->i_op = &obdfs_dir_inode_operations;
356         else if (S_ISLNK(inode->i_mode))
357                 inode->i_op = &obdfs_symlink_inode_operations;
358         else
359                 /* XXX what do we pass here??? */
360                 init_special_inode(inode, inode->i_mode, 0 /* XXX XXX */ );
361         return;
362 }
363
364 static void obdfs_write_inode(struct inode *inode) 
365 {
366         struct obdo *oa;
367         int err;
368         
369         ENTRY;
370         oa = obdo_alloc();
371         oa->o_valid = OBD_MD_FLALL;
372         obdfs_from_inode(oa, inode);
373         err = IOPS(inode, setattr)(IID(inode), oa);
374
375         obdo_free(oa);
376
377         if (err) {
378                 printk("obdfs_write_inode: obd_setattr fails (%d)\n", err);
379                 EXIT;
380                 return;
381         }
382         
383         EXIT;
384 }
385
386 static void obdfs_delete_inode(struct inode *inode)
387 {
388         struct obdo *oa;
389         int err;
390         ENTRY;
391
392         oa = obdo_alloc();
393         /* XXX we currently assume "id" is all that's needed for destroy */
394         oa->o_id = inode->i_ino;
395         err = IOPS(inode, destroy)(IID(inode), oa);
396         obdo_free(oa);
397
398         if (err) {
399                 printk("obdfs_delete_node: obd_destroy fails (%d)\n", err);
400                 return;
401         }
402
403         EXIT;
404 }
405
406
407
408
409 static int obdfs_notify_change(struct dentry *de, struct iattr *attr)
410 {
411         struct inode *inode = de->d_inode;
412         struct obdo *oa;
413         int err;
414
415         ENTRY;
416         oa = obdo_alloc();
417         if (!oa) {
418                 printk("obdfs_notify_change: obdo_alloc fails\n");
419                 return -ENOMEM;
420         }
421
422         oa->o_id = inode->i_ino;
423         obdo_from_iattr(oa, attr);
424         err = IOPS(inode, setattr)(IID(inode), oa);
425         obdo_free(oa);
426
427         if ( err ) {
428                 printk("obdfs_notify_change: obd_setattr fails (%d)\n", err);
429                 return err;
430         }
431         inode_setattr(inode, attr);
432
433         CDEBUG(D_INODE, "inode blocks now %ld\n", inode->i_blocks);
434         EXIT;
435         return err;
436 }
437
438
439 static int obdfs_statfs(struct super_block *sb, struct statfs *buf, 
440                        int bufsize)
441 {
442         struct statfs tmp;
443         int err;
444
445         ENTRY;
446
447         err = OPS(sb,statfs)(ID(sb), &tmp);
448         if ( err ) { 
449                 printk("obdfs_notify_change: obd_statfs fails (%d)\n", err);
450                 return err;
451         }
452         copy_to_user(buf, &tmp, (bufsize<sizeof(tmp)) ? bufsize : sizeof(tmp));
453
454         EXIT;
455
456         return err; 
457 }
458
459 struct file_system_type obdfs_fs_type = {
460    "obdfs", 0, obdfs_read_super, NULL
461 };
462
463 int init_obdfs(void)
464 {
465         int err;
466
467         printk(KERN_INFO "OBDFS v0.1, braam@stelias.com\n");
468
469         obdfs_sysctl_init();
470
471         INIT_LIST_HEAD(&obdfs_super_list);
472         err = obdfs_init_wreqcache();
473         if (err)
474                 return err;
475
476         /* XXX
477         flushd_init();
478         */
479
480         return register_filesystem(&obdfs_fs_type);
481 }
482
483
484 #ifdef MODULE
485 int init_module(void)
486 {
487         return init_obdfs();
488 }
489
490 void cleanup_module(void)
491 {
492         ENTRY;
493
494         obdfs_cleanup_wreqcache();
495         obdfs_sysctl_clean();
496         unregister_filesystem(&obdfs_fs_type);
497 }
498
499 #endif