Whamcloud - gitweb
- obd filter works over ext2, but bonnie++ is buggie
[fs/lustre-release.git] / lustre / obdfilter / filter.c
1 /*
2  *  linux/fs/ext2_obd/ext2_obd.c
3  *
4  * Copyright (C) 2001  Cluster File Systems, Inc.
5  *
6  * This code is issued under the GNU General Public License.
7  * See the file COPYING in this distribution
8  *
9  * by Peter Braam <braam@clusterfs.com>
10  */
11
12 #define EXPORT_SYMTAB
13
14 #include <linux/version.h>
15 #include <linux/module.h>
16 #include <linux/fs.h>
17 #include <linux/stat.h>
18 #include <linux/locks.h>
19 #include <linux/ext2_fs.h>
20 #include <linux/quotaops.h>
21 #include <asm/unistd.h>
22 #include <linux/obd_support.h>
23 #include <linux/obd_class.h>
24 #include <linux/obd_ext2.h>
25
26 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
27 long filter_memory;
28
29 void push_ctxt(struct run_ctxt *save, struct run_ctxt *new)
30
31         save->fs = get_fs();
32         save->pwd = dget(current->fs->pwd);
33         save->pwdmnt = mntget(current->fs->pwdmnt);
34
35         set_fs(new->fs);
36         set_fs_pwd(current->fs, new->pwdmnt, new->pwd);
37 }
38
39 void pop_ctxt(struct run_ctxt *saved)
40 {
41         set_fs(saved->fs);
42         set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
43
44         dput(saved->pwd);
45         mntput(saved->pwdmnt);
46 }
47
48 static void filter_prep(struct obd_device *obddev)
49 {
50         struct run_ctxt saved;
51         struct file *file;
52         struct inode *inode;
53         long rc;
54         int fd;
55         struct stat64 buf;
56         __u64 lastino = 2;
57
58         push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
59         rc = sys_mkdir("O", 0700);
60         rc = sys_mkdir("P", 0700);
61         rc = sys_mkdir("D", 0700);
62         rc = sys_mkdir("O/2", 0755);
63         if ( (fd = sys_open("D/status", O_RDWR | O_CREAT, 0700)) == -1 ) {
64                 printk("OBD filter: cannot create status file\n");
65                 goto out;
66         }
67         if ( (rc = sys_fstat64(fd, &buf, 0)) ) { 
68                 printk("OBD filter: cannot stat status file\n");
69                 goto out_close;
70         }
71         if (buf.st_size == 0) { 
72                 rc = sys_write(fd, (char *)&lastino, sizeof(lastino));
73                 if (rc != sizeof(lastino)) { 
74                         printk("OBD filter: error writing lastino\n");
75                         goto out_close;
76                 }
77         } else { 
78                 rc = sys_read(fd, (char *)&lastino, sizeof(lastino));
79                 if (rc != sizeof(lastino)) { 
80                         printk("OBD filter: error reading lastino\n");
81                         goto out_close;
82                 }
83         }
84         obddev->u.filter.fo_lastino = lastino;
85
86         /* this is also the moment to steal operations */
87         file = filp_open("D/status", O_RDONLY, 0);
88         if (!file || IS_ERR(file)) { 
89                 EXIT;
90                 goto out_close;
91         }
92         inode = file->f_dentry->d_inode;
93         obddev->u.filter.fo_fop = file->f_op;
94         obddev->u.filter.fo_iop = inode->i_op;
95         obddev->u.filter.fo_aops = inode->i_mapping->a_ops;
96         filp_close(file, 0);
97         
98  out_close:
99         rc = sys_close(fd);
100         if (rc) { 
101                 printk("OBD filter: cannot close status file\n");
102         }
103  out:
104         pop_ctxt(&saved);
105 }
106
107 static void filter_post(struct obd_device *obddev)
108 {
109         struct run_ctxt saved;
110         long rc;
111         int fd;
112
113         push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
114         if ( (fd = sys_open("D/status", O_RDWR | O_CREAT, 0700)) == -1 ) {
115                 printk("OBD filter: cannot create status file\n");
116                 goto out;
117         }
118         rc = sys_write(fd, (char *)&obddev->u.filter.fo_lastino, 
119                        sizeof(obddev->u.filter.fo_lastino));
120         if (rc != sizeof(sizeof(obddev->u.filter.fo_lastino)) ) { 
121                 printk("OBD filter: error writing lastino\n");
122         }
123
124         rc = sys_close(fd);
125         if (rc) { 
126                 printk("OBD filter: cannot close status file\n");
127         }
128  out:
129         pop_ctxt(&saved);
130 }
131
132
133 /* release per client resources */
134 static int filter_disconnect(struct obd_conn *conn)
135 {
136         /* XXX cleanup preallocated inodes */
137         return gen_disconnect(conn);
138 } /* ext2obd_disconnect */
139
140 /* mount the file system (secretly) */
141 static int filter_setup(struct obd_device *obddev, obd_count len,
142                         void *buf)
143                         
144 {
145         struct obd_ioctl_data* data = buf;
146         struct vfsmount *mnt;
147         int err; 
148         ENTRY;
149         
150         
151         mnt = do_kern_mount(data->ioc_inlbuf2, 0, 
152                             data->ioc_inlbuf1, NULL); 
153         err = PTR_ERR(mnt);
154         if (IS_ERR(mnt)) { 
155                 EXIT;
156                 return err;
157         }
158
159         obddev->u.filter.fo_sb = mnt->mnt_root->d_inode->i_sb;
160         if (!obddev->u.filter.fo_sb) {
161                 EXIT;
162                 return -ENODEV;
163         }
164
165         obddev->u.filter.fo_vfsmnt = mnt;
166         obddev->u.filter.fo_fstype = strdup(data->ioc_inlbuf2);
167
168         obddev->u.filter.fo_ctxt.pwdmnt = mnt;
169         obddev->u.filter.fo_ctxt.pwd = mnt->mnt_root;
170         obddev->u.filter.fo_ctxt.fs = KERNEL_DS;
171
172         filter_prep(obddev);
173         
174         MOD_INC_USE_COUNT;
175         EXIT; 
176         return 0;
177
178
179 static int filter_cleanup(struct obd_device * obddev)
180 {
181         struct super_block *sb;
182
183         ENTRY;
184
185         if ( !(obddev->obd_flags & OBD_SET_UP) ) {
186                 EXIT;
187                 return 0;
188         }
189
190         if ( !list_empty(&obddev->obd_gen_clients) ) {
191                 printk(KERN_WARNING __FUNCTION__ ": still has clients!\n");
192                 EXIT;
193                 return -EBUSY;
194         }
195
196         sb = obddev->u.filter.fo_sb;
197         if (!obddev->u.filter.fo_sb){
198                 EXIT;
199                 return 0;
200         }
201         filter_post(obddev);
202
203         unlock_kernel();
204         mntput(obddev->u.filter.fo_vfsmnt); 
205         obddev->u.filter.fo_sb = 0;
206         kfree(obddev->u.filter.fo_fstype);
207
208         lock_kernel();
209
210         MOD_DEC_USE_COUNT;
211         EXIT;
212         return 0;
213 }
214
215
216 static struct file *filter_obj_open(struct obd_device *obddev, 
217                                      struct obdo *oa)
218 {
219         struct file *file;
220         int error = 0;
221         char id[16];
222         struct run_ctxt saved;
223         struct super_block *sb;
224
225         sb = obddev->u.filter.fo_sb;
226         if (!sb || !sb->s_dev) {
227                 CDEBUG(D_SUPER, "fatal: device not initialized.\n");
228                 EXIT;
229                 return NULL;
230         }
231
232         if ( !oa->o_id ) {
233                 CDEBUG(D_INODE, "fatal: invalid obdo %lu\n", (long)oa->o_id);
234                 EXIT;
235                 return NULL;
236         }
237
238         sprintf(id, "O/%Ld", oa->o_id);
239         push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
240         file = filp_open(id , O_RDONLY, 0);
241         pop_ctxt(&saved);
242
243         if (IS_ERR(file)) { 
244                 error = PTR_ERR(file);
245                 file = NULL;
246         }
247         CDEBUG(D_INODE, "opening obdo %s\n", id);
248                 
249         if ( file ) {
250                 file->f_op = obddev->u.filter.fo_fop;
251                 file->f_dentry->d_inode->i_op = obddev->u.filter.fo_iop;
252                 file->f_dentry->d_inode->i_mapping->a_ops = obddev->u.filter.fo_aops;
253         } else { 
254                 printk("Error opening object %s,  error %d\n", id, error);
255         }
256         return file;
257 }
258
259 static struct inode *inode_from_obdo(struct obd_device *obddev, 
260                                      struct obdo *oa)
261 {
262         struct file *file;
263         struct inode *inode; 
264
265         file = filter_obj_open(obddev, oa);
266         if ( !file ) { 
267                 printk("inode_from_obdo failed\n"); 
268                 return NULL;
269         }
270
271         inode = iget(file->f_dentry->d_inode->i_sb, file->f_dentry->d_inode->i_ino); 
272         filp_close(file, 0);
273         return inode;
274 }
275
276 static inline void filter_from_inode(struct obdo *oa, struct inode *inode)
277 {
278         ENTRY;
279
280         CDEBUG(D_INFO, "src inode %ld, dst obdo %ld valid 0x%08x\n",
281                inode->i_ino, (long)oa->o_id, oa->o_valid);
282         obdo_from_inode(oa, inode);
283
284         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
285                 obd_rdev rdev = kdev_t_to_nr(inode->i_rdev);
286                 CDEBUG(D_INODE, "copying device %x from inode to obdo\n",
287                        rdev);
288                 *((obd_rdev *)oa->o_inline) = rdev;
289                 oa->o_obdflags |= OBD_FL_INLINEDATA;
290                 oa->o_valid |= OBD_MD_FLINLINE;
291         }
292
293 #if 0
294  else if (ext2obd_has_inline(inode)) {
295                 CDEBUG(D_INFO, "copying inline from inode to obdo\n");
296                 memcpy(oa->o_inline, inode->u.ext2_i.i_data,
297                        MIN(sizeof(inode->u.ext2_i.i_data),OBD_INLINESZ));
298                 oa->o_obdflags |= OBD_FL_INLINEDATA;
299                 oa->o_valid |= OBD_MD_FLINLINE;
300         }
301
302         if (ext2obd_has_obdmd(inode)) {
303                 /* XXX this will change when we don't store the obdmd in data */
304                 CDEBUG(D_INFO, "copying obdmd from inode to obdo\n");
305                 memcpy(oa->o_obdmd, inode->u.ext2_i.i_data,
306                        MIN(sizeof(inode->u.ext2_i.i_data),OBD_INLINESZ));
307                 oa->o_obdflags |= OBD_FL_OBDMDEXISTS;
308                 oa->o_valid |= OBD_MD_FLOBDMD;
309         }
310 #endif
311         EXIT;
312 }
313
314 static int filter_getattr(struct obd_conn *conn, struct obdo *oa)
315 {
316         struct inode *inode;
317
318         ENTRY;
319         if ( !gen_client(conn) ) {
320                 CDEBUG(D_IOCTL, "fatal: invalid client %u\n", conn->oc_id);
321                 EXIT;
322                 return -EINVAL;
323         }
324
325         if ( !(inode = inode_from_obdo(conn->oc_dev, oa)) ) { 
326                 EXIT;
327                 return -ENOENT;
328         }
329
330         oa->o_valid &= ~OBD_MD_FLID;
331         filter_from_inode(oa, inode);
332         
333         iput(inode);
334         EXIT;
335         return 0;
336
337
338 static int filter_setattr(struct obd_conn *conn, struct obdo *oa)
339 {
340         struct inode *inode;
341         struct iattr iattr;
342         int rc;
343         struct dentry de;
344
345         if (!gen_client(conn)) {
346                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
347                 return -EINVAL;
348         }
349
350         inode = inode_from_obdo(conn->oc_dev, oa); 
351         if ( !inode ) { 
352                 EXIT;
353                 return -ENOENT;
354         }
355
356         iattr_from_obdo(&iattr, oa);
357         de.d_inode = inode;
358         if ( inode->i_op->setattr ) {
359                 rc = inode->i_op->setattr(&de, &iattr);
360         } else { 
361                 rc = inode_setattr(inode, &iattr);
362         }
363
364         iput(inode);
365         EXIT;
366         return rc;
367 }
368
369 static int filter_create (struct obd_conn* conn, struct obdo *oa)
370 {
371         char name[64];
372         struct run_ctxt saved;
373         struct obd_device *obddev = conn->oc_dev;
374         struct iattr;
375         int rc;
376         ENTRY;
377
378         if (!gen_client(conn)) {
379                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
380                 return -EINVAL;
381         }
382         CDEBUG(D_IOCTL, "\n");
383
384         conn->oc_dev->u.filter.fo_lastino++;
385         oa->o_id = conn->oc_dev->u.filter.fo_lastino;
386         sprintf(name, "O/%Ld", oa->o_id);
387         push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
388         CDEBUG(D_IOCTL, "\n");
389         if (sys_mknod(name, 0100644, 0)) { 
390                 printk("Error mknod obj %s\n", name);
391                 return -ENOENT;
392         }
393         pop_ctxt(&saved);
394
395         CDEBUG(D_IOCTL, "\n");
396         rc = filter_setattr(conn, oa); 
397         if ( rc ) { 
398                 EXIT;
399                 return -EINVAL;
400         }
401         CDEBUG(D_IOCTL, "\n");
402         
403         /* Set flags for fields we have set in ext2_new_inode */
404         oa->o_valid |= OBD_MD_FLID | OBD_MD_FLBLKSZ | OBD_MD_FLBLOCKS |
405                  OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME |
406                  OBD_MD_FLUID | OBD_MD_FLGID;
407         return 0;
408 }
409
410 static int filter_destroy(struct obd_conn *conn, struct obdo *oa)
411 {
412         struct obd_device * obddev;
413         struct obd_client * cli;
414         struct inode * inode;
415         struct run_ctxt saved;
416         char id[128];
417
418         if (!(cli = gen_client(conn))) {
419                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
420                 EXIT;
421                 return -EINVAL;
422         }
423
424         obddev = conn->oc_dev;
425         inode = inode_from_obdo(obddev, oa);
426
427         if (!inode) { 
428                 EXIT;
429                 return -ENOENT;
430         }
431
432         inode->i_nlink = 1;
433         inode->i_mode = 010000;
434         iput(inode);
435
436         sprintf(id, "O/%Ld", oa->o_id);
437         push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
438         if (sys_unlink(id)) { 
439                 EXIT;
440                 return -EPERM;
441         }
442         pop_ctxt(&saved);
443
444         EXIT;
445         return 0;
446 }
447
448 /* buffer must lie in user memory here */
449 static int filter_read(struct obd_conn *conn, struct obdo *oa, char *buf,
450                         obd_size *count, obd_off offset)
451 {
452         struct file * file;
453         unsigned long retval;
454         int err;
455
456         if (!gen_client(conn)) {
457                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
458                 EXIT;
459                 return -EINVAL;
460         }
461
462         file = filter_obj_open(conn->oc_dev, oa); 
463         if (!file || IS_ERR(file)) { 
464                 EXIT;
465                 return -PTR_ERR(file);
466         }
467
468         /* count doubles as retval */
469         retval = file->f_op->read(file, buf, *count, &offset);
470         filp_close(file, 0);
471
472         if ( retval >= 0 ) {
473                 err = 0;
474                 *count = retval;
475         } else {
476                 err = retval;
477                 *count = 0;
478         }
479
480         return err;
481 } /* ext2obd_read */
482
483
484 /* buffer must lie in user memory here */
485 static int filter_write(struct obd_conn *conn, struct obdo *oa, char *buf, 
486                          obd_size *count, obd_off offset)
487 {
488         int err;
489         struct file * file;
490         unsigned long retval;
491
492         ENTRY;
493         if (!gen_client(conn)) {
494                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
495                 EXIT;
496                 return -EINVAL;
497         }
498
499         file = filter_obj_open(conn->oc_dev, oa); 
500         if (!file || IS_ERR(file)) { 
501                 EXIT;
502                 return -PTR_ERR(file);
503         }
504
505         /* count doubles as retval */
506         retval = file->f_op->write(file, buf, *count, &offset);
507         filp_close(file, 0);
508
509         if ( retval >= 0 ) {
510                 err = 0;
511                 *count = retval;
512                 EXIT;
513         } else {
514                 err = retval;
515                 *count = 0;
516                 EXIT;
517         }
518
519         return err;
520 } /* ext2obd_write */
521
522 static int filter_pgcache_brw(int rw, struct obd_conn *conn, 
523                                obd_count num_oa,
524                                struct obdo **oa, 
525                                obd_count *oa_bufs, 
526                                struct page **pages,
527                                obd_size *count, 
528                                obd_off *offset, 
529                                obd_flag *flags)
530 {
531         struct super_block      *sb;
532         mm_segment_t oldfs;
533         int                      onum;          /* index to oas */
534         int                      pnum;          /* index to pages (bufs) */
535         unsigned long            retval;
536         int                      error;
537         struct file *file;
538
539         ENTRY;
540
541         if (!gen_client(conn)) {
542                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
543                 EXIT;
544                 return -EINVAL;
545         }
546
547         sb = conn->oc_dev->u.filter.fo_sb;
548         oldfs = get_fs();
549         set_fs(KERNEL_DS); 
550
551         pnum = 0; /* pnum indexes buf 0..num_pages */
552         for (onum = 0; onum < num_oa; onum++) {
553                 int              pg;
554
555                 file = filter_obj_open(conn->oc_dev, oa[onum]); 
556                 if (!file || IS_ERR(file)) { 
557                         EXIT;
558                         error = -ENOENT;
559                         goto ERROR;
560                 }
561
562                 /* count doubles as retval */
563                 for (pg = 0; pg < oa_bufs[onum]; pg++) {
564                         CDEBUG(D_INODE, "OP %d obdo no/pno: (%d,%d) (%ld,%ld) off count (%Ld,%Ld)\n", 
565                                rw, onum, pnum, file->f_dentry->d_inode->i_ino,
566                                (unsigned long)offset[pnum] >> PAGE_CACHE_SHIFT,
567                                offset[pnum], count[pnum]);
568                         if (rw == WRITE) { 
569                                 loff_t off; 
570                                 char *buffer;
571                                 off = offset[pnum]; 
572                                 buffer = kmap(pages[pnum]); 
573                                 retval = file->f_op->write(file, buffer, count[pnum], &off);
574                                 kunmap(pages[pnum]);
575                                 CDEBUG(D_INODE, "retval %ld\n", retval); 
576                         } else { 
577                                 loff_t off = offset[pnum]; 
578                                 char *buffer = kmap(pages[pnum]);
579
580                                 if (off >= file->f_dentry->d_inode->i_size) {
581                                         memset(buffer, 0, count[pnum]);
582                                         retval = count[pnum];
583                                 } else {
584                                         retval = file->f_op->read(file, buffer, count[pnum], &off);
585                                 } 
586                                 kunmap(pages[pnum]);
587
588                                 if ( retval != count[pnum] ) {
589                                         filp_close(file, 0);
590                                         retval = -EIO;
591                                         EXIT;
592                                         goto ERROR;
593                                 }
594                                 CDEBUG(D_INODE, "retval %ld\n", retval); 
595                         }
596                         pnum++;
597                 }
598                 /* sizes and blocks are set by generic_file_write */
599                 /* ctimes/mtimes will follow with a setattr call */ 
600                 filp_close(file, 0);
601         }
602         
603         EXIT;
604  ERROR:
605         set_fs(oldfs);
606         error = (retval >= 0) ? 0 : retval;
607         return error;
608 }
609
610 static int filter_statfs (struct obd_conn *conn, struct statfs * statfs)
611 {
612         struct super_block *sb;
613         int err;
614
615         ENTRY;
616
617         if (!gen_client(conn)) {
618                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
619                 EXIT;
620                 return -EINVAL;
621         }
622
623         sb = conn->oc_dev->u.filter.fo_sb;
624
625         err = sb->s_op->statfs(sb, statfs);
626         EXIT;
627         return err;
628 } /* ext2obd_statfs */
629
630
631 static int  filter_get_info(struct obd_conn *conn, obd_count keylen,
632                              void *key, obd_count *vallen, void **val)
633 {
634         struct obd_device *obddev;
635         struct obd_client * cli;
636         ENTRY;
637
638         if (!(cli = gen_client(conn))) {
639                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
640                 return -EINVAL;
641         }
642
643         obddev = conn->oc_dev;
644         
645         if ( keylen == strlen("blocksize") &&
646              memcmp(key, "blocksize", keylen) == 0 ) {
647                 *vallen = sizeof(int);
648                 *val = (void *)obddev->u.filter.fo_sb->s_blocksize;
649                 EXIT;
650                 return 0;
651         }
652
653         if ( keylen == strlen("blocksize_bits") &&
654              memcmp(key, "blocksize_bits", keylen) == 0 ){
655                 *vallen = sizeof(int);
656                 *val = (void *)(int)obddev->u.filter.fo_sb->s_blocksize_bits;
657                 EXIT;
658                 return 0;
659         }
660
661         if ( keylen == strlen("root_ino") &&
662              memcmp(key, "root_ino", keylen) == 0 ){
663                 *vallen = sizeof(int);
664                 *val = (void *)(int)
665                         obddev->u.filter.fo_sb->s_root->d_inode->i_ino;
666                 EXIT;
667                 return 0;
668         }
669         
670         CDEBUG(D_IOCTL, "invalid key\n");
671         return -EINVAL;
672 }
673
674
675 struct obd_ops filter_obd_ops = {
676         o_iocontrol:   NULL,
677         o_get_info:    filter_get_info,
678         o_setup:       filter_setup,
679         o_cleanup:     filter_cleanup,
680         o_connect:     gen_connect,
681         o_disconnect:  filter_disconnect,
682         o_statfs:      filter_statfs,
683         o_getattr:     filter_getattr,
684         o_create:      filter_create,
685         o_setattr:     filter_setattr,
686         o_destroy:     filter_destroy,
687         o_read:        filter_read,
688         o_write:       filter_write,
689         o_brw:         filter_pgcache_brw,
690 #if 0
691         o_preallocate: ext2obd_preallocate_inodes,
692         o_setattr:     ext2obd_setattr,
693         o_punch:       ext2obd_punch,
694         o_migrate:     ext2obd_migrate,
695         o_copy:        gen_copy_data,
696         o_iterate:     ext2obd_iterate
697 #endif
698 };
699
700
701 #ifdef MODULE
702
703 void init_module(void)
704 {
705         printk(KERN_INFO "Filtering OBD driver  v0.001, braam@clusterfs.com\n");
706         obd_register_type(&filter_obd_ops, OBD_FILTER_DEVICENAME);
707 }
708
709 void cleanup_module(void)
710 {
711         obd_unregister_type(OBD_FILTER_DEVICENAME);
712         CDEBUG(D_MALLOC, "FILTER mem used %ld\n", filter_memory);
713 }
714
715 #endif