Whamcloud - gitweb
- obd filter works over ext2, but bonnie++ is buggie
[fs/lustre-release.git] / lustre / obdfs / super.c
1 /*
2  * OBDFS Super operations
3  *
4  * This code is issued under the GNU General Public License.
5  * See the file COPYING in this distribution
6  *
7  * Copryright (C) 1996 Peter J. Braam <braam@stelias.com>
8  * Copryright (C) 1999 Stelias Computing Inc. <braam@stelias.com>
9  * Copryright (C) 1999 Seagate Technology Inc.
10  * Copryright (C) 2001 Mountain View Data, Inc.
11  *
12  */
13
14 #include <linux/config.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/mm.h>
18 #include <linux/string.h>
19 #include <linux/stat.h>
20 #include <linux/errno.h>
21 #include <linux/locks.h>
22 #include <linux/unistd.h>
23
24 #include <asm/system.h>
25 #include <asm/uaccess.h>
26
27 #include <linux/fs.h>
28 #include <linux/stat.h>
29 #include <asm/uaccess.h>
30 #include <linux/vmalloc.h>
31 #include <asm/segment.h>
32
33 #include <linux/obd_support.h>
34 #include <linux/obd_class.h>
35 #include <linux/obdfs.h>
36
37 struct list_head obdfs_super_list;
38 struct address_space_operations obdfs_aops;
39 struct super_operations obdfs_super_operations;
40 long obdfs_cache_count = 0;
41 long obdfs_mutex_start = 0;
42 long obd_memory = 0;
43
44 static char *obdfs_read_opt(const char *opt, char *data)
45 {
46         char *value;
47         char *retval;
48
49         CDEBUG(D_INFO, "option: %s, data %s\n", opt, data);
50         if ( strncmp(opt, data, strlen(opt)) )
51                 return NULL;
52
53         if ( (value = strchr(data, '=')) == NULL )
54                 return NULL;
55
56         value++;
57         OBD_ALLOC(retval, char *, strlen(value) + 1);
58         if ( !retval ) {
59                 printk(KERN_ALERT __FUNCTION__ ": out of memory!\n");
60                 return NULL;
61         }
62         
63         memcpy(retval, value, strlen(value)+1);
64         CDEBUG(D_PSDEV, "Assigned option: %s, value %s\n", opt, retval);
65         return retval;
66 }
67
68 static void obdfs_options(char *options, char **dev, char **vers)
69 {
70         char *this_char;
71
72         if (!options)
73                 return;
74
75         for (this_char = strtok (options, ",");
76              this_char != NULL;
77              this_char = strtok (NULL, ",")) {
78                 CDEBUG(D_INFO, "this_char %s\n", this_char);
79                 if ( (!*dev && (*dev = obdfs_read_opt("device", this_char)))||
80                      (!*vers && (*vers = obdfs_read_opt("version", this_char))) )
81                         continue;
82                 
83         }
84 }
85
86 static int obdfs_getdev(char *devpath, int *dev)
87 {
88         struct dentry *dentry;
89         kdev_t devno;
90         struct nameidata nd;
91         int error = 0; 
92
93         ENTRY;
94         if (path_init(devpath, LOOKUP_POSITIVE, &nd))
95                 error = path_walk(devpath, &nd);
96         if (error)
97                 return error;
98
99         dentry = nd.dentry;
100         if (!S_ISCHR(dentry->d_inode->i_mode))
101                 return -ENODEV;
102
103         devno = dentry->d_inode->i_rdev;
104         if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) 
105                 return -ENODEV;
106         
107         if ( MINOR(devno) >= MAX_OBD_DEVICES ) 
108                 return -ENODEV;
109
110         *dev = devno;
111         return 0;
112 }
113
114
115 static struct super_block * obdfs_read_super(struct super_block *sb, 
116                                             void *data, int silent)
117 {
118         struct inode *root = 0; 
119         struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)(&sb->u.generic_sbp);
120         struct obd_device *obddev;
121         char *device = NULL;
122         char *version = NULL;
123         int connected = 0;
124         int devno;
125         int err;
126         unsigned long blocksize;
127         unsigned long blocksize_bits;
128         unsigned long root_ino;
129         int scratch;
130         struct obdo *oa;
131         
132
133         ENTRY;
134         MOD_INC_USE_COUNT; 
135         
136         memset(sbi, 0, sizeof(*sbi));
137         
138         CDEBUG(D_INFO, "\n"); 
139         obdfs_options(data, &device, &version);
140         if ( !device ) {
141                 printk(__FUNCTION__ ": no device\n");
142                 EXIT;
143                 goto ERR;
144         }
145
146         CDEBUG(D_INFO, "\n"); 
147         if ( (err = obdfs_getdev(device, &devno)) ) {
148                 printk("Cannot get devno of %s, error %d\n", device, err);
149                 EXIT;
150                 goto ERR;;
151         }
152
153         CDEBUG(D_INFO, "\n"); 
154         if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) {
155                 printk(__FUNCTION__ ": wrong major number %d!\n", MAJOR(devno));
156                 EXIT;
157                 goto ERR;
158         }
159                 
160         CDEBUG(D_INFO, "\n"); 
161         if ( MINOR(devno) >= MAX_OBD_DEVICES ) {
162                 printk(__FUNCTION__ ": minor of %s too high (%d)\n",
163                        device, MINOR(devno));
164                 EXIT;
165                 goto ERR;
166         } 
167
168         CDEBUG(D_INFO, "\n"); 
169         obddev = &obd_dev[MINOR(devno)];
170
171         CDEBUG(D_INFO, "\n"); 
172         if ( ! (obddev->obd_flags & OBD_ATTACHED) || 
173              ! (obddev->obd_flags & OBD_SET_UP) ){
174                 printk("device %s not attached or not set up (%d)\n", 
175                        device, MINOR(devno));
176                 EXIT;
177                 goto ERR;;
178         } 
179
180         CDEBUG(D_INFO, "\n"); 
181         sbi->osi_obd = obddev;
182         sbi->osi_ops = sbi->osi_obd->obd_type->typ_ops;
183         
184         sbi->osi_conn.oc_dev = obddev;
185         err = sbi->osi_ops->o_connect(&sbi->osi_conn);
186         if ( err ) {
187                 printk("OBDFS: cannot connect to %s\n", device);
188                 EXIT;
189                 goto ERR;
190         }
191         connected = 1;
192         CDEBUG(D_INFO, "\n"); 
193         /* list of dirty inodes, and a mutex to hold while modifying it */
194         INIT_LIST_HEAD(&sbi->osi_inodes);
195         init_MUTEX (&sbi->osi_list_mutex);
196
197         CDEBUG(D_INFO, "\n"); 
198         sbi->osi_super = sb;
199
200         CDEBUG(D_INFO, "\n"); 
201         err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize"),
202                                        "blocksize", &scratch,
203                                        (void *)&blocksize);
204         if ( err ) {
205                 printk("getinfo call to drive failed (blocksize)\n");
206                 EXIT;
207                 goto ERR;
208         }
209
210         CDEBUG(D_INFO, "\n"); 
211         err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize_bits"),
212                                        "blocksize_bits", &scratch,
213                                        (void *)&blocksize_bits);
214         if ( err ) {
215                 printk("getinfo call to drive failed (blocksize_bits)\n");
216                 EXIT;
217                 goto ERR;
218         }
219
220         CDEBUG(D_INFO, "\n"); 
221         err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("root_ino"), 
222                                        "root_ino", &scratch, (void *)&root_ino);
223         if ( err ) {
224                 printk("getinfo call to drive failed (root_ino)\n");
225                 EXIT;
226                 goto ERR;
227         }
228         
229         CDEBUG(D_INFO, "\n"); 
230         sb->s_maxbytes = 1LL << 36;
231         printk("Max bytes: %Lx\n", sb->s_maxbytes);
232         sb->s_blocksize = PAGE_SIZE;
233         sb->s_blocksize_bits = (unsigned char)PAGE_SHIFT;
234         sb->s_magic = OBDFS_SUPER_MAGIC;
235         sb->s_op = &obdfs_super_operations;
236
237         /* XXX how to get "sb->s_flags |= MS_RDONLY" here for snapshots? */
238
239         /* make root inode */
240         CDEBUG(D_INFO, "\n"); 
241         oa = obdo_fromid(&sbi->osi_conn, root_ino,
242                          OBD_MD_FLNOTOBD | OBD_MD_FLBLOCKS);
243         CDEBUG(D_INFO, "\n"); 
244         if ( IS_ERR(oa) ) {
245                 printk(__FUNCTION__ ": obdo_fromid failed\n");
246                 iput(root); 
247                 EXIT;
248                 goto ERR;
249         }
250         CDEBUG(D_INFO, "\n"); 
251         root = iget4(sb, root_ino, NULL, oa);
252         obdo_free(oa);
253         CDEBUG(D_INFO, "\n"); 
254         if (!root) {
255             printk("OBDFS: bad iget4 for root\n");
256             sb->s_dev = 0;
257             err = -ENOENT;
258             EXIT;
259             goto ERR;
260         } 
261         
262         CDEBUG(D_INFO, "obdfs_read_super: sbdev %d, rootino: %ld, dev %s, "
263                "minor: %d, blocksize: %ld, blocksize bits %ld\n", 
264                sb->s_dev, root->i_ino, device, MINOR(devno), 
265                blocksize, blocksize_bits);
266         sb->s_root = d_alloc_root(root);
267         list_add(&sbi->osi_list, &obdfs_super_list);
268         OBD_FREE(device, strlen(device) + 1);
269         if (version)
270                 OBD_FREE(version, strlen(version) + 1);
271         EXIT;  
272         return sb;
273
274 ERR:
275         MOD_DEC_USE_COUNT;
276         if (device)
277                 OBD_FREE(device, strlen(device) + 1);
278         if (version)
279                 OBD_FREE(version, strlen(version) + 1);
280         if (connected) 
281                 sbi->osi_ops->o_disconnect(&sbi->osi_conn);
282
283         if (sbi) {
284                 sbi->osi_super = NULL;
285         }
286         if (root) {
287                 iput(root);
288         }
289         sb->s_dev = 0;
290         return NULL;
291 } /* obdfs_read_super */
292
293
294 static void obdfs_put_super(struct super_block *sb)
295 {
296         struct obdfs_sb_info *sbi;
297
298         ENTRY;
299         sb->s_dev = 0;
300         
301         sbi = (struct obdfs_sb_info *) &sb->u.generic_sbp;
302         //obdfs_flush_reqs(&sbi->osi_inodes, ~0UL);
303
304         OPS(sb,disconnect)(ID(sb));
305         list_del(&sbi->osi_list);
306         
307         printk(KERN_INFO "OBDFS: Bye bye.\n");
308
309         MOD_DEC_USE_COUNT;
310         EXIT;
311 } /* obdfs_put_super */
312
313
314 void obdfs_do_change_inode(struct inode *inode, int valid)
315 {
316         struct obdo *oa;
317         int err;
318         
319         ENTRY;
320         if (IOPS(inode, setattr) == NULL) {
321                 printk(KERN_ERR __FUNCTION__ ": no setattr method!\n");
322                 EXIT;
323                 return;
324         }
325         oa = obdo_alloc();
326         if ( !oa ) {
327                 printk(__FUNCTION__ ": obdo_alloc failed\n");
328                 EXIT;
329                 return;
330         }
331
332         oa->o_valid = OBD_MD_FLNOTOBD & (valid | OBD_MD_FLID);
333         obdfs_from_inode(oa, inode);
334         err = IOPS(inode, setattr)(IID(inode), oa);
335
336         if ( err )
337                 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
338
339         EXIT;
340         obdo_free(oa);
341 } /* obdfs_write_inode */
342
343 void obdfs_change_inode(struct inode *inode, int mask)
344 {
345         return obdfs_do_change_inode(inode, OBD_MD_FLNLINK); 
346 }
347
348
349 extern void write_inode_pages(struct inode *);
350 /* This routine is called from iput() (for each unlink on the inode).
351  * We can't put this call into delete_inode() since that is called only
352  * when i_count == 0, and we need to keep a reference on the inode while
353  * it is in the page cache, which means i_count > 0.  Catch 22.
354  */
355 static void obdfs_put_inode(struct inode *inode)
356 {
357         ENTRY;
358         if (inode->i_nlink && (atomic_read(&inode->i_count) == 1)) {
359                 write_inode_pages(inode);
360                 EXIT;
361                 return;
362         }
363
364         //obdfs_dequeue_pages(inode);
365         EXIT;
366 } /* obdfs_put_inode */
367
368
369 static void obdfs_delete_inode(struct inode *inode)
370 {
371         obdfs_do_change_inode(inode, ~0);
372         clear_inode(inode); 
373 }
374 #if 0
375 {
376         struct obdo *oa;
377         int err;
378
379         ENTRY;
380         if (IOPS(inode, destroy) == NULL) {
381                 printk(KERN_ERR __FUNCTION__ ": no destroy method!\n");
382                 EXIT;
383                 return;
384         }
385
386         oa = obdo_alloc();
387         if ( !oa ) {
388                 printk(__FUNCTION__ ": obdo_alloc failed\n");
389                 EXIT;
390                 return;
391         }
392         oa->o_valid = OBD_MD_FLNOTOBD;
393         obdfs_from_inode(oa, inode);
394
395         /* XXX how do we know that this inode is now clean? */
396         printk("delete_inode ------> link %d\n", inode->i_nlink);
397         ODEBUG(oa);
398         err = IOPS(inode, destroy)(IID(inode), oa);
399         obdo_free(oa);
400         clear_inode(inode);
401         if (err) {
402                 printk(__FUNCTION__ ": obd_destroy fails (%d)\n", err);
403                 EXIT;
404                 return;
405         }
406
407         EXIT;
408 } /* obdfs_delete_inode */
409 #endif
410
411
412 static int obdfs_attr2inode(struct inode * inode, struct iattr * attr)
413 {
414         unsigned int ia_valid = attr->ia_valid;
415         int error = 0;
416
417         if (ia_valid & ATTR_SIZE) {
418                 error = vmtruncate(inode, attr->ia_size);
419                 if (error)
420                         goto out;
421         }
422
423         if (ia_valid & ATTR_UID)
424                 inode->i_uid = attr->ia_uid;
425         if (ia_valid & ATTR_GID)
426                 inode->i_gid = attr->ia_gid;
427         if (ia_valid & ATTR_ATIME)
428                 inode->i_atime = attr->ia_atime;
429         if (ia_valid & ATTR_MTIME)
430                 inode->i_mtime = attr->ia_mtime;
431         if (ia_valid & ATTR_CTIME)
432                 inode->i_ctime = attr->ia_ctime;
433         if (ia_valid & ATTR_MODE) {
434                 inode->i_mode = attr->ia_mode;
435                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
436                         inode->i_mode &= ~S_ISGID;
437         }
438 out:
439         return error;
440 }
441
442 int obdfs_setattr(struct dentry *de, struct iattr *attr)
443 {
444         struct inode *inode = de->d_inode;
445         struct obdo *oa;
446         int err;
447
448         ENTRY;
449         if (IOPS(inode, setattr) == NULL) {
450                 printk(KERN_ERR __FUNCTION__ ": no setattr method!\n");
451                 EXIT;
452                 return -EIO;
453         }
454         oa = obdo_alloc();
455         if ( !oa ) {
456                 printk(__FUNCTION__ ": obdo_alloc failed\n");
457                 return -ENOMEM;
458         }
459
460         obdfs_attr2inode(inode, attr);
461         oa->o_id = inode->i_ino;
462         obdo_from_iattr(oa, attr);
463         err = IOPS(inode, setattr)(IID(inode), oa);
464
465         if ( err )
466                 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
467
468         EXIT;
469         obdo_free(oa);
470         return err;
471 } /* obdfs_setattr */
472
473
474
475 static int obdfs_statfs(struct super_block *sb, struct statfs *buf)
476 {
477         struct statfs tmp;
478         int err;
479
480         ENTRY;
481
482         err = OPS(sb,statfs)(ID(sb), &tmp);
483         if ( err ) { 
484                 printk(__FUNCTION__ ": obd_statfs fails (%d)\n", err);
485                 return err;
486         }
487         memcpy(buf, &tmp, sizeof(*buf));
488         CDEBUG(D_SUPER, "statfs returns avail %ld\n", tmp.f_bavail);
489         EXIT;
490
491         return err; 
492 }
493
494 static inline void obdfs_read_inode2(struct inode *inode, void *opaque)
495 {
496         struct obdo *oa = opaque; 
497         
498         ENTRY;
499         obdfs_to_inode(inode, oa); 
500
501         INIT_LIST_HEAD(obdfs_iplist(inode)); /* list of dirty pages on inode */
502         INIT_LIST_HEAD(obdfs_islist(inode)); /* list of inodes in superblock */
503
504         /* OIDEBUG(inode); */
505
506         if (S_ISREG(inode->i_mode)) {
507                 inode->i_op = &obdfs_file_inode_operations;
508                 inode->i_fop = &obdfs_file_operations;
509                 inode->i_mapping->a_ops = &obdfs_aops;
510                 EXIT;
511         } else if (S_ISDIR(inode->i_mode)) {
512                 inode->i_op = &obdfs_dir_inode_operations;
513                 inode->i_fop = &obdfs_dir_operations; 
514                 inode->i_mapping->a_ops = &obdfs_aops;
515                 EXIT;
516         } else if (S_ISLNK(inode->i_mode)) {
517                 if (inode->i_blocks) { 
518                         inode->i_op = &obdfs_symlink_inode_operations;
519                         inode->i_mapping->a_ops = &obdfs_aops;
520                 }else {
521                         inode->i_op = &obdfs_fast_symlink_inode_operations;
522                 }
523                 EXIT;
524         } else {
525                 init_special_inode(inode, inode->i_mode,
526                                    ((int *)obdfs_i2info(inode)->oi_inline)[0]);
527         }
528
529         EXIT;
530         return;
531 }
532
533 /* exported operations */
534 struct super_operations obdfs_super_operations =
535 {
536         read_inode2: obdfs_read_inode2,
537         put_inode: obdfs_put_inode,
538         delete_inode: obdfs_delete_inode,
539         put_super: obdfs_put_super,
540         statfs: obdfs_statfs
541 };
542
543
544 struct file_system_type obdfs_fs_type = {
545    "obdfs", 0, obdfs_read_super, NULL
546 };
547
548 int init_obdfs(void)
549 {
550         int err;
551
552         printk(KERN_INFO "OBDFS v0.1, braam@stelias.com\n");
553
554         obdfs_sysctl_init();
555
556         INIT_LIST_HEAD(&obdfs_super_list);
557         //err = obdfs_init_pgrqcache();
558         //if (err)
559         //return err;
560
561         //obdfs_flushd_init();
562         return register_filesystem(&obdfs_fs_type);
563 }
564
565
566
567
568 #ifdef MODULE
569 int init_module(void)
570 {
571         return init_obdfs();
572 }
573
574 void cleanup_module(void)
575 {
576         ENTRY;
577
578         //obdfs_flushd_cleanup();
579         obdfs_sysctl_clean();
580         //obdfs_cleanup_pgrqcache();
581         unregister_filesystem(&obdfs_fs_type);
582         CDEBUG(D_MALLOC, "OBDFS mem used %ld\n", obd_memory);
583         EXIT;
584 }
585
586 #endif