Whamcloud - gitweb
Optimize write-full-page so that we don't read in a page full of data we
[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 (filter_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 (filter_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_open(struct obd_conn *conn, struct obdo *oa)
444 {
445         struct inode *inode;
446         /* ENTRY; */
447
448         if (!gen_client(conn))
449                 RETURN(-EINVAL);
450
451         if ( !(inode = filter_inode_from_obj(conn->oc_dev,
452                                              oa->o_id, oa->o_mode)) )
453                 RETURN(-ENOENT);
454
455         return 0;
456 } /* filter_open */
457
458 static int filter_close(struct obd_conn *conn, struct obdo *oa)
459 {
460         struct inode *inode;
461         /* ENTRY; */
462
463         if (!gen_client(conn))
464                 RETURN(-EINVAL);
465
466         if ( !(inode = filter_inode_from_obj(conn->oc_dev,
467                                              oa->o_id, oa->o_mode)) )
468                 RETURN(-ENOENT);
469
470         iput(inode);  /* for the close */
471         iput(inode);  /* for this call */
472         return 0;
473 } /* filter_close */
474
475 static int filter_create (struct obd_conn* conn, struct obdo *oa)
476 {
477         char name[64];
478         struct obd_run_ctxt saved;
479         struct file *file;
480         int mode;
481         struct obd_device *obddev = conn->oc_dev;
482         struct iattr;
483         ENTRY;
484
485         if (!gen_client(conn)) {
486                 CERROR("invalid client %u\n", conn->oc_id);
487                 return -EINVAL;
488         }
489
490         oa->o_id = filter_next_id(conn->oc_dev);
491         if ( !(oa->o_mode && S_IFMT) ) { 
492                 CERROR("filter obd: no type!\n");
493                 return -ENOENT;
494         }
495
496         filter_id(name, oa->o_id, oa->o_mode);
497         push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
498         mode = oa->o_mode;
499         mode &= ~S_IFMT;
500         mode |= S_IFREG; 
501         file = filp_open(name, O_RDONLY | O_CREAT, mode);
502         pop_ctxt(&saved);
503         if (IS_ERR(file)) { 
504                 CERROR("Error mknod obj %s, err %ld\n", name, PTR_ERR(file));
505                 return -ENOENT;
506         }
507         filp_close(file, 0);
508         
509         /* Set flags for fields we have set in ext2_new_inode */
510         oa->o_valid |= OBD_MD_FLID | OBD_MD_FLBLKSZ | OBD_MD_FLBLOCKS |
511                  OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME |
512                  OBD_MD_FLUID | OBD_MD_FLGID;
513         return 0;
514 }
515
516 static int filter_destroy(struct obd_conn *conn, struct obdo *oa)
517 {
518         struct obd_device * obddev;
519         struct obd_client * cli;
520         struct inode * inode;
521         struct file *dir;
522         struct file *object;
523         int rc;
524         struct obd_run_ctxt saved;
525
526         if (!(cli = gen_client(conn))) {
527                 CERROR("invalid client %u\n", conn->oc_id);
528                 EXIT;
529                 return -EINVAL;
530         }
531
532         obddev = conn->oc_dev;
533         object = filter_obj_open(obddev, oa->o_id, oa->o_mode);
534         if (!object || IS_ERR(object)) { 
535                 EXIT;
536                 return -ENOENT;
537         }
538         
539         inode = object->f_dentry->d_inode;
540         inode->i_nlink = 1;
541         inode->i_mode = 010000;
542
543         push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
544         dir = filter_parent(oa->o_id, oa->o_mode);
545         if (IS_ERR(dir)) {
546                 rc = PTR_ERR(dir);
547                 EXIT;
548                 goto out;
549         }
550         dget(dir->f_dentry); 
551         dget(object->f_dentry);
552         rc = vfs_unlink(dir->f_dentry->d_inode, object->f_dentry);
553
554         filp_close(dir, 0);
555         filp_close(object, 0);
556 out:
557         pop_ctxt(&saved);
558         EXIT;
559         return rc;
560 }
561
562 static int filter_truncate(struct obd_conn *conn, struct obdo *oa, obd_size count,
563                          obd_off offset)
564 {
565         int error;
566
567         error = filter_setattr(conn, oa);
568         oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLCTIME | OBD_MD_FLMTIME;
569
570         EXIT;
571         return error;
572 }
573
574 /* buffer must lie in user memory here */
575 static int filter_read(struct obd_conn *conn, struct obdo *oa, char *buf,
576                         obd_size *count, obd_off offset)
577 {
578         struct file * file;
579         unsigned long retval;
580         int err;
581
582         if (!gen_client(conn)) {
583                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
584                 EXIT;
585                 return -EINVAL;
586         }
587
588         file = filter_obj_open(conn->oc_dev, oa->o_id, oa->o_mode); 
589         if (!file || IS_ERR(file)) { 
590                 EXIT;
591                 return -PTR_ERR(file);
592         }
593
594         /* count doubles as retval */
595         retval = file->f_op->read(file, buf, *count, &offset);
596         filp_close(file, 0);
597
598         if ( retval >= 0 ) {
599                 err = 0;
600                 *count = retval;
601         } else {
602                 err = retval;
603                 *count = 0;
604         }
605
606         return err;
607 }
608
609
610 /* buffer must lie in user memory here */
611 static int filter_write(struct obd_conn *conn, struct obdo *oa, char *buf, 
612                          obd_size *count, obd_off offset)
613 {
614         int err;
615         struct file * file;
616         unsigned long retval;
617
618         ENTRY;
619         if (!gen_client(conn)) {
620                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
621                 EXIT;
622                 return -EINVAL;
623         }
624
625         file = filter_obj_open(conn->oc_dev, oa->o_id, oa->o_mode); 
626         if (!file || IS_ERR(file)) { 
627                 EXIT;
628                 return -PTR_ERR(file);
629         }
630
631         /* count doubles as retval */
632         retval = file->f_op->write(file, buf, *count, &offset);
633         filp_close(file, 0);
634
635         if ( retval >= 0 ) {
636                 err = 0;
637                 *count = retval;
638                 EXIT;
639         } else {
640                 err = retval;
641                 *count = 0;
642                 EXIT;
643         }
644
645         return err;
646 } /* filter_write */
647
648 static int filter_pgcache_brw(int rw, struct obd_conn *conn, 
649                                obd_count num_oa,
650                                struct obdo **oa, 
651                                obd_count *oa_bufs, 
652                                struct page **pages,
653                                obd_size *count, 
654                                obd_off *offset, 
655                                obd_flag *flags)
656 {
657         struct super_block      *sb;
658         mm_segment_t oldfs;
659         int                      onum;          /* index to oas */
660         int                      pnum;          /* index to pages (bufs) */
661         unsigned long            retval;
662         int                      error;
663         struct file *file;
664
665         ENTRY;
666
667         if (!gen_client(conn)) {
668                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
669                 EXIT;
670                 return -EINVAL;
671         }
672
673         sb = conn->oc_dev->u.filter.fo_sb;
674         oldfs = get_fs();
675         set_fs(KERNEL_DS); 
676
677         pnum = 0; /* pnum indexes buf 0..num_pages */
678         for (onum = 0; onum < num_oa; onum++) {
679                 int              pg;
680
681                 file = filter_obj_open(conn->oc_dev, oa[onum]->o_id, 
682                                        oa[onum]->o_mode); 
683                 if (!file || IS_ERR(file)) { 
684                         EXIT;
685                         error = -ENOENT;
686                         goto ERROR;
687                 }
688
689                 /* count doubles as retval */
690                 for (pg = 0; pg < oa_bufs[onum]; pg++) {
691                         CDEBUG(D_INODE, "OP %d obdo no/pno: (%d,%d) (%ld,%ld) off count (%Ld,%Ld)\n", 
692                                rw, onum, pnum, file->f_dentry->d_inode->i_ino,
693                                (unsigned long)offset[pnum] >> PAGE_CACHE_SHIFT,
694                                offset[pnum], count[pnum]);
695                         if (rw == WRITE) { 
696                                 loff_t off; 
697                                 char *buffer;
698                                 off = offset[pnum]; 
699                                 buffer = kmap(pages[pnum]); 
700                                 retval = file->f_op->write(file, buffer, count[pnum], &off);
701                                 kunmap(pages[pnum]);
702                                 CDEBUG(D_INODE, "retval %ld\n", retval); 
703                         } else { 
704                                 loff_t off = offset[pnum]; 
705                                 char *buffer = kmap(pages[pnum]);
706
707                                 if (off >= file->f_dentry->d_inode->i_size) {
708                                         memset(buffer, 0, count[pnum]);
709                                         retval = count[pnum];
710                                 } else {
711                                         retval = file->f_op->read(file, buffer, count[pnum], &off);
712                                 } 
713                                 kunmap(pages[pnum]);
714
715                                 if ( retval != count[pnum] ) {
716                                         filp_close(file, 0);
717                                         retval = -EIO;
718                                         EXIT;
719                                         goto ERROR;
720                                 }
721                                 CDEBUG(D_INODE, "retval %ld\n", retval); 
722                         }
723                         pnum++;
724                 }
725                 /* sizes and blocks are set by generic_file_write */
726                 /* ctimes/mtimes will follow with a setattr call */ 
727                 filp_close(file, 0);
728         }
729         
730         EXIT;
731  ERROR:
732         set_fs(oldfs);
733         error = (retval >= 0) ? 0 : retval;
734         return error;
735 }
736
737
738 struct inode *ioobj_to_inode(struct obd_conn *conn, struct obd_ioobj *o)
739 {
740         struct inode *inode = NULL;
741         struct super_block *sb = conn->oc_dev->u.ext2.e2_sb;
742
743         if (!sb || !sb->s_dev) {
744                 CDEBUG(D_SUPER, "fatal: device not initialized.\n");
745                 EXIT;
746                 return NULL;
747         }
748
749         if ( !o->ioo_id ) {
750                 CDEBUG(D_INODE, "fatal: invalid obdo %lu\n", (long)o->ioo_id);
751                 EXIT;
752                 return NULL;
753         }
754
755         inode = filter_inode_from_obj(conn->oc_dev, o->ioo_id, S_IFREG);
756         if (!inode || inode->i_nlink == 0 || is_bad_inode(inode)) {
757                 CERROR("from obdo - fatal: invalid inode %ld (%s).\n",
758                        (long)o->ioo_id, inode ? inode->i_nlink ? "bad inode" :
759                        "no links" : "NULL");
760                 if (inode)
761                         iput(inode);
762                 EXIT;
763                 return NULL;
764         }
765
766         return inode;
767 }
768
769 static int filter_preprw(int cmd, struct obd_conn *conn,
770                          int objcount, struct obd_ioobj *obj,
771                          int niocount, struct niobuf *nb,
772                          struct niobuf *res)
773 {
774         struct obd_ioobj *o = obj;
775         struct niobuf *b = nb;
776         struct niobuf *r = res;
777         int i;
778         ENTRY;
779
780         memset(res, 0, sizeof(*res) * niocount);
781
782         for (i = 0; i < objcount; i++, o++) {
783                 int j;
784                 for (j = 0; j < o->ioo_bufcnt; j++, b++, r++) {
785                         unsigned long index = b->offset >> PAGE_SHIFT;
786                         struct inode *inode = ioobj_to_inode(conn, o);
787                         struct page *page;
788
789                         /* FIXME: we need to iput all inodes on error */
790                         if (!inode)
791                                 RETURN(-EINVAL);
792
793                         if (cmd == OBD_BRW_WRITE)
794                                 page = lustre_get_page_write(inode, index);
795                         else
796                                 page = lustre_get_page_read(inode, index);
797                         if (IS_ERR(page))
798                                 RETURN(PTR_ERR(page));
799
800                         r->addr = (__u64)(unsigned long)page_address(page);
801                         r->offset = b->offset;
802                         r->page = page;
803                         r->len = PAGE_SIZE;
804                 }
805         }
806         return 0;
807 }
808
809 static int filter_commitrw(int cmd, struct obd_conn *conn,
810                            int objcount, struct obd_ioobj *obj,
811                            int niocount, struct niobuf *res)
812 {
813         struct obd_ioobj *o = obj;
814         struct niobuf *r = res;
815         int i;
816         ENTRY;
817
818         for (i = 0; i < objcount; i++, obj++) {
819                 int j;
820                 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
821                         struct page *page = r->page;
822
823                         if (!r->page)
824                                 LBUG();
825
826                         if (cmd == OBD_BRW_WRITE) {
827                                 int rc = lustre_commit_page(page, 0, PAGE_SIZE);
828
829                                 /* FIXME: still need to iput the other inodes */
830                                 if (rc)
831                                         RETURN(rc);
832                         } else
833                                 lustre_put_page(page);
834
835                         iput(page->mapping->host);
836                 }
837         }
838         RETURN(0);
839 }
840
841 static int filter_statfs (struct obd_conn *conn, struct statfs * statfs)
842 {
843         struct super_block *sb;
844         int err;
845
846         ENTRY;
847
848         if (!gen_client(conn)) {
849                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
850                 EXIT;
851                 return -EINVAL;
852         }
853
854         sb = conn->oc_dev->u.filter.fo_sb;
855
856         err = sb->s_op->statfs(sb, statfs);
857         EXIT;
858         return err;
859 } /* filter_statfs */
860
861
862 static int  filter_get_info(struct obd_conn *conn, obd_count keylen,
863                              void *key, obd_count *vallen, void **val)
864 {
865         struct obd_device *obddev;
866         struct obd_client * cli;
867         ENTRY;
868
869         if (!(cli = gen_client(conn))) {
870                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
871                 return -EINVAL;
872         }
873
874         obddev = conn->oc_dev;
875         
876         if ( keylen == strlen("blocksize") &&
877              memcmp(key, "blocksize", keylen) == 0 ) {
878                 *vallen = sizeof(int);
879                 *val = (void *)obddev->u.filter.fo_sb->s_blocksize;
880                 EXIT;
881                 return 0;
882         }
883
884         if ( keylen == strlen("blocksize_bits") &&
885              memcmp(key, "blocksize_bits", keylen) == 0 ){
886                 *vallen = sizeof(int);
887                 *val = (void *)(int)obddev->u.filter.fo_sb->s_blocksize_bits;
888                 EXIT;
889                 return 0;
890         }
891
892         if ( keylen == strlen("root_ino") &&
893              memcmp(key, "root_ino", keylen) == 0 ){
894                 *vallen = sizeof(int);
895                 *val = (void *)(int) FILTER_ROOTINO;
896                 EXIT;
897                 return 0;
898         }
899         
900         CDEBUG(D_IOCTL, "invalid key\n");
901         return -EINVAL;
902 }
903
904
905 struct obd_ops filter_obd_ops = {
906         o_iocontrol:   NULL,
907         o_get_info:    filter_get_info,
908         o_setup:       filter_setup,
909         o_cleanup:     filter_cleanup,
910         o_connect:     filter_connect,
911         o_disconnect:  filter_disconnect,
912         o_statfs:      filter_statfs,
913         o_getattr:     filter_getattr,
914         o_create:      filter_create,
915         o_setattr:     filter_setattr,
916         o_destroy:     filter_destroy,
917         o_open:        filter_open,
918         o_close:       filter_close,
919         o_read:        filter_read,
920         o_write:       filter_write,
921         o_brw:         filter_pgcache_brw,
922         o_punch:       filter_truncate,
923         o_preprw:      filter_preprw,
924         o_commitrw:    filter_commitrw
925 #if 0
926         o_preallocate: filter_preallocate_inodes,
927         o_migrate:     filter_migrate,
928         o_copy:        gen_copy_data,
929         o_iterate:     filter_iterate
930 #endif
931 };
932
933
934 static int __init obdfilter_init(void)
935 {
936         printk(KERN_INFO "Filtering OBD driver  v0.001, braam@clusterfs.com\n");
937         return obd_register_type(&filter_obd_ops, OBD_FILTER_DEVICENAME);
938 }
939
940 static void __exit obdfilter_exit(void)
941 {
942         obd_unregister_type(OBD_FILTER_DEVICENAME);
943 }
944
945 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
946 MODULE_DESCRIPTION("Lustre Filtering OBD driver v1.0");
947 MODULE_LICENSE("GPL"); 
948
949 module_init(obdfilter_init);
950 module_exit(obdfilter_exit);