Whamcloud - gitweb
Because we now return an error code from filter_prep() we have to make sure
[fs/lustre-release.git] / lustre / obdfilter / filter.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/fs/ext2_obd/ext2_obd.c
5  *
6  * Copyright (C) 2001  Cluster File Systems, Inc.
7  *
8  * This code is issued under the GNU General Public License.
9  * See the file COPYING in this distribution
10  *
11  * by Peter Braam <braam@clusterfs.com>
12  */
13
14 #define EXPORT_SYMTAB
15
16 #include <linux/version.h>
17 #include <linux/module.h>
18 #include <linux/fs.h>
19 #include <linux/stat.h>
20 #include <linux/locks.h>
21 #include <linux/ext2_fs.h>
22 #include <linux/quotaops.h>
23 #include <asm/unistd.h>
24
25 #define DEBUG_SUBSYSTEM S_FILTER
26
27 #include <linux/obd_class.h>
28 #include <linux/obd_ext2.h>
29 #include <linux/obd_filter.h>
30
31 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
32 long filter_memory;
33
34 #define FILTER_ROOTINO 2
35
36 #define S_SHIFT 12
37 static char * obd_type_by_mode[S_IFMT >> S_SHIFT] = {
38         [0]                     "",
39         [S_IFREG >> S_SHIFT]    "R", 
40         [S_IFDIR >> S_SHIFT]    "D",
41         [S_IFCHR >> S_SHIFT]    "C",
42         [S_IFBLK >> S_SHIFT]    "B", 
43         [S_IFIFO >> S_SHIFT]    "F", 
44         [S_IFSOCK >> S_SHIFT]   "S",
45         [S_IFLNK >> S_SHIFT]    "L"
46 };
47
48
49 /* write the pathname into the string */
50 static void filter_id(char *buf, obd_id id, obd_mode mode)
51 {
52         sprintf(buf, "O/%s/%Ld", obd_type_by_mode[(mode & S_IFMT) >> S_SHIFT],
53                 id);
54 }
55
56 /* setup the object store with correct subdirectories */
57 static int filter_prep(struct obd_device *obddev)
58 {
59         struct obd_run_ctxt saved;
60         struct file *file;
61         struct inode *inode;
62         loff_t off;
63         int rc = 0;
64         char rootid[128];
65         __u64 lastino = 2;
66
67         push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
68         rc = simple_mkdir(current->fs->pwd, "O", 0700);
69         rc = simple_mkdir(current->fs->pwd, "P", 0700);
70         rc = simple_mkdir(current->fs->pwd, "D", 0700);
71         file = filp_open("O", O_RDONLY, 0);
72         if (IS_ERR(file)) {
73                 CERROR("cannot open O\n");
74                 GOTO(out, rc = PTR_ERR(file));
75         }
76         rc = simple_mkdir(file->f_dentry, "R", 0700);  /* regular */
77         rc = simple_mkdir(file->f_dentry, "D", 0700);  /* directory */
78         rc = simple_mkdir(file->f_dentry, "L", 0700);  /* symbolic links */
79         rc = simple_mkdir(file->f_dentry, "C", 0700);  /* character devices */
80         rc = simple_mkdir(file->f_dentry, "B", 0700);  /* block devices */
81         rc = simple_mkdir(file->f_dentry, "F", 0700);  /* fifo's */
82         rc = simple_mkdir(file->f_dentry, "S", 0700);  /* sockets */
83         filp_close(file, NULL);
84
85         filter_id(rootid, FILTER_ROOTINO, S_IFDIR);
86         file = filp_open(rootid, O_RDWR | O_CREAT, 00755);
87         if (IS_ERR(file)) {
88                 CERROR("OBD filter: cannot make root directory"); 
89                 GOTO(out, rc = PTR_ERR(file));
90         }
91         filp_close(file, 0);
92         /* FIXME: this is the same as the _file_ we just created? */
93         rc = simple_mkdir(current->fs->pwd, rootid, 0755);
94
95         file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
96         if ( !file || IS_ERR(file) ) {
97                 CERROR("OBD filter: cannot open/create status file\n");
98                 GOTO(out, rc = PTR_ERR(file));
99         }
100
101         /* steal operations */
102         inode = file->f_dentry->d_inode;
103         obddev->u.filter.fo_fop = file->f_op;
104         obddev->u.filter.fo_iop = inode->i_op;
105         obddev->u.filter.fo_aops = inode->i_mapping->a_ops;
106
107         off = 0;
108         if (inode->i_size == 0) {
109                 ssize_t retval = file->f_op->write(file, (char *)&lastino,
110                                                    sizeof(lastino), &off);
111                 if (retval != sizeof(lastino)) {
112                         CERROR("OBD filter: error writing lastino\n");
113                         GOTO(out, rc = -EIO);
114                 }
115         } else {
116                 ssize_t retval = file->f_op->read(file, (char *)&lastino,
117                                                   sizeof(lastino), &off);
118                 if (retval != sizeof(lastino)) {
119                         CERROR("OBD filter: error reading lastino\n");
120                         GOTO(out, rc = -EIO);
121                 }
122         }
123         obddev->u.filter.fo_lastino = lastino;
124         filp_close(file, 0); 
125
126         rc = 0;
127  out:
128         pop_ctxt(&saved);
129
130         return(rc);
131 }
132
133 /* cleanup the filter: write last used object id to status file */
134 static void filter_post(struct obd_device *obddev)
135 {
136         struct obd_run_ctxt saved;
137         long rc;
138         struct file *file;
139         loff_t off = 0; 
140
141         push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
142         file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
143         if ( !file || IS_ERR(file)) { 
144                 CERROR("OBD filter: cannot create status file\n");
145                 goto out;
146         }
147         rc = file->f_op->write(file, (char *)&obddev->u.filter.fo_lastino, 
148                        sizeof(obddev->u.filter.fo_lastino), &off);
149         if (rc != sizeof(obddev->u.filter.fo_lastino) ) { 
150                 CERROR("OBD filter: error writing lastino\n");
151         }
152
153         rc = filp_close(file, NULL); 
154         if (rc) { 
155                 CERROR("OBD filter: cannot close status file\n");
156         }
157  out:
158         pop_ctxt(&saved);
159 }
160
161
162 static __u64 filter_next_id(struct obd_device *obddev) 
163 {
164         __u64 id;
165         spin_lock(&obddev->u.filter.fo_lock);
166         obddev->u.filter.fo_lastino++;
167         id =    obddev->u.filter.fo_lastino;
168         spin_unlock(&obddev->u.filter.fo_lock);
169         return id;
170 }
171
172 /* how to get files, dentries, inodes from object id's */
173 static struct file *filter_obj_open(struct obd_device *obddev, 
174                                    __u64 id, __u32 type)
175 {
176         struct obd_run_ctxt saved;
177         char name[24];
178         struct super_block *sb;
179         struct file *file;
180         
181         sb = obddev->u.filter.fo_sb;
182         if (!sb || !sb->s_dev) {
183                 CDEBUG(D_SUPER, "fatal: device not initialized.\n");
184                 EXIT;
185                 return NULL;
186         }
187
188         if ( !id ) {
189                 CDEBUG(D_INODE, "fatal: invalid obdo %Lu\n", id);
190                 EXIT;
191                 return NULL;
192         }
193
194         if ( ! (type & S_IFMT) ) { 
195                 CERROR("OBD filter_obj_open, no type (%Ld), mode %o!\n", 
196                        id, type);
197         }
198
199         filter_id(name, id, type); 
200         push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
201         file = filp_open(name, O_RDONLY | O_LARGEFILE, 0);
202         pop_ctxt(&saved);
203
204         CDEBUG(D_INODE, "opening obdo %s\n", name);
205
206         return file;
207 }
208
209 static struct file *filter_parent(obd_id id, obd_mode mode)
210 {
211         char path[64];
212         struct file *file;
213
214         sprintf(path, "O/%s", obd_type_by_mode[(mode & S_IFMT) >> S_SHIFT]);
215
216         file = filp_open(path, O_RDONLY, 0); 
217         return file;
218 }
219
220
221 static struct inode *filter_inode_from_obj(struct obd_device *obddev, 
222                                      __u64 id, __u32 type)
223 {
224         struct file *file;
225         struct inode *inode; 
226
227         file = filter_obj_open(obddev, id, type);
228         if ( !file ) { 
229                 CERROR("filter_inode_from_obdo failed\n"); 
230                 return NULL;
231         }
232
233         inode = iget(file->f_dentry->d_inode->i_sb, 
234                      file->f_dentry->d_inode->i_ino); 
235         filp_close(file, 0);
236         return inode;
237 }
238
239 /* obd methods */
240 static int filter_connect(struct obd_conn *conn)
241 {
242         int rc;
243
244         MOD_INC_USE_COUNT;
245         rc = gen_connect(conn);
246
247         if (rc)
248                 MOD_DEC_USE_COUNT;
249
250         return rc;
251 }
252
253 static int filter_disconnect(struct obd_conn *conn)
254 {
255         int rc;
256
257         rc = gen_disconnect(conn);
258         if (!rc)
259                 MOD_DEC_USE_COUNT;
260
261         /* XXX cleanup preallocated inodes */
262         return rc;
263 }
264
265 /* mount the file system (secretly) */
266 static int filter_setup(struct obd_device *obddev, obd_count len, void *buf)
267 {
268         struct obd_ioctl_data* data = buf;
269         struct vfsmount *mnt;
270         int err = 0;
271         ENTRY;
272
273         MOD_INC_USE_COUNT;
274         mnt = do_kern_mount(data->ioc_inlbuf2, 0, data->ioc_inlbuf1, NULL);
275         err = PTR_ERR(mnt);
276         if (IS_ERR(mnt))
277                 GOTO(err_dec, err);
278
279         /* XXX is this even possible if do_kern_mount succeeded? */
280         obddev->u.filter.fo_sb = mnt->mnt_root->d_inode->i_sb;
281         if (!obddev->u.filter.fo_sb)
282                 GOTO(err_put, err = -ENODEV);
283
284         obddev->u.filter.fo_vfsmnt = mnt;
285         obddev->u.filter.fo_fstype = strdup(data->ioc_inlbuf2);
286
287         obddev->u.filter.fo_ctxt.pwdmnt = mnt;
288         obddev->u.filter.fo_ctxt.pwd = mnt->mnt_root;
289         obddev->u.filter.fo_ctxt.fs = KERNEL_DS;
290
291         err = filter_prep(obddev);
292         if (err)
293                 GOTO(err_kfree, err);
294         spin_lock_init(&obddev->u.filter.fo_lock);
295
296         RETURN(0);
297
298 err_kfree:
299         kfree(obddev->u.filter.fo_fstype);
300 err_put:
301         unlock_kernel();
302         mntput(obddev->u.filter.fo_vfsmnt);
303         obddev->u.filter.fo_sb = 0;
304         lock_kernel();
305
306 err_dec:
307         MOD_DEC_USE_COUNT;
308         return err;
309 }
310
311
312 static int filter_cleanup(struct obd_device * obddev)
313 {
314         struct super_block *sb;
315
316         ENTRY;
317
318         if ( !(obddev->obd_flags & OBD_SET_UP) )
319                 RETURN(0);
320
321         if ( !list_empty(&obddev->obd_gen_clients) ) {
322                 CERROR("still has clients!\n");
323                 RETURN(-EBUSY);
324         }
325
326         sb = obddev->u.filter.fo_sb;
327         if (!obddev->u.filter.fo_sb)
328                 RETURN(0);
329
330         filter_post(obddev);
331
332         unlock_kernel();
333         mntput(obddev->u.filter.fo_vfsmnt); 
334         obddev->u.filter.fo_sb = 0;
335         kfree(obddev->u.filter.fo_fstype);
336
337         lock_kernel();
338
339         MOD_DEC_USE_COUNT;
340         RETURN(0);
341 }
342
343
344 static inline void filter_from_inode(struct obdo *oa, struct inode *inode)
345 {
346         int type = oa->o_mode & S_IFMT;
347         ENTRY;
348
349         CDEBUG(D_INFO, "src inode %ld, dst obdo %ld valid 0x%08x\n",
350                inode->i_ino, (long)oa->o_id, oa->o_valid);
351         obdo_from_inode(oa, inode);
352         oa->o_mode &= ~S_IFMT;
353         oa->o_mode |= type;
354
355         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
356                 obd_rdev rdev = kdev_t_to_nr(inode->i_rdev);
357                 CDEBUG(D_INODE, "copying device %x from inode to obdo\n",
358                        rdev);
359                 *((obd_rdev *)oa->o_inline) = rdev;
360                 oa->o_obdflags |= OBD_FL_INLINEDATA;
361                 oa->o_valid |= OBD_MD_FLINLINE;
362         }
363
364 #if 0
365  else if (ext2obd_has_inline(inode)) {
366                 CDEBUG(D_INFO, "copying inline from inode to obdo\n");
367                 memcpy(oa->o_inline, inode->u.ext2_i.i_data,
368                        MIN(sizeof(inode->u.ext2_i.i_data),OBD_INLINESZ));
369                 oa->o_obdflags |= OBD_FL_INLINEDATA;
370                 oa->o_valid |= OBD_MD_FLINLINE;
371         }
372
373         if (ext2obd_has_obdmd(inode)) {
374                 /* XXX this will change when we don't store the obdmd in data */
375                 CDEBUG(D_INFO, "copying obdmd from inode to obdo\n");
376                 memcpy(oa->o_obdmd, inode->u.ext2_i.i_data,
377                        MIN(sizeof(inode->u.ext2_i.i_data),OBD_INLINESZ));
378                 oa->o_obdflags |= OBD_FL_OBDMDEXISTS;
379                 oa->o_valid |= OBD_MD_FLOBDMD;
380         }
381 #endif
382         EXIT;
383 }
384
385 static int filter_getattr(struct obd_conn *conn, struct obdo *oa)
386 {
387         struct inode *inode;
388
389         ENTRY;
390         if ( !gen_client(conn) ) {
391                 CDEBUG(D_IOCTL, "fatal: invalid client %u\n", conn->oc_id);
392                 EXIT;
393                 return -EINVAL;
394         }
395
396         if ( !(inode = filter_inode_from_obj(conn->oc_dev, 
397                                               oa->o_id, oa->o_mode)) ) { 
398                 EXIT;
399                 return -ENOENT;
400         }
401
402         oa->o_valid &= ~OBD_MD_FLID;
403         filter_from_inode(oa, inode);
404         
405         iput(inode);
406         EXIT;
407         return 0;
408
409
410 static int filter_setattr(struct obd_conn *conn, struct obdo *oa)
411 {
412         struct inode *inode;
413         struct iattr iattr;
414         int rc;
415         struct dentry de;
416
417         if (!gen_client(conn)) {
418                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
419                 return -EINVAL;
420         }
421
422         inode = filter_inode_from_obj(conn->oc_dev, oa->o_id, oa->o_mode); 
423         if ( !inode ) { 
424                 EXIT;
425                 return -ENOENT;
426         }
427
428         iattr_from_obdo(&iattr, oa);
429         iattr.ia_mode &= ~S_IFMT;
430         iattr.ia_mode |= S_IFREG;
431         de.d_inode = inode;
432         if ( inode->i_op->setattr ) {
433                 rc = inode->i_op->setattr(&de, &iattr);
434         } else { 
435                 rc = inode_setattr(inode, &iattr);
436         }
437
438         iput(inode);
439         EXIT;
440         return rc;
441 }
442
443 static int filter_create (struct obd_conn* conn, struct obdo *oa)
444 {
445         char name[64];
446         struct obd_run_ctxt saved;
447         struct file *file;
448         int mode;
449         struct obd_device *obddev = conn->oc_dev;
450         struct iattr;
451         ENTRY;
452
453         if (!gen_client(conn)) {
454                 CERROR("invalid client %u\n", conn->oc_id);
455                 return -EINVAL;
456         }
457
458         oa->o_id = filter_next_id(conn->oc_dev);
459         if ( !(oa->o_mode && S_IFMT) ) { 
460                 CERROR("filter obd: no type!\n");
461                 return -ENOENT;
462         }
463
464         filter_id(name, oa->o_id, oa->o_mode);
465         push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
466         mode = oa->o_mode;
467         mode &= ~S_IFMT;
468         mode |= S_IFREG; 
469         file = filp_open(name, O_RDONLY | O_CREAT, mode);
470         pop_ctxt(&saved);
471         if (IS_ERR(file)) { 
472                 CERROR("Error mknod obj %s, err %ld\n", name, PTR_ERR(file));
473                 return -ENOENT;
474         }
475         filp_close(file, 0);
476         
477         /* Set flags for fields we have set in ext2_new_inode */
478         oa->o_valid |= OBD_MD_FLID | OBD_MD_FLBLKSZ | OBD_MD_FLBLOCKS |
479                  OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME |
480                  OBD_MD_FLUID | OBD_MD_FLGID;
481         return 0;
482 }
483
484 static int filter_destroy(struct obd_conn *conn, struct obdo *oa)
485 {
486         struct obd_device * obddev;
487         struct obd_client * cli;
488         struct inode * inode;
489         struct file *dir;
490         struct file *object;
491         int rc;
492         struct obd_run_ctxt saved;
493
494         if (!(cli = gen_client(conn))) {
495                 CERROR("invalid client %u\n", conn->oc_id);
496                 EXIT;
497                 return -EINVAL;
498         }
499
500         obddev = conn->oc_dev;
501         object = filter_obj_open(obddev, oa->o_id, oa->o_mode);
502         if (!object || IS_ERR(object)) { 
503                 EXIT;
504                 return -ENOENT;
505         }
506         
507         inode = object->f_dentry->d_inode;
508         inode->i_nlink = 1;
509         inode->i_mode = 010000;
510
511         push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
512         dir = filter_parent(oa->o_id, oa->o_mode);
513         if (IS_ERR(dir)) {
514                 rc = PTR_ERR(dir);
515                 EXIT;
516                 goto out;
517         }
518         dget(dir->f_dentry); 
519         dget(object->f_dentry);
520         rc = vfs_unlink(dir->f_dentry->d_inode, object->f_dentry);
521
522         filp_close(dir, 0);
523         filp_close(object, 0);
524 out:
525         pop_ctxt(&saved);
526         EXIT;
527         return rc;
528 }
529
530 static int filter_truncate(struct obd_conn *conn, struct obdo *oa, obd_size count,
531                          obd_off offset)
532 {
533         int error;
534
535         error = filter_setattr(conn, oa);
536         oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLCTIME | OBD_MD_FLMTIME;
537
538         EXIT;
539         return error;
540 }
541
542 /* buffer must lie in user memory here */
543 static int filter_read(struct obd_conn *conn, struct obdo *oa, char *buf,
544                         obd_size *count, obd_off offset)
545 {
546         struct file * file;
547         unsigned long retval;
548         int err;
549
550         if (!gen_client(conn)) {
551                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
552                 EXIT;
553                 return -EINVAL;
554         }
555
556         file = filter_obj_open(conn->oc_dev, oa->o_id, oa->o_mode); 
557         if (!file || IS_ERR(file)) { 
558                 EXIT;
559                 return -PTR_ERR(file);
560         }
561
562         /* count doubles as retval */
563         retval = file->f_op->read(file, buf, *count, &offset);
564         filp_close(file, 0);
565
566         if ( retval >= 0 ) {
567                 err = 0;
568                 *count = retval;
569         } else {
570                 err = retval;
571                 *count = 0;
572         }
573
574         return err;
575 }
576
577
578 /* buffer must lie in user memory here */
579 static int filter_write(struct obd_conn *conn, struct obdo *oa, char *buf, 
580                          obd_size *count, obd_off offset)
581 {
582         int err;
583         struct file * file;
584         unsigned long retval;
585
586         ENTRY;
587         if (!gen_client(conn)) {
588                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
589                 EXIT;
590                 return -EINVAL;
591         }
592
593         file = filter_obj_open(conn->oc_dev, oa->o_id, oa->o_mode); 
594         if (!file || IS_ERR(file)) { 
595                 EXIT;
596                 return -PTR_ERR(file);
597         }
598
599         /* count doubles as retval */
600         retval = file->f_op->write(file, buf, *count, &offset);
601         filp_close(file, 0);
602
603         if ( retval >= 0 ) {
604                 err = 0;
605                 *count = retval;
606                 EXIT;
607         } else {
608                 err = retval;
609                 *count = 0;
610                 EXIT;
611         }
612
613         return err;
614 } /* ext2obd_write */
615
616 static int filter_pgcache_brw(int rw, struct obd_conn *conn, 
617                                obd_count num_oa,
618                                struct obdo **oa, 
619                                obd_count *oa_bufs, 
620                                struct page **pages,
621                                obd_size *count, 
622                                obd_off *offset, 
623                                obd_flag *flags)
624 {
625         struct super_block      *sb;
626         mm_segment_t oldfs;
627         int                      onum;          /* index to oas */
628         int                      pnum;          /* index to pages (bufs) */
629         unsigned long            retval;
630         int                      error;
631         struct file *file;
632
633         ENTRY;
634
635         if (!gen_client(conn)) {
636                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
637                 EXIT;
638                 return -EINVAL;
639         }
640
641         sb = conn->oc_dev->u.filter.fo_sb;
642         oldfs = get_fs();
643         set_fs(KERNEL_DS); 
644
645         pnum = 0; /* pnum indexes buf 0..num_pages */
646         for (onum = 0; onum < num_oa; onum++) {
647                 int              pg;
648
649                 file = filter_obj_open(conn->oc_dev, oa[onum]->o_id, 
650                                        oa[onum]->o_mode); 
651                 if (!file || IS_ERR(file)) { 
652                         EXIT;
653                         error = -ENOENT;
654                         goto ERROR;
655                 }
656
657                 /* count doubles as retval */
658                 for (pg = 0; pg < oa_bufs[onum]; pg++) {
659                         CDEBUG(D_INODE, "OP %d obdo no/pno: (%d,%d) (%ld,%ld) off count (%Ld,%Ld)\n", 
660                                rw, onum, pnum, file->f_dentry->d_inode->i_ino,
661                                (unsigned long)offset[pnum] >> PAGE_CACHE_SHIFT,
662                                offset[pnum], count[pnum]);
663                         if (rw == WRITE) { 
664                                 loff_t off; 
665                                 char *buffer;
666                                 off = offset[pnum]; 
667                                 buffer = kmap(pages[pnum]); 
668                                 retval = file->f_op->write(file, buffer, count[pnum], &off);
669                                 kunmap(pages[pnum]);
670                                 CDEBUG(D_INODE, "retval %ld\n", retval); 
671                         } else { 
672                                 loff_t off = offset[pnum]; 
673                                 char *buffer = kmap(pages[pnum]);
674
675                                 if (off >= file->f_dentry->d_inode->i_size) {
676                                         memset(buffer, 0, count[pnum]);
677                                         retval = count[pnum];
678                                 } else {
679                                         retval = file->f_op->read(file, buffer, count[pnum], &off);
680                                 } 
681                                 kunmap(pages[pnum]);
682
683                                 if ( retval != count[pnum] ) {
684                                         filp_close(file, 0);
685                                         retval = -EIO;
686                                         EXIT;
687                                         goto ERROR;
688                                 }
689                                 CDEBUG(D_INODE, "retval %ld\n", retval); 
690                         }
691                         pnum++;
692                 }
693                 /* sizes and blocks are set by generic_file_write */
694                 /* ctimes/mtimes will follow with a setattr call */ 
695                 filp_close(file, 0);
696         }
697         
698         EXIT;
699  ERROR:
700         set_fs(oldfs);
701         error = (retval >= 0) ? 0 : retval;
702         return error;
703 }
704
705
706 struct inode *ioobj_to_inode(struct obd_conn *conn, struct obd_ioobj *o)
707 {
708         struct inode *inode = NULL;
709         struct super_block *sb = conn->oc_dev->u.ext2.e2_sb;
710
711         if (!sb || !sb->s_dev) {
712                 CDEBUG(D_SUPER, "fatal: device not initialized.\n");
713                 EXIT;
714                 return NULL;
715         }
716
717         if ( !o->ioo_id ) {
718                 CDEBUG(D_INODE, "fatal: invalid obdo %lu\n", (long)o->ioo_id);
719                 EXIT;
720                 return NULL;
721         }
722
723         inode = filter_inode_from_obj(conn->oc_dev, o->ioo_id, S_IFREG);
724         if (!inode || inode->i_nlink == 0 || is_bad_inode(inode)) {
725                 CERROR("from obdo - fatal: invalid inode %ld (%s).\n",
726                        (long)o->ioo_id, inode ? inode->i_nlink ? "bad inode" :
727                        "no links" : "NULL");
728                 if (inode)
729                         iput(inode);
730                 EXIT;
731                 return NULL;
732         }
733
734         return inode;
735 }
736
737 static int filter_preprw(int cmd, struct obd_conn *conn,
738                          int objcount, struct obd_ioobj *obj,
739                          int niocount, struct niobuf *nb,
740                          struct niobuf *res)
741 {
742         struct obd_ioobj *o = obj;
743         struct niobuf *b = nb;
744         struct niobuf *r = res;
745         int i;
746         ENTRY;
747
748         memset(res, 0, sizeof(*res) * niocount);
749
750         for (i = 0; i < objcount; i++, o++) {
751                 int j;
752                 for (j = 0; j < o->ioo_bufcnt; j++, b++, r++) {
753                         struct inode *inode = ioobj_to_inode(conn, o);
754                         struct page *page;
755
756                         /* FIXME: we need to iput all inodes on error */
757                         if (!inode)
758                                 RETURN(-EINVAL);
759
760                         page = lustre_get_page(inode, b->offset >> PAGE_SHIFT);
761                         if (IS_ERR(page))
762                                 RETURN(PTR_ERR(page));
763
764                         if (cmd == OBD_BRW_WRITE) {
765                                 int rc = lustre_prepare_page(0, PAGE_SIZE,page);
766                                 if (rc)
767                                         CERROR("i %d j %d objcount %d bufcnt %d , rc %d, offset %Ld\n", i, j, objcount, o->ioo_bufcnt, rc, b->offset);
768                         }
769
770                         r->addr = (__u64)(unsigned long)page_address(page);
771                         r->offset = b->offset;
772                         r->page = page;
773                         r->len = PAGE_SIZE;
774                 }
775         }
776         return 0;
777 }
778
779 static int filter_commitrw(int cmd, struct obd_conn *conn,
780                            int objcount, struct obd_ioobj *obj,
781                            int niocount, struct niobuf *res)
782 {
783         struct obd_ioobj *o = obj;
784         struct niobuf *r = res;
785         int i;
786         ENTRY;
787
788         for (i = 0; i < objcount; i++, obj++) {
789                 int j;
790                 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
791                         struct page *page = r->page;
792
793                         if (!r->page)
794                                 LBUG();
795
796                         if (cmd == OBD_BRW_WRITE) {
797                                 int rc = lustre_commit_page(page, 0, PAGE_SIZE);
798                                 if (rc)
799                                         RETURN(rc);
800                         } else
801                                 lustre_put_page(page);
802
803                         iput(page->mapping->host);
804                 }
805         }
806         return 0;
807 }
808
809 static int filter_statfs (struct obd_conn *conn, struct statfs * statfs)
810 {
811         struct super_block *sb;
812         int err;
813
814         ENTRY;
815
816         if (!gen_client(conn)) {
817                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
818                 EXIT;
819                 return -EINVAL;
820         }
821
822         sb = conn->oc_dev->u.filter.fo_sb;
823
824         err = sb->s_op->statfs(sb, statfs);
825         EXIT;
826         return err;
827 } /* ext2obd_statfs */
828
829
830 static int  filter_get_info(struct obd_conn *conn, obd_count keylen,
831                              void *key, obd_count *vallen, void **val)
832 {
833         struct obd_device *obddev;
834         struct obd_client * cli;
835         ENTRY;
836
837         if (!(cli = gen_client(conn))) {
838                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
839                 return -EINVAL;
840         }
841
842         obddev = conn->oc_dev;
843         
844         if ( keylen == strlen("blocksize") &&
845              memcmp(key, "blocksize", keylen) == 0 ) {
846                 *vallen = sizeof(int);
847                 *val = (void *)obddev->u.filter.fo_sb->s_blocksize;
848                 EXIT;
849                 return 0;
850         }
851
852         if ( keylen == strlen("blocksize_bits") &&
853              memcmp(key, "blocksize_bits", keylen) == 0 ){
854                 *vallen = sizeof(int);
855                 *val = (void *)(int)obddev->u.filter.fo_sb->s_blocksize_bits;
856                 EXIT;
857                 return 0;
858         }
859
860         if ( keylen == strlen("root_ino") &&
861              memcmp(key, "root_ino", keylen) == 0 ){
862                 *vallen = sizeof(int);
863                 *val = (void *)(int) FILTER_ROOTINO;
864                 EXIT;
865                 return 0;
866         }
867         
868         CDEBUG(D_IOCTL, "invalid key\n");
869         return -EINVAL;
870 }
871
872
873 struct obd_ops filter_obd_ops = {
874         o_iocontrol:   NULL,
875         o_get_info:    filter_get_info,
876         o_setup:       filter_setup,
877         o_cleanup:     filter_cleanup,
878         o_connect:     filter_connect,
879         o_disconnect:  filter_disconnect,
880         o_statfs:      filter_statfs,
881         o_getattr:     filter_getattr,
882         o_create:      filter_create,
883         o_setattr:     filter_setattr,
884         o_destroy:     filter_destroy,
885         o_read:        filter_read,
886         o_write:       filter_write,
887         o_brw:         filter_pgcache_brw,
888         o_punch:       filter_truncate,
889         o_preprw:      filter_preprw,
890         o_commitrw:    filter_commitrw
891 #if 0
892         o_preallocate: ext2obd_preallocate_inodes,
893         o_migrate:     ext2obd_migrate,
894         o_copy:        gen_copy_data,
895         o_iterate:     ext2obd_iterate
896 #endif
897 };
898
899
900 static int __init obdfilter_init(void)
901 {
902         printk(KERN_INFO "Filtering OBD driver  v0.001, braam@clusterfs.com\n");
903         return obd_register_type(&filter_obd_ops, OBD_FILTER_DEVICENAME);
904 }
905
906 static void __exit obdfilter_exit(void)
907 {
908         obd_unregister_type(OBD_FILTER_DEVICENAME);
909 }
910
911 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
912 MODULE_DESCRIPTION("Lustre Filtering OBD driver v1.0");
913 MODULE_LICENSE("GPL"); 
914
915 module_init(obdfilter_init);
916 module_exit(obdfilter_exit);