Whamcloud - gitweb
Minor bug fixes to get export handles used by the DLM. I believe the
[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/filter/filter.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 #define DEBUG_SUBSYSTEM S_FILTER
16
17 #include <linux/module.h>
18 #include <linux/obd_filter.h>
19 #include <linux/ext3_jbd.h>
20 #include <linux/quotaops.h>
21
22 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
23 long filter_memory;
24
25 #define FILTER_ROOTINO 2
26
27 #define S_SHIFT 12
28 static char *obd_type_by_mode[S_IFMT >> S_SHIFT] = {
29         [0]                     NULL,
30         [S_IFREG >> S_SHIFT]    "R",
31         [S_IFDIR >> S_SHIFT]    "D",
32         [S_IFCHR >> S_SHIFT]    "C",
33         [S_IFBLK >> S_SHIFT]    "B",
34         [S_IFIFO >> S_SHIFT]    "F",
35         [S_IFSOCK >> S_SHIFT]   "S",
36         [S_IFLNK >> S_SHIFT]    "L"
37 };
38
39 static inline const char *obd_mode_to_type(int mode)
40 {
41         return obd_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
42 }
43
44 /* write the pathname into the string */
45 static int filter_id(char *buf, obd_id id, obd_mode mode)
46 {
47         return sprintf(buf, "O/%s/%Ld", obd_mode_to_type(mode),
48                        (unsigned long long)id);
49 }
50
51 /* setup the object store with correct subdirectories */
52 static int filter_prep(struct obd_device *obddev)
53 {
54         struct obd_run_ctxt saved;
55         struct filter_obd *filter = &obddev->u.filter;
56         struct dentry *dentry;
57         struct file *file;
58         struct inode *inode;
59         loff_t off;
60         int rc = 0;
61         char rootid[128];
62         __u64 lastino = 2;
63         int mode = 0;
64
65         push_ctxt(&saved, &filter->fo_ctxt);
66         dentry = simple_mkdir(current->fs->pwd, "O", 0700);
67         CDEBUG(D_INODE, "got/created O: %p\n", dentry);
68         if (IS_ERR(dentry)) {
69                 rc = PTR_ERR(dentry);
70                 CERROR("cannot open/create O: rc = %d\n", rc);
71                 GOTO(out, rc);
72         }
73         filter->fo_dentry_O = dentry;
74         dentry = simple_mkdir(current->fs->pwd, "P", 0700);
75         CDEBUG(D_INODE, "got/created P: %p\n", dentry);
76         if (IS_ERR(dentry)) {
77                 rc = PTR_ERR(dentry);
78                 CERROR("cannot open/create P: rc = %d\n", rc);
79                 GOTO(out_O, rc);
80         }
81         CDEBUG(D_INODE, "putting P: %p, count = %d\n", dentry,
82                atomic_read(&dentry->d_count) - 1);
83         dput(dentry);
84         dentry = simple_mkdir(current->fs->pwd, "D", 0700);
85         CDEBUG(D_INODE, "got/created D: %p\n", dentry);
86         if (IS_ERR(dentry)) {
87                 rc = PTR_ERR(dentry);
88                 CERROR("cannot open/create D: rc = %d\n", rc);
89                 GOTO(out_O, rc);
90         }
91         CDEBUG(D_INODE, "putting D: %p, count = %d\n", dentry,
92                atomic_read(&dentry->d_count) - 1);
93         dput(dentry);
94
95         /*
96          * Create directories and/or get dentries for each object type.
97          * This saves us from having to do multiple lookups for each one.
98          */
99         for (mode = 0; mode < (S_IFMT >> S_SHIFT); mode++) {
100                 char *type = obd_type_by_mode[mode];
101
102                 if (!type) {
103                         filter->fo_dentry_O_mode[mode] = NULL;
104                         continue;
105                 }
106                 dentry = simple_mkdir(filter->fo_dentry_O, type, 0700);
107                 CDEBUG(D_INODE, "got/created O/%s: %p\n", type, dentry);
108                 if (IS_ERR(dentry)) {
109                         rc = PTR_ERR(dentry);
110                         CERROR("cannot create O/%s: rc = %d\n", type, rc);
111                         GOTO(out_O_mode, rc);
112                 }
113                 filter->fo_dentry_O_mode[mode] = dentry;
114         }
115
116         filter_id(rootid, FILTER_ROOTINO, S_IFDIR);
117         file = filp_open(rootid, O_RDWR | O_CREAT, 0755);
118         if (IS_ERR(file)) {
119                 rc = PTR_ERR(file);
120                 CERROR("OBD filter: cannot open/create root %s: rc = %d\n",
121                        rootid, rc);
122                 GOTO(out_O_mode, rc);
123         }
124         filp_close(file, 0);
125
126         file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
127         if ( !file || IS_ERR(file) ) {
128                 rc = PTR_ERR(file);
129                 CERROR("OBD filter: cannot open/create status %s: rc = %d\n",
130                        "D/status", rc);
131                 GOTO(out_O_mode, rc);
132         }
133
134         /* steal operations */
135         inode = file->f_dentry->d_inode;
136         filter->fo_fop = file->f_op;
137         filter->fo_iop = inode->i_op;
138         filter->fo_aops = inode->i_mapping->a_ops;
139
140         off = 0;
141         if (inode->i_size == 0) {
142                 ssize_t retval = file->f_op->write(file, (char *)&lastino,
143                                                    sizeof(lastino), &off);
144                 if (retval != sizeof(lastino)) {
145                         CDEBUG(D_INODE, "OBD filter: error writing lastino\n");
146                         filp_close(file, 0);
147                         GOTO(out_O_mode, rc = -EIO);
148                 }
149         } else {
150                 ssize_t retval = file->f_op->read(file, (char *)&lastino,
151                                                   sizeof(lastino), &off);
152                 if (retval != sizeof(lastino)) {
153                         CDEBUG(D_INODE, "OBD filter: error reading lastino\n");
154                         filp_close(file, 0);
155                         GOTO(out_O_mode, rc = -EIO);
156                 }
157         }
158         filter->fo_lastino = lastino;
159         filp_close(file, 0);
160
161         rc = 0;
162  out:
163         pop_ctxt(&saved);
164
165         return(rc);
166
167 out_O_mode:
168         while (mode-- > 0) {
169                 struct dentry *dentry = filter->fo_dentry_O_mode[mode];
170                 if (dentry) {
171                         CDEBUG(D_INODE, "putting O/%s: %p, count = %d\n",
172                                obd_type_by_mode[mode], dentry,
173                                atomic_read(&dentry->d_count) - 1);
174                         dput(dentry);
175                         filter->fo_dentry_O_mode[mode] = NULL;
176                 }
177         }
178 out_O:
179         CDEBUG(D_INODE, "putting O: %p, count = %d\n", filter->fo_dentry_O,
180                atomic_read(&filter->fo_dentry_O->d_count) - 1);
181         dput(filter->fo_dentry_O);
182         filter->fo_dentry_O = NULL;
183         goto out;
184 }
185
186 /* cleanup the filter: write last used object id to status file */
187 static void filter_post(struct obd_device *obddev)
188 {
189         struct obd_run_ctxt saved;
190         struct filter_obd *filter = &obddev->u.filter;
191         long rc;
192         struct file *file;
193         loff_t off = 0;
194         int mode;
195
196         push_ctxt(&saved, &filter->fo_ctxt);
197         file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
198         if (IS_ERR(file)) {
199                 CERROR("OBD filter: cannot create status file\n");
200                 goto out;
201         }
202         rc = file->f_op->write(file, (char *)&filter->fo_lastino,
203                        sizeof(filter->fo_lastino), &off);
204         if (rc != sizeof(filter->fo_lastino))
205                 CERROR("OBD filter: error writing lastino: rc = %ld\n", rc);
206
207         rc = filp_close(file, NULL);
208         if (rc)
209                 CERROR("OBD filter: cannot close status file: rc = %ld\n", rc);
210
211         for (mode = 0; mode < (S_IFMT >> S_SHIFT); mode++) {
212                 struct dentry *dentry = filter->fo_dentry_O_mode[mode];
213                 if (dentry) {
214                         CDEBUG(D_INODE, "putting O/%s: %p, count = %d\n",
215                                obd_type_by_mode[mode], dentry,
216                                atomic_read(&dentry->d_count) - 1);
217                         dput(dentry);
218                         filter->fo_dentry_O_mode[mode] = NULL;
219                 }
220         }
221         CDEBUG(D_INODE, "putting O: %p, count = %d\n", filter->fo_dentry_O,
222                atomic_read(&filter->fo_dentry_O->d_count) - 1);
223         dput(filter->fo_dentry_O);
224 out:
225         pop_ctxt(&saved);
226 }
227
228
229 static __u64 filter_next_id(struct obd_device *obddev)
230 {
231         __u64 id;
232         spin_lock(&obddev->u.filter.fo_lock);
233         obddev->u.filter.fo_lastino++;
234         id =    obddev->u.filter.fo_lastino;
235         spin_unlock(&obddev->u.filter.fo_lock);
236         return id;
237 }
238
239 /* how to get files, dentries, inodes from object id's */
240 /* parent i_sem is already held if needed for exclusivity */
241 static struct dentry *filter_fid2dentry(struct obd_device *obddev,
242                                         struct dentry *dparent,
243                                         __u64 id, __u32 type)
244 {
245         struct super_block *sb = obddev->u.filter.fo_sb;
246         struct dentry *dchild;
247         char name[32];
248         int len;
249         ENTRY;
250
251         if (!sb || !sb->s_dev) {
252                 CERROR("fatal: device not initialized.\n");
253                 RETURN(ERR_PTR(-ENXIO));
254         }
255
256         if (id == 0) {
257                 CERROR("fatal: invalid object #0\n");
258                 LBUG();
259                 RETURN(ERR_PTR(-ESTALE));
260         }
261
262         if (!(type & S_IFMT)) {
263                 CERROR("OBD %s, object %Lu has bad type: %o\n", __FUNCTION__,
264                        (unsigned long long)id, type);
265                 RETURN(ERR_PTR(-EINVAL));
266         }
267
268         len = sprintf(name, "%Ld", id);
269         CDEBUG(D_INODE, "opening object O/%s/%s\n", obd_mode_to_type(type),
270                name);
271         dchild = lookup_one_len(name, dparent, len);
272         CDEBUG(D_INODE, "got child obj O/%s/%s: %p, count = %d\n",
273                obd_mode_to_type(type), name, dchild,
274                atomic_read(&dchild->d_count));
275
276         if (IS_ERR(dchild)) {
277                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
278                 RETURN(dchild);
279         }
280
281         RETURN(dchild);
282 }
283
284 static struct file *filter_obj_open(struct obd_device *obddev,
285                                     __u64 id, __u32 type)
286 {
287         struct super_block *sb = obddev->u.filter.fo_sb;
288         struct obd_run_ctxt saved;
289         char name[24];
290         struct file *file;
291         ENTRY;
292
293         if (!sb || !sb->s_dev) {
294                 CERROR("fatal: device not initialized.\n");
295                 RETURN(ERR_PTR(-ENXIO));
296         }
297
298         if (!id) {
299                 CERROR("fatal: invalid obdo %Lu\n", (unsigned long long)id);
300                 RETURN(ERR_PTR(-ESTALE));
301         }
302
303         if (!(type & S_IFMT)) {
304                 CERROR("OBD %s, no type (%Ld), mode %o!\n", __FUNCTION__,
305                        (unsigned long long)id, type);
306                 RETURN(ERR_PTR(-EINVAL));
307         }
308
309         filter_id(name, id, type);
310         push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
311         file = filp_open(name, O_RDONLY | O_LARGEFILE, 0 /* type? */);
312         pop_ctxt(&saved);
313
314         CDEBUG(D_INODE, "opening obdo %s: rc = %p\n", name, file);
315
316         if (IS_ERR(file))
317                 file = NULL;
318         RETURN(file);
319 }
320
321 static struct dentry *filter_parent(struct obd_device *obddev, obd_mode mode)
322 {
323         struct filter_obd *filter = &obddev->u.filter;
324
325         return filter->fo_dentry_O_mode[(mode & S_IFMT) >> S_SHIFT];
326 }
327
328
329 static struct inode *filter_inode_from_obj(struct obd_device *obddev,
330                                            __u64 id, __u32 type)
331 {
332         struct dentry *dentry;
333         struct inode *inode;
334
335         dentry = filter_fid2dentry(obddev, filter_parent(obddev, type),
336                                    id, type);
337         if (IS_ERR(dentry)) {
338                 CERROR("%s: lookup failed: rc = %ld\n", __FUNCTION__,
339                        PTR_ERR(dentry));
340                 RETURN(NULL);
341         }
342
343         lock_kernel();
344         inode = iget(dentry->d_inode->i_sb, dentry->d_inode->i_ino);
345         unlock_kernel();
346         CDEBUG(D_INODE, "put child %p, count = %d\n", dentry,
347                atomic_read(&dentry->d_count) - 1);
348         dput(dentry);
349         CDEBUG(D_INODE, "got inode %p (%ld), count = %d\n", inode, inode->i_ino,
350                atomic_read(&inode->i_count));
351         return inode;
352 }
353
354 /* obd methods */
355 static int filter_connect(struct obd_conn *conn, struct obd_device *obd)
356 {
357         int rc;
358
359         MOD_INC_USE_COUNT;
360         rc = gen_connect(conn, obd);
361
362         if (rc)
363                 MOD_DEC_USE_COUNT;
364
365         return rc;
366 }
367
368 static int filter_disconnect(struct obd_conn *conn)
369 {
370         int rc;
371
372         rc = gen_disconnect(conn);
373         if (!rc)
374                 MOD_DEC_USE_COUNT;
375
376         /* XXX cleanup preallocated inodes */
377         return rc;
378 }
379
380 /* mount the file system (secretly) */
381 static int filter_setup(struct obd_device *obddev, obd_count len, void *buf)
382 {
383         struct obd_ioctl_data* data = buf;
384         struct filter_obd *filter;
385         struct vfsmount *mnt;
386         int err = 0;
387         ENTRY;
388
389         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
390                 RETURN(-EINVAL);
391
392         MOD_INC_USE_COUNT;
393         mnt = do_kern_mount(data->ioc_inlbuf2, 0, data->ioc_inlbuf1, NULL);
394         err = PTR_ERR(mnt);
395         if (IS_ERR(mnt))
396                 GOTO(err_dec, err);
397
398         filter = &obddev->u.filter;;
399         filter->fo_sb = mnt->mnt_root->d_inode->i_sb;
400         /* XXX is this even possible if do_kern_mount succeeded? */
401         if (!filter->fo_sb)
402                 GOTO(err_put, err = -ENODEV);
403
404         filter->fo_vfsmnt = mnt;
405         filter->fo_fstype = strdup(data->ioc_inlbuf2);
406
407         OBD_SET_CTXT_MAGIC(&filter->fo_ctxt);
408         filter->fo_ctxt.pwdmnt = mnt;
409         filter->fo_ctxt.pwd = mnt->mnt_root;
410         filter->fo_ctxt.fs = get_ds();
411
412         err = filter_prep(obddev);
413         if (err)
414                 GOTO(err_kfree, err);
415         spin_lock_init(&filter->fo_lock);
416
417         RETURN(0);
418
419 err_kfree:
420         kfree(filter->fo_fstype);
421 err_put:
422         unlock_kernel();
423         mntput(filter->fo_vfsmnt);
424         filter->fo_sb = 0;
425         lock_kernel();
426
427 err_dec:
428         MOD_DEC_USE_COUNT;
429         return err;
430 }
431
432
433 static int filter_cleanup(struct obd_device * obddev)
434 {
435         struct super_block *sb;
436         ENTRY;
437
438         if (!(obddev->obd_flags & OBD_SET_UP))
439                 RETURN(0);
440
441         if (!list_empty(&obddev->obd_exports)) {
442                 CERROR("still has clients!\n");
443                 RETURN(-EBUSY);
444         }
445
446         sb = obddev->u.filter.fo_sb;
447         if (!obddev->u.filter.fo_sb)
448                 RETURN(0);
449
450         filter_post(obddev);
451
452         shrink_dcache_parent(sb->s_root);
453         unlock_kernel();
454         mntput(obddev->u.filter.fo_vfsmnt);
455         obddev->u.filter.fo_sb = 0;
456         kfree(obddev->u.filter.fo_fstype);
457
458         lock_kernel();
459
460         MOD_DEC_USE_COUNT;
461         RETURN(0);
462 }
463
464
465 static inline void filter_from_inode(struct obdo *oa, struct inode *inode)
466 {
467         int type = oa->o_mode & S_IFMT;
468         ENTRY;
469
470         CDEBUG(D_INFO, "src inode %ld, dst obdo %ld valid 0x%08x\n",
471                inode->i_ino, (long)oa->o_id, oa->o_valid);
472         obdo_from_inode(oa, inode);
473         oa->o_mode &= ~S_IFMT;
474         oa->o_mode |= type;
475
476         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
477                 obd_rdev rdev = kdev_t_to_nr(inode->i_rdev);
478                 CDEBUG(D_INODE, "copying device %x from inode to obdo\n",
479                        rdev);
480                 *((obd_rdev *)oa->o_inline) = rdev;
481                 oa->o_obdflags |= OBD_FL_INLINEDATA;
482                 oa->o_valid |= OBD_MD_FLINLINE;
483         }
484
485 #if 0
486         else if (filter_has_inline(inode)) {
487                 CDEBUG(D_INFO, "copying inline from inode to obdo\n");
488                 memcpy(oa->o_inline, inode->u.ext2_i.i_data,
489                        MIN(sizeof(inode->u.ext2_i.i_data),OBD_INLINESZ));
490                 oa->o_obdflags |= OBD_FL_INLINEDATA;
491                 oa->o_valid |= OBD_MD_FLINLINE;
492         }
493
494         if (filter_has_obdmd(inode)) {
495                 /* XXX this will change when we don't store the obdmd in data */
496                 CDEBUG(D_INFO, "copying obdmd from inode to obdo\n");
497                 memcpy(oa->o_obdmd, inode->u.ext2_i.i_data,
498                        MIN(sizeof(inode->u.ext2_i.i_data),OBD_INLINESZ));
499                 oa->o_obdflags |= OBD_FL_OBDMDEXISTS;
500                 oa->o_valid |= OBD_MD_FLOBDMD;
501         }
502 #endif
503         EXIT;
504 }
505
506 static int filter_getattr(struct obd_conn *conn, struct obdo *oa)
507 {
508         struct obd_device *obddev = gen_conn2obd(conn);
509         struct dentry *dentry;
510         ENTRY;
511
512         if (!gen_client(conn)) {
513                 CDEBUG(D_IOCTL, "fatal: invalid client %Lx\n", conn->addr);
514                 RETURN(-EINVAL);
515         }
516
517         obddev = gen_conn2obd(conn);
518         dentry = filter_fid2dentry(obddev, filter_parent(obddev, oa->o_mode),
519                                    oa->o_id, oa->o_mode);
520         if (IS_ERR(dentry))
521                 RETURN(PTR_ERR(dentry));
522
523         oa->o_valid &= ~OBD_MD_FLID;
524         filter_from_inode(oa, dentry->d_inode);
525
526         dput(dentry);
527         RETURN(0);
528 }
529
530 static int filter_setattr(struct obd_conn *conn, struct obdo *oa)
531 {
532         struct obd_run_ctxt saved;
533         struct obd_device *obd = gen_conn2obd(conn);
534         struct dentry *dentry;
535         struct iattr iattr;
536         struct inode *inode;
537         int rc;
538         ENTRY;
539
540         dentry = filter_fid2dentry(obd, filter_parent(obd, oa->o_mode),
541                                    oa->o_id, oa->o_mode);
542         if (IS_ERR(dentry))
543                 RETURN(PTR_ERR(dentry));
544
545         inode = dentry->d_inode;
546         iattr_from_obdo(&iattr, oa);
547         iattr.ia_mode &= ~S_IFMT;
548         iattr.ia_mode |= S_IFREG;
549         lock_kernel();
550         if (iattr.ia_mode & ATTR_SIZE)
551                 down(&inode->i_sem);
552         push_ctxt(&saved, &obd->u.filter.fo_ctxt);
553         if (inode->i_op->setattr)
554                 rc = inode->i_op->setattr(dentry, &iattr);
555         else
556                 rc = inode_setattr(inode, &iattr);
557         pop_ctxt(&saved);
558         if (iattr.ia_mode & ATTR_SIZE) {
559                 up(&inode->i_sem);
560                 oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLCTIME | OBD_MD_FLMTIME;
561                 obdo_from_inode(oa, inode);
562         }
563         unlock_kernel();
564
565         CDEBUG(D_INODE, "put dentry %p, count = %d\n", inode,
566                atomic_read(&dentry->d_count) - 1);
567         dput(dentry);
568         RETURN(rc);
569 }
570
571 static int filter_open(struct obd_conn *conn, struct obdo *oa)
572 {
573         struct obd_device *obd;
574         struct dentry *dentry;
575         /* ENTRY; */
576
577         if (!gen_client(conn)) {
578                 CDEBUG(D_IOCTL, "fatal: invalid client %Lx\n", conn->addr);
579                 RETURN(-EINVAL);
580         }
581
582         obd = gen_conn2obd(conn);
583         dentry = filter_fid2dentry(obd, filter_parent(obd, oa->o_mode),
584                                    oa->o_id, oa->o_mode);
585         if (IS_ERR(dentry))
586                 RETURN(PTR_ERR(dentry));
587
588         return 0;
589 } /* filter_open */
590
591 static int filter_close(struct obd_conn *conn, struct obdo *oa)
592 {
593         struct obd_device *obd;
594         struct dentry *dentry;
595         /* ENTRY; */
596
597         if (!gen_client(conn)) {
598                 CDEBUG(D_IOCTL, "fatal: invalid client %Lx\n", conn->addr);
599                 RETURN(-EINVAL);
600         }
601
602         obd = gen_conn2obd(conn);
603         dentry = filter_fid2dentry(obd, filter_parent(obd, oa->o_mode),
604                                    oa->o_id, oa->o_mode);
605         if (IS_ERR(dentry))
606                 RETURN(PTR_ERR(dentry));
607
608         CDEBUG(D_INODE, "put dentry %p, count = %d\n", dentry,
609                atomic_read(&dentry->d_count) - 1);
610         dput(dentry);  /* for the close */
611         CDEBUG(D_INODE, "put dentry %p, count = %d\n", dentry,
612                atomic_read(&dentry->d_count) - 1);
613         dput(dentry);  /* for this call */
614         return 0;
615 } /* filter_close */
616
617 static int filter_create(struct obd_conn* conn, struct obdo *oa)
618 {
619         char name[64];
620         struct obd_run_ctxt saved;
621         struct file *file;
622         int mode;
623         struct obd_device *obd = gen_conn2obd(conn);
624         struct iattr;
625         ENTRY;
626
627         if (!gen_client(conn)) {
628                 CERROR("invalid client %Lx\n", conn->addr);
629                 return -EINVAL;
630         }
631
632         oa->o_id = filter_next_id(obd);
633         if (!(oa->o_mode && S_IFMT)) {
634                 CERROR("filter obd: no type!\n");
635                 return -ENOENT;
636         }
637
638         filter_id(name, oa->o_id, oa->o_mode);
639         mode = (oa->o_mode & ~S_IFMT) | S_IFREG;
640         push_ctxt(&saved, &obd->u.filter.fo_ctxt);
641         file = filp_open(name, O_RDONLY | O_CREAT, mode);
642         pop_ctxt(&saved);
643         if (IS_ERR(file)) {
644                 CERROR("Error mknod obj %s, err %ld\n", name, PTR_ERR(file));
645                 return -ENOENT;
646         }
647         filp_close(file, 0);
648
649         /* Set flags for fields we have set in the inode struct */
650         oa->o_valid |= OBD_MD_FLID | OBD_MD_FLBLKSZ | OBD_MD_FLBLOCKS |
651                  OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME |
652                  OBD_MD_FLUID | OBD_MD_FLGID;
653
654         /* XXX Hmm, shouldn't we copy the fields into the obdo here? */
655         return 0;
656 }
657
658 static int filter_destroy(struct obd_conn *conn, struct obdo *oa)
659 {
660         struct obd_run_ctxt saved;
661         struct obd_device *obd;
662         struct obd_export *export;
663         struct inode *inode;
664         struct dentry *dir_dentry, *object_dentry;
665         int rc;
666         ENTRY;
667
668         if (!(export = gen_client(conn))) {
669                 CERROR("invalid client %Lx\n", conn->addr);
670                 RETURN(-EINVAL);
671         }
672
673         CDEBUG(D_INODE, "destroying object %Ld\n",oa->o_id);
674         obd = gen_conn2obd(conn);
675
676         dir_dentry = filter_parent(obd, oa->o_mode);
677         down(&dir_dentry->d_inode->i_sem);
678
679         object_dentry = filter_fid2dentry(obd, dir_dentry, oa->o_id,
680                                           oa->o_mode);
681         if (IS_ERR(object_dentry))
682                 GOTO(out, rc = -ENOENT);
683
684         inode = object_dentry->d_inode;
685         if (inode->i_nlink != 1) {
686                 CERROR("destroying inode with nlink = %d\n", inode->i_nlink);
687                 inode->i_nlink = 1;
688         }
689         inode->i_mode = S_IFREG;
690
691         push_ctxt(&saved, &obd->u.filter.fo_ctxt);
692
693         rc = vfs_unlink(dir_dentry->d_inode, object_dentry);
694         pop_ctxt(&saved);
695         CDEBUG(D_INODE, "put child %p, count = %d\n", object_dentry,
696                atomic_read(&object_dentry->d_count) - 1);
697         dput(object_dentry);
698
699         EXIT;
700 out:
701         up(&dir_dentry->d_inode->i_sem);
702         return rc;
703 }
704
705 /* NB count and offset are used for punch, but not truncate */
706 static int filter_truncate(struct obd_conn *conn, struct obdo *oa,
707                            obd_size count, obd_off offset)
708 {
709         int error;
710         ENTRY;
711
712         CDEBUG(D_INODE, "calling truncate for object #%Ld, valid = %x, "
713                "o_size = %Ld\n", oa->o_id, oa->o_valid, oa->o_size);
714         error = filter_setattr(conn, oa);
715
716         RETURN(error);
717 }
718
719 static int filter_pgcache_brw(int cmd, struct obd_conn *conn, obd_count num_oa,
720                                struct obdo **oa, obd_count *oa_bufs,
721                                struct page **pages, obd_size *count,
722                                obd_off *offset, obd_flag *flags, void *callback)
723 {
724         struct obd_run_ctxt      saved;
725         struct super_block      *sb;
726         int                      onum;          /* index to oas */
727         int                      pnum;          /* index to pages (bufs) */
728         unsigned long            retval;
729         int                      error;
730         struct file             *file;
731         struct obd_device      *obd = gen_conn2obd(conn);
732         ENTRY;
733
734         if (!gen_client(conn)) {
735                 CDEBUG(D_IOCTL, "invalid client %Lx\n", conn->addr);
736                 RETURN(-EINVAL);
737         }
738
739         sb = obd->u.filter.fo_sb;
740         push_ctxt(&saved, &obd->u.filter.fo_ctxt);
741         pnum = 0; /* pnum indexes buf 0..num_pages */
742         for (onum = 0; onum < num_oa; onum++) {
743                 int pg;
744
745                 file = filter_obj_open(obd, oa[onum]->o_id, oa[onum]->o_mode);
746                 if (IS_ERR(file))
747                         GOTO(out, retval = PTR_ERR(file));
748
749                 /* count doubles as retval */
750                 for (pg = 0; pg < oa_bufs[onum]; pg++) {
751                         CDEBUG(D_INODE, "OP %d obdo no/pno: (%d,%d) (%ld,%ld) "
752                                "off count (%Ld,%Ld)\n",
753                                cmd, onum, pnum, file->f_dentry->d_inode->i_ino,
754                                (unsigned long)offset[pnum] >> PAGE_CACHE_SHIFT,
755                                (unsigned long long)offset[pnum],
756                                (unsigned long long)count[pnum]);
757                         if (cmd & OBD_BRW_WRITE) {
758                                 loff_t off;
759                                 char *buffer;
760                                 off = offset[pnum];
761                                 buffer = kmap(pages[pnum]);
762                                 retval = file->f_op->write(file, buffer, count[pnum], &off);
763                                 kunmap(pages[pnum]);
764                                 CDEBUG(D_INODE, "retval %ld\n", retval);
765                         } else {
766                                 loff_t off = offset[pnum];
767                                 char *buffer = kmap(pages[pnum]);
768
769                                 if (off >= file->f_dentry->d_inode->i_size) {
770                                         memset(buffer, 0, count[pnum]);
771                                         retval = count[pnum];
772                                 } else {
773                                         retval = file->f_op->read(file, buffer, count[pnum], &off);
774                                 }
775                                 kunmap(pages[pnum]);
776
777                                 if (retval != count[pnum]) {
778                                         filp_close(file, 0);
779                                         GOTO(out, retval = -EIO);
780                                 }
781                                 CDEBUG(D_INODE, "retval %ld\n", retval);
782                         }
783                         pnum++;
784                 }
785                 /* sizes and blocks are set by generic_file_write */
786                 /* ctimes/mtimes will follow with a setattr call */
787                 filp_close(file, 0);
788         }
789
790         EXIT;
791 out:
792         pop_ctxt(&saved);
793         error = (retval >= 0) ? 0 : retval;
794         return error;
795 }
796
797
798 struct inode *ioobj_to_inode(struct obd_conn *conn, struct obd_ioobj *o)
799 {
800         struct obd_device *obd = gen_conn2obd(conn);
801         struct super_block *sb = obd->u.filter.fo_sb;
802         struct inode *inode = NULL;
803         ENTRY;
804
805         if (!sb || !sb->s_dev) {
806                 CDEBUG(D_SUPER, "fatal: device not initialized.\n");
807                 RETURN(NULL);
808         }
809
810         if ( !o->ioo_id ) {
811                 CDEBUG(D_INODE, "fatal: invalid obdo %lu\n", (long)o->ioo_id);
812                 RETURN(NULL);
813         }
814
815         inode = filter_inode_from_obj(obd, o->ioo_id, S_IFREG);
816         if (!inode || inode->i_nlink == 0 || is_bad_inode(inode)) {
817                 CERROR("from obdo - fatal: invalid inode %ld (%s).\n",
818                        (long)o->ioo_id, inode ? inode->i_nlink ? "bad inode" :
819                        "no links" : "NULL");
820                 iput(inode);
821                 RETURN(NULL);
822         }
823
824         RETURN(inode);
825 }
826
827 /*
828  * Calculate the number of buffer credits needed to write multiple pages in
829  * a single ext3/extN transaction.  No, this shouldn't be here, but as yet
830  * ext3 doesn't have a nice API for calculating this sort of thing in advance.
831  *
832  * See comment above ext3_writepage_trans_blocks for details.  We assume
833  * no data journaling is being done, but it does allow for all of the pages
834  * being non-contiguous.  If we are guaranteed contiguous pages we could
835  * reduce the number of (d)indirect blocks a lot.
836  *
837  * With N blocks per page and P pages, for each inode we have at most:
838  * N*P indirect
839  * min(N*P, blocksize/4 + 1) dindirect blocks
840  * 1 tindirect
841  *
842  * For the entire filesystem, we have at most:
843  * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
844  * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
845  * 1 inode block
846  * 1 superblock
847  * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
848  */
849 static int ext3_credits_needed(struct super_block *sb, int objcount,
850                                struct obd_ioobj *obj)
851 {
852         struct obd_ioobj *o = obj;
853         int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
854         int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
855         int nbitmaps = 0;
856         int ngdblocks = 0;
857         int needed = objcount + 1;
858         int i;
859
860         for (i = 0; i < objcount; i++, o++) {
861                 int nblocks = o->ioo_bufcnt * blockpp;
862                 int ndindirect = min(nblocks, addrpp + 1);
863                 int nindir = nblocks + ndindirect + 1;
864
865                 nbitmaps += nindir + nblocks;
866                 ngdblocks += nindir + nblocks;
867
868                 needed += nindir;
869         }
870
871         if (nbitmaps > EXT3_SB(sb)->s_groups_count)
872                 nbitmaps = EXT3_SB(sb)->s_groups_count;
873         if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
874                 ngdblocks = EXT3_SB(sb)->s_gdb_count;
875
876         needed += nbitmaps + ngdblocks;
877
878 #ifdef CONFIG_QUOTA
879         /* We assume that there will be 1 bit set in s_dquot.flags for each
880          * quota file that is active.  This is at least true for now.
881          */
882         needed += hweight32(sb_any_quota_enabled(sb)) *
883                 EXT3_SINGLEDATA_TRANS_BLOCKS;
884 #endif
885
886         return needed;
887 }
888
889 /* We have to start a huge journal transaction here to hold all of the
890  * metadata for the pages being written here.  This is necessitated by
891  * the fact that we do lots of prepare_write operations before we do
892  * any of the matching commit_write operations, so even if we split
893  * up to use "smaller" transactions none of them could complete until
894  * all of them were opened.  By having a single journal transaction,
895  * we eliminate duplicate reservations for common blocks like the
896  * superblock and group descriptors or bitmaps.
897  *
898  * We will start the transaction here, but each prepare_write will
899  * add a refcount to the transaction, and each commit_write will
900  * remove a refcount.  The transaction will be closed when all of
901  * the pages have been written.
902  */
903 static void *ext3_filter_journal_start(struct filter_obd *filter,
904                                        int objcount, struct obd_ioobj *obj,
905                                        int niocount, struct niobuf_remote *nb)
906 {
907         journal_t *journal = NULL;
908         handle_t *handle = NULL;
909         int needed;
910
911         /* Assumes ext3 and extN have same sb_info layout, but avoids issues
912          * with having extN built properly before filterobd for now.
913          */
914         journal = EXT3_SB(filter->fo_sb)->s_journal;
915         needed = ext3_credits_needed(filter->fo_sb, objcount, obj);
916
917         /* The number of blocks we could _possibly_ dirty can very large.
918          * We reduce our request if it is absurd (and we couldn't get that
919          * many credits for a single handle anyways).
920          *
921          * At some point we have to limit the size of I/Os sent at one time,
922          * increase the size of the journal, or we have to calculate the
923          * actual journal requirements more carefully by checking all of
924          * the blocks instead of being maximally pessimistic.  It remains to
925          * be seen if this is a real problem or not.
926          */
927         if (needed > journal->j_max_transaction_buffers) {
928                 CERROR("want too many journal credits (%d) using %d instead\n",
929                        needed, journal->j_max_transaction_buffers);
930                 needed = journal->j_max_transaction_buffers;
931         }
932
933         handle = journal_start(journal, needed);
934         if (IS_ERR(handle))
935                 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
936                        PTR_ERR(handle));
937
938         return(handle);
939 }
940
941 static void *filter_journal_start(void **journal_save,
942                                   struct filter_obd *filter,
943                                   int objcount, struct obd_ioobj *obj,
944                                   int niocount, struct niobuf_remote *nb)
945 {
946         void *handle = NULL;
947
948         /* This may not be necessary - we probably never have a
949          * transaction started when we enter here, so we can
950          * remove the saving of the journal state entirely.
951          * For now leave it in just to see if it ever happens.
952          */
953         *journal_save = current->journal_info;
954         if (*journal_save) {
955                 CERROR("Already have handle %p???\n", *journal_save);
956                 LBUG();
957                 current->journal_info = NULL;
958         }
959
960         if (!strcmp(filter->fo_fstype, "ext3") ||
961             !strcmp(filter->fo_fstype, "extN"))
962                 handle = ext3_filter_journal_start(filter, objcount, obj,
963                                                    niocount, nb);
964         return handle;
965 }
966
967 static int ext3_filter_journal_stop(void *handle)
968 {
969         int rc;
970
971         /* We got a refcount on the handle for each call to prepare_write,
972          * so we can drop the "parent" handle here to avoid the need for
973          * osc to call back into filterobd to close the handle.  The
974          * remaining references will be dropped in commit_write.
975          */
976         rc = journal_stop((handle_t *)handle);
977
978         return rc;
979 }
980
981 static int filter_journal_stop(void *journal_save, struct filter_obd *filter,
982                                void *handle)
983 {
984         int rc = 0;
985
986         if (!strcmp(filter->fo_fstype, "ext3") ||
987             !strcmp(filter->fo_fstype, "extN"))
988                 rc = ext3_filter_journal_stop(handle);
989
990         if (rc)
991                 CERROR("error on journal stop: rc = %d\n", rc);
992
993         current->journal_info = journal_save;
994
995         return rc;
996 }
997
998 struct page *filter_get_page_write(struct inode *inode, unsigned long index,
999                                    struct niobuf_local *lnb)
1000 {
1001         struct address_space *mapping = inode->i_mapping;
1002         struct page *page;
1003         int rc;
1004
1005         //ASSERT_PAGE_INDEX(index, GOTO(err, rc = -EINVAL));
1006         page = grab_cache_page_nowait(mapping, index); /* locked page */
1007
1008         /* This page is currently locked, so get a temporary page instead */
1009         if (!page) {
1010                 unsigned long addr;
1011                 CDEBUG(D_PAGE, "ino %ld page %ld locked\n", inode->i_ino,index);
1012                 addr = __get_free_pages(GFP_KERNEL, 0); /* locked page */
1013                 if (!addr) {
1014                         CERROR("no memory for a temp page\n");
1015                         LBUG();
1016                         GOTO(err, rc = -ENOMEM);
1017                 }
1018                 page = virt_to_page(addr);
1019                 kmap(page);
1020                 page->index = index;
1021                 lnb->flags |= N_LOCAL_TEMP_PAGE;
1022         } else if (!IS_ERR(page)) {
1023                 /* Note: Called with "O" and "PAGE_SIZE" this is essentially
1024                  * a no-op for most filesystems, because we write the whole
1025                  * page.  For partial-page I/O this will read in the page.
1026                  */
1027                 kmap(page);
1028                 rc = mapping->a_ops->prepare_write(NULL, page, 0, PAGE_SIZE);
1029                 if (rc) {
1030                         CERROR("page index %lu, rc = %d\n", index, rc);
1031                         if (rc != -ENOSPC)
1032                                 LBUG();
1033                         GOTO(err_unlock, rc);
1034                 }
1035                 /* XXX not sure if we need this if we are overwriting page */
1036                 if (PageError(page)) {
1037                         CERROR("error on page index %lu, rc = %d\n", index, rc);
1038                         LBUG();
1039                         GOTO(err_unlock, rc = -EIO);
1040                 }
1041         }
1042         return page;
1043
1044 err_unlock:
1045         unlock_page(page);
1046         lustre_put_page(page);
1047 err:
1048         return ERR_PTR(rc);
1049 }
1050
1051 /*
1052  * We need to balance prepare_write() calls with commit_write() calls.
1053  * If the page has been prepared, but we have no data for it, we don't
1054  * want to overwrite valid data on disk, but we still need to zero out
1055  * data for space which was newly allocated.  Like part of what happens
1056  * in __block_prepare_write() for newly allocated blocks.
1057  *
1058  * XXX currently __block_prepare_write() creates buffers for all the
1059  *     pages, and the filesystems mark these buffers as BH_New if they
1060  *     were newly allocated from disk. We use the BH_New flag similarly.
1061  */
1062 static int filter_commit_write(struct page *page, unsigned from, unsigned to,
1063                                int err)
1064 {
1065         if (err) {
1066                 unsigned block_start, block_end;
1067                 struct buffer_head *bh, *head = page->buffers;
1068                 unsigned blocksize = head->b_size;
1069                 void *addr = page_address(page);
1070
1071                 /* Currently one buffer per page, but in the future... */
1072                 for (bh = head, block_start = 0; bh != head || !block_start;
1073                      block_start = block_end, bh = bh->b_this_page) {
1074                         block_end = block_start + blocksize;
1075                         if (buffer_new(bh))
1076                                 memset(addr + block_start, 0, blocksize);
1077                 }
1078         }
1079
1080         return lustre_commit_write(page, from, to);
1081 }
1082
1083 static int filter_preprw(int cmd, struct obd_conn *conn,
1084                          int objcount, struct obd_ioobj *obj,
1085                          int niocount, struct niobuf_remote *nb,
1086                          struct niobuf_local *res, void **desc_private)
1087 {
1088         struct obd_run_ctxt saved;
1089         struct obd_device *obd;
1090         struct obd_ioobj *o = obj;
1091         struct niobuf_remote *b = nb;
1092         struct niobuf_local *r = res;
1093         void *journal_save = NULL;
1094         int rc = 0;
1095         int i;
1096         ENTRY;
1097
1098         memset(res, 0, sizeof(*res) * niocount);
1099         obd = gen_conn2obd(conn);
1100
1101         push_ctxt(&saved, &obd->u.filter.fo_ctxt);
1102
1103         if (cmd & OBD_BRW_WRITE) {
1104                 *desc_private = filter_journal_start(&journal_save,
1105                                                      &obd->u.filter,
1106                                                      objcount, obj, niocount,
1107                                                      nb);
1108                 if (IS_ERR(*desc_private))
1109                         GOTO(out_ctxt, rc = PTR_ERR(*desc_private));
1110         }
1111
1112         for (i = 0; i < objcount; i++, o++) {
1113                 struct dentry *dentry;
1114                 struct inode *inode;
1115                 int j;
1116
1117                 dentry = filter_fid2dentry(obd,
1118                                            filter_parent(obd, S_IFREG),
1119                                            o->ioo_id, S_IFREG);
1120                 if (IS_ERR(dentry))
1121                         GOTO(out_clean, rc = PTR_ERR(dentry));
1122                 inode = dentry->d_inode;
1123                 if (!inode) {
1124                         CERROR("trying to BRW to non-existent file %Ld\n",
1125                                (unsigned long long)o->ioo_id);
1126                         dput(dentry);
1127                         GOTO(out_clean, rc = -ENOENT);
1128                 }
1129
1130                 for (j = 0; j < o->ioo_bufcnt; j++, b++, r++) {
1131                         unsigned long index = b->offset >> PAGE_SHIFT;
1132                         struct page *page;
1133
1134                         if (j == 0)
1135                                 r->dentry = dentry;
1136                         else
1137                                 r->dentry = dget(dentry);
1138
1139                         if (cmd & OBD_BRW_WRITE)
1140                                 page = filter_get_page_write(inode, index, r);
1141                         else
1142                                 page = lustre_get_page_read(inode, index);
1143
1144                         if (IS_ERR(page)) {
1145                                 dput(dentry);
1146                                 GOTO(out_clean, rc = PTR_ERR(page));
1147                         }
1148
1149                         r->addr = page_address(page);
1150                         r->offset = b->offset;
1151                         r->page = page;
1152                         r->len = PAGE_SIZE;
1153                 }
1154         }
1155
1156 out_stop:
1157         if (cmd & OBD_BRW_WRITE) {
1158                 int err = filter_journal_stop(journal_save, &obd->u.filter,
1159                                               *desc_private);
1160                 if (!rc)
1161                         rc = err;
1162         }
1163 out_ctxt:
1164         pop_ctxt(&saved);
1165         RETURN(rc);
1166 out_clean:
1167         while (r-- > res) {
1168                 dput(r->dentry);
1169                 if (cmd & OBD_BRW_WRITE)
1170                         filter_commit_write(r->page, 0, PAGE_SIZE, rc);
1171                 else
1172                         lustre_put_page(r->page);
1173         }
1174         goto out_stop;
1175 }
1176
1177 static int filter_write_locked_page(struct niobuf_local *lnb)
1178 {
1179         struct page *lpage;
1180         int rc;
1181
1182         lpage = lustre_get_page_write(lnb->dentry->d_inode, lnb->page->index);
1183         if (IS_ERR(lpage)) {
1184                 /* It is highly unlikely that we would ever get an error here.
1185                  * The page we want to get was previously locked, so it had to
1186                  * have already allocated the space, and we were just writing
1187                  * over the same data, so there would be no hole in the file.
1188                  *
1189                  * XXX: possibility of a race with truncate could exist, need
1190                  *      to check that.  There are no guarantees w.r.t.
1191                  *      write order even on a local filesystem, although the
1192                  *      normal response would be to return the number of bytes
1193                  *      successfully written and leave the rest to the app.
1194                  */
1195                 rc = PTR_ERR(lpage);
1196                 CERROR("error getting locked page index %ld: rc = %d\n",
1197                        lnb->page->index, rc);
1198                 GOTO(out, rc);
1199         }
1200
1201         memcpy(page_address(lpage), kmap(lnb->page), PAGE_SIZE);
1202         rc = lustre_commit_write(lpage, 0, PAGE_SIZE);
1203         if (rc)
1204                 CERROR("error committing locked page %ld: rc = %d\n",
1205                        lnb->page->index, rc);
1206 out:
1207         kunmap(lnb->page);
1208         __free_pages(lnb->page, 0);
1209         dput(lnb->dentry);
1210
1211         return rc;
1212 }
1213
1214 static int filter_commitrw(int cmd, struct obd_conn *conn,
1215                            int objcount, struct obd_ioobj *obj,
1216                            int niocount, struct niobuf_local *res,
1217                            void *private)
1218 {
1219         struct obd_run_ctxt saved;
1220         struct obd_ioobj *o = obj;
1221         struct niobuf_local *r = res;
1222         struct obd_device *obd = gen_conn2obd(conn); 
1223         void *journal_save;
1224         int found_locked = 0;
1225         int rc = 0;
1226         int i;
1227         ENTRY;
1228
1229         push_ctxt(&saved, &obd->u.filter.fo_ctxt);
1230         journal_save = current->journal_info;
1231         if (journal_save)
1232                 CERROR("Existing handle %p???\n", journal_save);
1233         current->journal_info = private;
1234         for (i = 0; i < objcount; i++, obj++) {
1235                 int j;
1236                 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
1237                         struct page *page = r->page;
1238
1239                         if (!page)
1240                                 LBUG();
1241
1242                         if (r->flags & N_LOCAL_TEMP_PAGE) {
1243                                 found_locked = 1;
1244                                 continue;
1245                         }
1246
1247                         if (cmd & OBD_BRW_WRITE) {
1248                                 int err = filter_commit_write(page, 0,
1249                                                               PAGE_SIZE, 0);
1250
1251                                 if (!rc)
1252                                         rc = err;
1253                         } else
1254                                 lustre_put_page(page);
1255
1256                         CDEBUG(D_INODE,
1257                                "put inode %p (%ld), count = %d, nlink = %d\n",
1258                                page->mapping->host,
1259                                page->mapping->host->i_ino,
1260                                atomic_read(&page->mapping->host->i_count) - 1,
1261                                page->mapping->host->i_nlink);
1262                         dput(r->dentry);
1263                 }
1264         }
1265         if (!found_locked)
1266                 goto out_ctxt;
1267
1268         for (i = 0; i < objcount; i++, obj++) {
1269                 int j;
1270                 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
1271                         int err;
1272                         if (!(r->flags & N_LOCAL_TEMP_PAGE))
1273                                 continue;
1274
1275                         err = filter_write_locked_page(r);
1276                         if (!rc)
1277                                 rc = err;
1278                 }
1279         }
1280
1281 out_ctxt:
1282         current->journal_info = journal_save;
1283         pop_ctxt(&saved);
1284         RETURN(0);
1285 }
1286
1287 static int filter_statfs(struct obd_conn *conn, struct statfs * statfs)
1288 {
1289         struct super_block *sb;
1290         int err;
1291         ENTRY;
1292
1293         if (!gen_client(conn)) {
1294                 CDEBUG(D_IOCTL, "invalid client %Lx\n", conn->addr);
1295                 RETURN(-EINVAL);
1296         }
1297
1298         sb = gen_conn2obd(conn)->u.filter.fo_sb;
1299
1300         err = sb->s_op->statfs(sb, statfs);
1301         RETURN(err);
1302 } /* filter_statfs */
1303
1304
1305 static int filter_get_info(struct obd_conn *conn, obd_count keylen,
1306                            void *key, obd_count *vallen, void **val)
1307 {
1308         struct obd_device *obd;
1309         struct obd_export * export;
1310         ENTRY;
1311
1312         if (!(export = gen_client(conn))) {
1313                 CDEBUG(D_IOCTL, "invalid client %Lx\n", conn->addr);
1314                 RETURN(-EINVAL);
1315         }
1316
1317         obd = gen_conn2obd(conn);
1318
1319         if ( keylen == strlen("blocksize") &&
1320              memcmp(key, "blocksize", keylen) == 0 ) {
1321                 *vallen = sizeof(long);
1322                 *val = (void *)(long)obd->u.filter.fo_sb->s_blocksize;
1323                 RETURN(0);
1324         }
1325
1326         if ( keylen == strlen("blocksize_bits") &&
1327              memcmp(key, "blocksize_bits", keylen) == 0 ){
1328                 *vallen = sizeof(long);
1329                 *val = (void *)(long)obd->u.filter.fo_sb->s_blocksize_bits;
1330                 RETURN(0);
1331         }
1332
1333         if ( keylen == strlen("root_ino") &&
1334              memcmp(key, "root_ino", keylen) == 0 ){
1335                 *vallen = sizeof(long);
1336                 *val = (void *)(long)FILTER_ROOTINO;
1337                 RETURN(0);
1338         }
1339
1340         CDEBUG(D_IOCTL, "invalid key\n");
1341         RETURN(-EINVAL);
1342 }
1343
1344
1345 static struct obd_ops filter_obd_ops = {
1346         o_get_info:    filter_get_info,
1347         o_setup:       filter_setup,
1348         o_cleanup:     filter_cleanup,
1349         o_connect:     filter_connect,
1350         o_disconnect:  filter_disconnect,
1351         o_statfs:      filter_statfs,
1352         o_getattr:     filter_getattr,
1353         o_create:      filter_create,
1354         o_setattr:     filter_setattr,
1355         o_destroy:     filter_destroy,
1356         o_open:        filter_open,
1357         o_close:       filter_close,
1358         o_brw:         filter_pgcache_brw,
1359         o_punch:       filter_truncate,
1360         o_preprw:      filter_preprw,
1361         o_commitrw:    filter_commitrw
1362 #if 0
1363         o_preallocate: filter_preallocate_inodes,
1364         o_migrate:     filter_migrate,
1365         o_copy:        gen_copy_data,
1366         o_iterate:     filter_iterate
1367 #endif
1368 };
1369
1370
1371 static int __init obdfilter_init(void)
1372 {
1373         printk(KERN_INFO "Filtering OBD driver  v0.001, braam@clusterfs.com\n");
1374         return obd_register_type(&filter_obd_ops, OBD_FILTER_DEVICENAME);
1375 }
1376
1377 static void __exit obdfilter_exit(void)
1378 {
1379         obd_unregister_type(OBD_FILTER_DEVICENAME);
1380 }
1381
1382 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
1383 MODULE_DESCRIPTION("Lustre Filtering OBD driver v1.0");
1384 MODULE_LICENSE("GPL");
1385
1386 module_init(obdfilter_init);
1387 module_exit(obdfilter_exit);