Whamcloud - gitweb
Fix o_valid and o_size for truncate through osc/ost/filterobd (on HEAD).
[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)
356 {
357         int rc;
358
359         MOD_INC_USE_COUNT;
360         rc = gen_connect(conn);
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         filter->fo_ctxt.pwdmnt = mnt;
408         filter->fo_ctxt.pwd = mnt->mnt_root;
409         filter->fo_ctxt.fs = KERNEL_DS;
410
411         err = filter_prep(obddev);
412         if (err)
413                 GOTO(err_kfree, err);
414         spin_lock_init(&filter->fo_lock);
415
416         RETURN(0);
417
418 err_kfree:
419         kfree(filter->fo_fstype);
420 err_put:
421         unlock_kernel();
422         mntput(filter->fo_vfsmnt);
423         filter->fo_sb = 0;
424         lock_kernel();
425
426 err_dec:
427         MOD_DEC_USE_COUNT;
428         return err;
429 }
430
431
432 static int filter_cleanup(struct obd_device * obddev)
433 {
434         struct super_block *sb;
435         ENTRY;
436
437         if (!(obddev->obd_flags & OBD_SET_UP))
438                 RETURN(0);
439
440         if (!list_empty(&obddev->obd_gen_clients)) {
441                 CERROR("still has clients!\n");
442                 RETURN(-EBUSY);
443         }
444
445         sb = obddev->u.filter.fo_sb;
446         if (!obddev->u.filter.fo_sb)
447                 RETURN(0);
448
449         filter_post(obddev);
450
451         shrink_dcache_parent(sb->s_root);
452         unlock_kernel();
453         mntput(obddev->u.filter.fo_vfsmnt);
454         obddev->u.filter.fo_sb = 0;
455         kfree(obddev->u.filter.fo_fstype);
456
457         lock_kernel();
458
459         MOD_DEC_USE_COUNT;
460         RETURN(0);
461 }
462
463
464 static inline void filter_from_inode(struct obdo *oa, struct inode *inode)
465 {
466         int type = oa->o_mode & S_IFMT;
467         ENTRY;
468
469         CDEBUG(D_INFO, "src inode %ld, dst obdo %ld valid 0x%08x\n",
470                inode->i_ino, (long)oa->o_id, oa->o_valid);
471         obdo_from_inode(oa, inode);
472         oa->o_mode &= ~S_IFMT;
473         oa->o_mode |= type;
474
475         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
476                 obd_rdev rdev = kdev_t_to_nr(inode->i_rdev);
477                 CDEBUG(D_INODE, "copying device %x from inode to obdo\n",
478                        rdev);
479                 *((obd_rdev *)oa->o_inline) = rdev;
480                 oa->o_obdflags |= OBD_FL_INLINEDATA;
481                 oa->o_valid |= OBD_MD_FLINLINE;
482         }
483
484 #if 0
485         else if (filter_has_inline(inode)) {
486                 CDEBUG(D_INFO, "copying inline from inode to obdo\n");
487                 memcpy(oa->o_inline, inode->u.ext2_i.i_data,
488                        MIN(sizeof(inode->u.ext2_i.i_data),OBD_INLINESZ));
489                 oa->o_obdflags |= OBD_FL_INLINEDATA;
490                 oa->o_valid |= OBD_MD_FLINLINE;
491         }
492
493         if (filter_has_obdmd(inode)) {
494                 /* XXX this will change when we don't store the obdmd in data */
495                 CDEBUG(D_INFO, "copying obdmd from inode to obdo\n");
496                 memcpy(oa->o_obdmd, inode->u.ext2_i.i_data,
497                        MIN(sizeof(inode->u.ext2_i.i_data),OBD_INLINESZ));
498                 oa->o_obdflags |= OBD_FL_OBDMDEXISTS;
499                 oa->o_valid |= OBD_MD_FLOBDMD;
500         }
501 #endif
502         EXIT;
503 }
504
505 static int filter_getattr(struct obd_conn *conn, struct obdo *oa)
506 {
507         struct obd_device *obddev;
508         struct dentry *dentry;
509         ENTRY;
510
511         if (!gen_client(conn)) {
512                 CDEBUG(D_IOCTL, "fatal: invalid client %u\n", conn->oc_id);
513                 RETURN(-EINVAL);
514         }
515
516         obddev = conn->oc_dev;
517         dentry = filter_fid2dentry(obddev, filter_parent(obddev, oa->o_mode),
518                                    oa->o_id, oa->o_mode);
519         if (IS_ERR(dentry))
520                 RETURN(PTR_ERR(dentry));
521
522         oa->o_valid &= ~OBD_MD_FLID;
523         filter_from_inode(oa, dentry->d_inode);
524
525         dput(dentry);
526         RETURN(0);
527 }
528
529 static int filter_setattr(struct obd_conn *conn, struct obdo *oa)
530 {
531         struct obd_run_ctxt saved;
532         struct obd_device *obddev;
533         struct dentry *dentry;
534         struct iattr iattr;
535         struct inode *inode;
536         int rc;
537         ENTRY;
538
539         if (!gen_client(conn)) {
540                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
541                 RETURN(-EINVAL);
542         }
543
544         obddev = conn->oc_dev;
545         dentry = filter_fid2dentry(obddev, filter_parent(obddev, oa->o_mode),
546                                    oa->o_id, oa->o_mode);
547         if (IS_ERR(dentry))
548                 RETURN(PTR_ERR(dentry));
549
550         inode = dentry->d_inode;
551         iattr_from_obdo(&iattr, oa);
552         iattr.ia_mode &= ~S_IFMT;
553         iattr.ia_mode |= S_IFREG;
554         lock_kernel();
555         if (iattr.ia_mode & ATTR_SIZE)
556                 down(&inode->i_sem);
557         push_ctxt(&saved, &conn->oc_dev->u.filter.fo_ctxt);
558         if (inode->i_op->setattr)
559                 rc = inode->i_op->setattr(dentry, &iattr);
560         else
561                 rc = inode_setattr(inode, &iattr);
562         pop_ctxt(&saved);
563         if (iattr.ia_mode & ATTR_SIZE) {
564                 up(&inode->i_sem);
565                 oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLCTIME | OBD_MD_FLMTIME;
566                 obdo_from_inode(oa, inode);
567         }
568         unlock_kernel();
569
570         CDEBUG(D_INODE, "put dentry %p, count = %d\n", inode,
571                atomic_read(&dentry->d_count) - 1);
572         dput(dentry);
573         RETURN(rc);
574 }
575
576 static int filter_open(struct obd_conn *conn, struct obdo *oa)
577 {
578         struct obd_device *obddev;
579         struct dentry *dentry;
580         /* ENTRY; */
581
582         if (!gen_client(conn)) {
583                 CDEBUG(D_IOCTL, "fatal: invalid client %u\n", conn->oc_id);
584                 RETURN(-EINVAL);
585         }
586
587         obddev = conn->oc_dev;
588         dentry = filter_fid2dentry(obddev, filter_parent(obddev, oa->o_mode),
589                                    oa->o_id, oa->o_mode);
590         if (IS_ERR(dentry))
591                 RETURN(PTR_ERR(dentry));
592
593         return 0;
594 } /* filter_open */
595
596 static int filter_close(struct obd_conn *conn, struct obdo *oa)
597 {
598         struct obd_device *obddev;
599         struct dentry *dentry;
600         /* ENTRY; */
601
602         if (!gen_client(conn)) {
603                 CDEBUG(D_IOCTL, "fatal: invalid client %u\n", conn->oc_id);
604                 RETURN(-EINVAL);
605         }
606
607         obddev = conn->oc_dev;
608         dentry = filter_fid2dentry(obddev, filter_parent(obddev, oa->o_mode),
609                                    oa->o_id, oa->o_mode);
610         if (IS_ERR(dentry))
611                 RETURN(PTR_ERR(dentry));
612
613         CDEBUG(D_INODE, "put dentry %p, count = %d\n", dentry,
614                atomic_read(&dentry->d_count) - 1);
615         dput(dentry);  /* for the close */
616         CDEBUG(D_INODE, "put dentry %p, count = %d\n", dentry,
617                atomic_read(&dentry->d_count) - 1);
618         dput(dentry);  /* for this call */
619         return 0;
620 } /* filter_close */
621
622 static int filter_create(struct obd_conn* conn, struct obdo *oa)
623 {
624         char name[64];
625         struct obd_run_ctxt saved;
626         struct file *file;
627         int mode;
628         struct obd_device *obddev = conn->oc_dev;
629         struct iattr;
630         ENTRY;
631
632         if (!gen_client(conn)) {
633                 CERROR("invalid client %u\n", conn->oc_id);
634                 return -EINVAL;
635         }
636
637         oa->o_id = filter_next_id(conn->oc_dev);
638         if (!(oa->o_mode && S_IFMT)) {
639                 CERROR("filter obd: no type!\n");
640                 return -ENOENT;
641         }
642
643         filter_id(name, oa->o_id, oa->o_mode);
644         mode = (oa->o_mode & ~S_IFMT) | S_IFREG;
645         push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
646         file = filp_open(name, O_RDONLY | O_CREAT, mode);
647         pop_ctxt(&saved);
648         if (IS_ERR(file)) {
649                 CERROR("Error mknod obj %s, err %ld\n", name, PTR_ERR(file));
650                 return -ENOENT;
651         }
652         filp_close(file, 0);
653
654         /* Set flags for fields we have set in the inode struct */
655         oa->o_valid |= OBD_MD_FLID | OBD_MD_FLBLKSZ | OBD_MD_FLBLOCKS |
656                  OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME |
657                  OBD_MD_FLUID | OBD_MD_FLGID;
658
659         /* XXX Hmm, shouldn't we copy the fields into the obdo here? */
660         return 0;
661 }
662
663 static int filter_destroy(struct obd_conn *conn, struct obdo *oa)
664 {
665         struct obd_run_ctxt saved;
666         struct obd_device *obddev;
667         struct obd_client *cli;
668         struct inode *inode;
669         struct dentry *dir_dentry, *object_dentry;
670         int rc;
671         ENTRY;
672
673         if (!(cli = gen_client(conn))) {
674                 CERROR("invalid client %u\n", conn->oc_id);
675                 RETURN(-EINVAL);
676         }
677
678         CDEBUG(D_INODE, "destroying object %Ld\n",oa->o_id);
679         obddev = conn->oc_dev;
680
681         dir_dentry = filter_parent(obddev, oa->o_mode);
682         down(&dir_dentry->d_inode->i_sem);
683
684         object_dentry = filter_fid2dentry(obddev, dir_dentry, oa->o_id,
685                                           oa->o_mode);
686         if (IS_ERR(object_dentry))
687                 GOTO(out, rc = -ENOENT);
688
689         inode = object_dentry->d_inode;
690         if (inode->i_nlink != 1) {
691                 CERROR("destroying inode with nlink = %d\n", inode->i_nlink);
692                 inode->i_nlink = 1;
693         }
694         inode->i_mode = S_IFREG;
695
696         push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
697
698         rc = vfs_unlink(dir_dentry->d_inode, object_dentry);
699         pop_ctxt(&saved);
700         CDEBUG(D_INODE, "put child %p, count = %d\n", object_dentry,
701                atomic_read(&object_dentry->d_count) - 1);
702         dput(object_dentry);
703
704         EXIT;
705 out:
706         up(&dir_dentry->d_inode->i_sem);
707         return rc;
708 }
709
710 /* NB count and offset are used for punch, but not truncate */
711 static int filter_truncate(struct obd_conn *conn, struct obdo *oa,
712                            obd_size count, obd_off offset)
713 {
714         int error;
715         ENTRY;
716
717         CDEBUG(D_INODE, "calling truncate for object #%Ld, valid = %x, "
718                "o_size = %Ld\n", oa->o_id, oa->o_valid, oa->o_size);
719         error = filter_setattr(conn, oa);
720
721         RETURN(error);
722 }
723
724 /* buffer must lie in user memory here */
725 static int filter_read(struct obd_conn *conn, struct obdo *oa, char *buf,
726                         obd_size *count, obd_off offset)
727 {
728         struct file * file;
729         unsigned long retval;
730         int err;
731         ENTRY;
732
733         if (!gen_client(conn)) {
734                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
735                 RETURN(-EINVAL);
736         }
737
738         file = filter_obj_open(conn->oc_dev, oa->o_id, oa->o_mode);
739         if (IS_ERR(file))
740                 RETURN(PTR_ERR(file));
741
742         /* count doubles as retval */
743         retval = file->f_op->read(file, buf, *count, (loff_t *)&offset);
744         filp_close(file, 0);
745
746         if ( retval >= 0 ) {
747                 err = 0;
748                 *count = retval;
749         } else {
750                 err = retval;
751                 *count = 0;
752         }
753
754         return err;
755 }
756
757
758 /* buffer must lie in user memory here */
759 static int filter_write(struct obd_conn *conn, struct obdo *oa, char *buf,
760                          obd_size *count, obd_off offset)
761 {
762         struct obd_run_ctxt saved;
763         int err;
764         struct file * file;
765         unsigned long retval;
766         ENTRY;
767
768         if (!gen_client(conn)) {
769                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
770                 RETURN(-EINVAL);
771         }
772
773         file = filter_obj_open(conn->oc_dev, oa->o_id, oa->o_mode);
774         if (IS_ERR(file))
775                 RETURN(PTR_ERR(file));
776
777         /* count doubles as retval */
778         push_ctxt(&saved, &conn->oc_dev->u.filter.fo_ctxt);
779         retval = file->f_op->write(file, buf, *count, (loff_t *)&offset);
780         pop_ctxt(&saved);
781         filp_close(file, 0);
782
783         if ( retval >= 0 ) {
784                 err = 0;
785                 *count = retval;
786                 EXIT;
787         } else {
788                 err = retval;
789                 *count = 0;
790                 EXIT;
791         }
792
793         return err;
794 } /* filter_write */
795
796 static int filter_pgcache_brw(int rw, struct obd_conn *conn, obd_count num_oa,
797                                struct obdo **oa, obd_count *oa_bufs,
798                                struct page **pages, obd_size *count,
799                                obd_off *offset, obd_flag *flags, void *callback)
800 {
801         struct obd_run_ctxt      saved;
802         struct super_block      *sb;
803         int                      onum;          /* index to oas */
804         int                      pnum;          /* index to pages (bufs) */
805         unsigned long            retval;
806         int                      error;
807         struct file             *file;
808         ENTRY;
809
810         if (!gen_client(conn)) {
811                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
812                 RETURN(-EINVAL);
813         }
814
815         sb = conn->oc_dev->u.filter.fo_sb;
816         // if (rw == WRITE)
817         push_ctxt(&saved, &conn->oc_dev->u.filter.fo_ctxt);
818         pnum = 0; /* pnum indexes buf 0..num_pages */
819         for (onum = 0; onum < num_oa; onum++) {
820                 int pg;
821
822                 file = filter_obj_open(conn->oc_dev, oa[onum]->o_id,
823                                        oa[onum]->o_mode);
824                 if (IS_ERR(file))
825                         GOTO(out, retval = PTR_ERR(file));
826
827                 /* count doubles as retval */
828                 for (pg = 0; pg < oa_bufs[onum]; pg++) {
829                         CDEBUG(D_INODE, "OP %d obdo no/pno: (%d,%d) (%ld,%ld) "
830                                "off count (%Ld,%Ld)\n",
831                                rw, onum, pnum, file->f_dentry->d_inode->i_ino,
832                                (unsigned long)offset[pnum] >> PAGE_CACHE_SHIFT,
833                                (unsigned long long)offset[pnum],
834                                (unsigned long long)count[pnum]);
835                         if (rw == WRITE) {
836                                 loff_t off;
837                                 char *buffer;
838                                 off = offset[pnum];
839                                 buffer = kmap(pages[pnum]);
840                                 retval = file->f_op->write(file, buffer, count[pnum], &off);
841                                 kunmap(pages[pnum]);
842                                 CDEBUG(D_INODE, "retval %ld\n", retval);
843                         } else {
844                                 loff_t off = offset[pnum];
845                                 char *buffer = kmap(pages[pnum]);
846
847                                 if (off >= file->f_dentry->d_inode->i_size) {
848                                         memset(buffer, 0, count[pnum]);
849                                         retval = count[pnum];
850                                 } else {
851                                         retval = file->f_op->read(file, buffer, count[pnum], &off);
852                                 }
853                                 kunmap(pages[pnum]);
854
855                                 if (retval != count[pnum]) {
856                                         filp_close(file, 0);
857                                         GOTO(out, retval = -EIO);
858                                 }
859                                 CDEBUG(D_INODE, "retval %ld\n", retval);
860                         }
861                         pnum++;
862                 }
863                 /* sizes and blocks are set by generic_file_write */
864                 /* ctimes/mtimes will follow with a setattr call */
865                 filp_close(file, 0);
866         }
867
868         EXIT;
869 out:
870         // if (rw == WRITE)
871         pop_ctxt(&saved);
872         error = (retval >= 0) ? 0 : retval;
873         return error;
874 }
875
876
877 struct inode *ioobj_to_inode(struct obd_conn *conn, struct obd_ioobj *o)
878 {
879         struct super_block *sb = conn->oc_dev->u.filter.fo_sb;
880         struct inode *inode = NULL;
881         ENTRY;
882
883         if (!sb || !sb->s_dev) {
884                 CDEBUG(D_SUPER, "fatal: device not initialized.\n");
885                 RETURN(NULL);
886         }
887
888         if ( !o->ioo_id ) {
889                 CDEBUG(D_INODE, "fatal: invalid obdo %lu\n", (long)o->ioo_id);
890                 RETURN(NULL);
891         }
892
893         inode = filter_inode_from_obj(conn->oc_dev, o->ioo_id, S_IFREG);
894         if (!inode || inode->i_nlink == 0 || is_bad_inode(inode)) {
895                 CERROR("from obdo - fatal: invalid inode %ld (%s).\n",
896                        (long)o->ioo_id, inode ? inode->i_nlink ? "bad inode" :
897                        "no links" : "NULL");
898                 iput(inode);
899                 RETURN(NULL);
900         }
901
902         RETURN(inode);
903 }
904
905 /*
906  * Calculate the number of buffer credits needed to write multiple pages in
907  * a single ext3/extN transaction.  No, this shouldn't be here, but as yet
908  * ext3 doesn't have a nice API for calculating this sort of thing in advance.
909  *
910  * See comment above ext3_writepage_trans_blocks for details.  We assume
911  * no data journaling is being done, but it does allow for all of the pages
912  * being non-contiguous.  If we are guaranteed contiguous pages we could
913  * reduce the number of (d)indirect blocks a lot.
914  *
915  * With N blocks per page and P pages, for each inode we have at most:
916  * N*P indirect
917  * min(N*P, blocksize/4 + 1) dindirect blocks
918  * 1 tindirect
919  *
920  * For the entire filesystem, we have at most:
921  * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
922  * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
923  * 1 inode block
924  * 1 superblock
925  * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
926  */
927 static int ext3_credits_needed(struct super_block *sb, int objcount,
928                                struct obd_ioobj *obj)
929 {
930         struct obd_ioobj *o = obj;
931         int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
932         int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
933         int nbitmaps = 0;
934         int ngdblocks = 0;
935         int needed = objcount + 1;
936         int i;
937
938         for (i = 0; i < objcount; i++, o++) {
939                 int nblocks = o->ioo_bufcnt * blockpp;
940                 int ndindirect = min(nblocks, addrpp + 1);
941                 int nindir = nblocks + ndindirect + 1;
942
943                 nbitmaps += nindir + nblocks;
944                 ngdblocks += nindir + nblocks;
945
946                 needed += nindir;
947         }
948
949         if (nbitmaps > EXT3_SB(sb)->s_groups_count)
950                 nbitmaps = EXT3_SB(sb)->s_groups_count;
951         if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
952                 ngdblocks = EXT3_SB(sb)->s_gdb_count;
953
954         needed += nbitmaps + ngdblocks;
955
956 #ifdef CONFIG_QUOTA
957         /* We assume that there will be 1 bit set in s_dquot.flags for each
958          * quota file that is active.  This is at least true for now.
959          */
960         needed += hweight32(sb_any_quota_enabled(sb)) *
961                 EXT3_SINGLEDATA_TRANS_BLOCKS;
962 #endif
963
964         return needed;
965 }
966
967 /* We have to start a huge journal transaction here to hold all of the
968  * metadata for the pages being written here.  This is necessitated by
969  * the fact that we do lots of prepare_write operations before we do
970  * any of the matching commit_write operations, so even if we split
971  * up to use "smaller" transactions none of them could complete until
972  * all of them were opened.  By having a single journal transaction,
973  * we eliminate duplicate reservations for common blocks like the
974  * superblock and group descriptors or bitmaps.
975  *
976  * We will start the transaction here, but each prepare_write will
977  * add a refcount to the transaction, and each commit_write will
978  * remove a refcount.  The transaction will be closed when all of
979  * the pages have been written.
980  */
981 static void *ext3_filter_journal_start(struct filter_obd *filter,
982                                        int objcount, struct obd_ioobj *obj,
983                                        int niocount, struct niobuf_remote *nb)
984 {
985         journal_t *journal = NULL;
986         handle_t *handle = NULL;
987         int needed;
988
989         /* Assumes ext3 and extN have same sb_info layout, but avoids issues
990          * with having extN built properly before filterobd for now.
991          */
992         journal = EXT3_SB(filter->fo_sb)->s_journal;
993         needed = ext3_credits_needed(filter->fo_sb, objcount, obj);
994
995         /* The number of blocks we could _possibly_ dirty can very large.
996          * We reduce our request if it is absurd (and we couldn't get that
997          * many credits for a single handle anyways).
998          *
999          * At some point we have to limit the size of I/Os sent at one time,
1000          * increase the size of the journal, or we have to calculate the
1001          * actual journal requirements more carefully by checking all of
1002          * the blocks instead of being maximally pessimistic.  It remains to
1003          * be seen if this is a real problem or not.
1004          */
1005         if (needed > journal->j_max_transaction_buffers) {
1006                 CERROR("want too many journal credits (%d) using %d instead\n",
1007                        needed, journal->j_max_transaction_buffers);
1008                 needed = journal->j_max_transaction_buffers;
1009         }
1010
1011         handle = journal_start(journal, needed);
1012         if (IS_ERR(handle))
1013                 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
1014                        PTR_ERR(handle));
1015
1016         return(handle);
1017 }
1018
1019 static void *filter_journal_start(void **journal_save,
1020                                   struct filter_obd *filter,
1021                                   int objcount, struct obd_ioobj *obj,
1022                                   int niocount, struct niobuf_remote *nb)
1023 {
1024         void *handle = NULL;
1025
1026         /* This may not be necessary - we probably never have a
1027          * transaction started when we enter here, so we can
1028          * remove the saving of the journal state entirely.
1029          * For now leave it in just to see if it ever happens.
1030          */
1031         *journal_save = current->journal_info;
1032         if (*journal_save) {
1033                 CERROR("Already have handle %p???\n", *journal_save);
1034                 LBUG();
1035                 current->journal_info = NULL;
1036         }
1037
1038         if (!strcmp(filter->fo_fstype, "ext3") ||
1039             !strcmp(filter->fo_fstype, "extN"))
1040                 handle = ext3_filter_journal_start(filter, objcount, obj,
1041                                                    niocount, nb);
1042         return handle;
1043 }
1044
1045 static int ext3_filter_journal_stop(void *handle)
1046 {
1047         int rc;
1048
1049         /* We got a refcount on the handle for each call to prepare_write,
1050          * so we can drop the "parent" handle here to avoid the need for
1051          * osc to call back into filterobd to close the handle.  The
1052          * remaining references will be dropped in commit_write.
1053          */
1054         rc = journal_stop((handle_t *)handle);
1055
1056         return rc;
1057 }
1058
1059 static int filter_journal_stop(void *journal_save, struct filter_obd *filter,
1060                                void *handle)
1061 {
1062         int rc = 0;
1063
1064         if (!strcmp(filter->fo_fstype, "ext3") ||
1065             !strcmp(filter->fo_fstype, "extN"))
1066                 rc = ext3_filter_journal_stop(handle);
1067
1068         current->journal_info = journal_save;
1069
1070         return rc;
1071 }
1072
1073 struct page *filter_get_page_write(struct inode *inode, unsigned long index,
1074                                    struct niobuf_local *lnb)
1075 {
1076         struct address_space *mapping = inode->i_mapping;
1077         struct page *page;
1078         int rc;
1079
1080         //ASSERT_PAGE_INDEX(index, GOTO(err, rc = -EINVAL));
1081         page = grab_cache_page_nowait(mapping, index); /* locked page */
1082
1083         /* This page is currently locked, so we grab a new one temporarily */
1084         if (!page) {
1085                 unsigned long addr;
1086                 addr = __get_free_pages(GFP_KERNEL, 0);
1087                 if (!addr) {
1088                         CERROR("no memory for a temp page\n");
1089                         LBUG();
1090                         GOTO(err, rc = -ENOMEM);
1091                 }
1092                 page = virt_to_page(addr);
1093                 kmap(page);
1094                 page->index = index;
1095                 lnb->flags |= N_LOCAL_TEMP_PAGE;
1096         } else if (!IS_ERR(page)) {
1097                 /* Note: Called with "O" and "PAGE_SIZE" this is essentially
1098                  * a no-op for most filesystems, because we write the whole
1099                  * page.  For partial-page I/O this will read in the page.
1100                  */
1101                 kmap(page);
1102                 rc = mapping->a_ops->prepare_write(NULL, page, 0, PAGE_SIZE);
1103                 if (rc) {
1104                         CERROR("page index %lu, rc = %d\n", index, rc);
1105                         if (rc != -ENOSPC)
1106                                 LBUG();
1107                         GOTO(err_unlock, rc);
1108                 }
1109                 /* XXX not sure if we need this if we are overwriting page */
1110                 if (PageError(page)) {
1111                         CERROR("error on page index %lu, rc = %d\n", index, rc);
1112                         LBUG();
1113                         GOTO(err_unlock, rc = -EIO);
1114                 }
1115         }
1116         return page;
1117
1118 err_unlock:
1119         unlock_page(page);
1120         lustre_put_page(page);
1121 err:
1122         return ERR_PTR(rc);
1123 }
1124
1125 static int filter_preprw(int cmd, struct obd_conn *conn,
1126                          int objcount, struct obd_ioobj *obj,
1127                          int niocount, struct niobuf_remote *nb,
1128                          struct niobuf_local *res, void **desc_private)
1129 {
1130         struct obd_run_ctxt saved;
1131         struct obd_device *obddev;
1132         struct obd_ioobj *o = obj;
1133         struct niobuf_remote *b = nb;
1134         struct niobuf_local *r = res;
1135         void *journal_save = NULL;
1136         int rc = 0;
1137         int i;
1138         ENTRY;
1139
1140         memset(res, 0, sizeof(*res) * niocount);
1141         obddev = conn->oc_dev;
1142
1143         push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
1144
1145         if (cmd == OBD_BRW_WRITE) {
1146                 *desc_private = filter_journal_start(&journal_save,
1147                                                      &obddev->u.filter,
1148                                                      objcount, obj, niocount,
1149                                                      nb);
1150                 if (IS_ERR(*desc_private))
1151                         GOTO(out_ctxt, rc = PTR_ERR(*desc_private));
1152         }
1153
1154         for (i = 0; i < objcount; i++, o++) {
1155                 struct dentry *dentry;
1156                 struct inode *inode;
1157                 int j;
1158
1159                 dentry = filter_fid2dentry(obddev,
1160                                            filter_parent(obddev, S_IFREG),
1161                                            o->ioo_id, S_IFREG);
1162                 if (IS_ERR(dentry))
1163                         GOTO(out_ctxt, rc = PTR_ERR(dentry));
1164                 inode = dentry->d_inode;
1165
1166                 for (j = 0; j < o->ioo_bufcnt; j++, b++, r++) {
1167                         unsigned long index = b->offset >> PAGE_SHIFT;
1168                         struct page *page;
1169
1170                         /* XXX  We _might_ change this to a dcount if we
1171                          * wanted to pass a dentry pointer in the niobuf
1172                          * to avoid doing so many igets on an inode we
1173                          * already have.  It appears to be solely for the
1174                          * purpose of having a refcount that we can drop
1175                          * in commitrw where we get one call per page.
1176                          */
1177                         if (j > 0)
1178                                 r->dentry = dget(dentry);
1179                         else
1180                                 r->dentry = dentry;
1181
1182                         /* FIXME: we need to iput all inodes on error */
1183                         if (!inode)
1184                                 GOTO(out_ctxt, rc = -EINVAL);
1185
1186                         if (cmd == OBD_BRW_WRITE) {
1187                                 page = filter_get_page_write(inode, index, r);
1188
1189                                 /* We unlock the page to avoid deadlocks with
1190                                  * the page I/O because we are preparing
1191                                  * multiple pages at one time and we have lock
1192                                  * ordering problems.  Lustre I/O and disk I/O
1193                                  * on this page can happen concurrently.
1194                                  */
1195                         } else
1196                                 page = lustre_get_page_read(inode, index);
1197
1198                         /* FIXME: we need to clean up here... */
1199                         if (IS_ERR(page))
1200                                 GOTO(out_ctxt, rc = PTR_ERR(page));
1201
1202                         r->addr = page_address(page);
1203                         r->offset = b->offset;
1204                         r->page = page;
1205                         r->len = PAGE_SIZE;
1206                 }
1207         }
1208
1209         if (cmd == OBD_BRW_WRITE) {
1210                 /* FIXME: need to clean up here */
1211                 rc = filter_journal_stop(journal_save, &obddev->u.filter,
1212                                          *desc_private);
1213         }
1214 out_ctxt:
1215         pop_ctxt(&saved);
1216         RETURN(rc);
1217 }
1218
1219 static int filter_write_locked_page(struct niobuf_local *lnb)
1220 {
1221         struct page *lpage;
1222         int rc;
1223
1224         lpage = lustre_get_page_write(lnb->dentry->d_inode, lnb->page->index);
1225         /* XXX */
1226
1227         memcpy(page_address(lpage), kmap(lnb->page), PAGE_SIZE);
1228         kunmap(lnb->page);
1229         __free_pages(lnb->page, 0);
1230
1231         rc = lustre_commit_page(lpage, 0, PAGE_SIZE);
1232         dput(lnb->dentry);
1233
1234         return rc;
1235 }
1236
1237 static int filter_commitrw(int cmd, struct obd_conn *conn,
1238                            int objcount, struct obd_ioobj *obj,
1239                            int niocount, struct niobuf_local *res,
1240                            void *private)
1241 {
1242         struct obd_run_ctxt saved;
1243         struct obd_ioobj *o = obj;
1244         struct niobuf_local *r = res;
1245         void *journal_save;
1246         int found_locked = 0;
1247         int i;
1248         ENTRY;
1249
1250         // if (cmd == OBD_BRW_WRITE)
1251         push_ctxt(&saved, &conn->oc_dev->u.filter.fo_ctxt);
1252         journal_save = current->journal_info;
1253         if (journal_save)
1254                 CERROR("Existing handle %p???\n", journal_save);
1255         current->journal_info = private;
1256         for (i = 0; i < objcount; i++, obj++) {
1257                 int j;
1258                 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
1259                         struct page *page = r->page;
1260
1261                         /* If there was an error setting up a particular page
1262                          * for I/O we still need to continue with the rest of
1263                          * the pages in order to balance prepate/commit_write
1264                          * calls, and to complete as much I/O as possible.
1265                          */
1266                         if (!page)
1267                                 LBUG();
1268
1269                         if (r->flags & N_LOCAL_TEMP_PAGE) {
1270                                 found_locked = 1;
1271                                 continue;
1272                         }
1273
1274                         if (cmd == OBD_BRW_WRITE) {
1275                                 int rc;
1276                                 rc = lustre_commit_page(page, 0, PAGE_SIZE);
1277
1278                                 /* FIXME: still need to iput the other inodes */
1279                                 if (rc)
1280                                         RETURN(rc);
1281                         } else
1282                                 lustre_put_page(page);
1283
1284                         CDEBUG(D_INODE,
1285                                "put inode %p (%ld), count = %d, nlink = %d\n",
1286                                page->mapping->host,
1287                                page->mapping->host->i_ino,
1288                                atomic_read(&page->mapping->host->i_count) - 1,
1289                                page->mapping->host->i_nlink);
1290                         dput(r->dentry);
1291                 }
1292         }
1293         if (!found_locked)
1294                 goto out;
1295
1296         for (i = 0; i < objcount; i++, obj++) {
1297                 int j;
1298                 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
1299                         int rc;
1300                         if (!(r->flags & N_LOCAL_TEMP_PAGE))
1301                                 continue;
1302
1303                         rc = filter_write_locked_page(r);
1304                         /* XXX */
1305                 }
1306         }
1307
1308 out:
1309         current->journal_info = journal_save;
1310         pop_ctxt(&saved);
1311         RETURN(0);
1312 }
1313
1314 static int filter_statfs(struct obd_conn *conn, struct statfs * statfs)
1315 {
1316         struct super_block *sb;
1317         int err;
1318         ENTRY;
1319
1320         if (!gen_client(conn)) {
1321                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
1322                 RETURN(-EINVAL);
1323         }
1324
1325         sb = conn->oc_dev->u.filter.fo_sb;
1326
1327         err = sb->s_op->statfs(sb, statfs);
1328         RETURN(err);
1329 } /* filter_statfs */
1330
1331
1332 static int filter_get_info(struct obd_conn *conn, obd_count keylen,
1333                            void *key, obd_count *vallen, void **val)
1334 {
1335         struct obd_device *obddev;
1336         struct obd_client * cli;
1337         ENTRY;
1338
1339         if (!(cli = gen_client(conn))) {
1340                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
1341                 RETURN(-EINVAL);
1342         }
1343
1344         obddev = conn->oc_dev;
1345
1346         if ( keylen == strlen("blocksize") &&
1347              memcmp(key, "blocksize", keylen) == 0 ) {
1348                 *vallen = sizeof(long);
1349                 *val = (void *)(long)obddev->u.filter.fo_sb->s_blocksize;
1350                 RETURN(0);
1351         }
1352
1353         if ( keylen == strlen("blocksize_bits") &&
1354              memcmp(key, "blocksize_bits", keylen) == 0 ){
1355                 *vallen = sizeof(long);
1356                 *val = (void *)(long)obddev->u.filter.fo_sb->s_blocksize_bits;
1357                 RETURN(0);
1358         }
1359
1360         if ( keylen == strlen("root_ino") &&
1361              memcmp(key, "root_ino", keylen) == 0 ){
1362                 *vallen = sizeof(long);
1363                 *val = (void *)(long)FILTER_ROOTINO;
1364                 RETURN(0);
1365         }
1366
1367         CDEBUG(D_IOCTL, "invalid key\n");
1368         RETURN(-EINVAL);
1369 }
1370
1371
1372 static struct obd_ops filter_obd_ops = {
1373         o_get_info:    filter_get_info,
1374         o_setup:       filter_setup,
1375         o_cleanup:     filter_cleanup,
1376         o_connect:     filter_connect,
1377         o_disconnect:  filter_disconnect,
1378         o_statfs:      filter_statfs,
1379         o_getattr:     filter_getattr,
1380         o_create:      filter_create,
1381         o_setattr:     filter_setattr,
1382         o_destroy:     filter_destroy,
1383         o_open:        filter_open,
1384         o_close:       filter_close,
1385         o_read:        filter_read,
1386         o_write:       filter_write,
1387         o_brw:         filter_pgcache_brw,
1388         o_punch:       filter_truncate,
1389         o_preprw:      filter_preprw,
1390         o_commitrw:    filter_commitrw
1391 #if 0
1392         o_preallocate: filter_preallocate_inodes,
1393         o_migrate:     filter_migrate,
1394         o_copy:        gen_copy_data,
1395         o_iterate:     filter_iterate
1396 #endif
1397 };
1398
1399
1400 static int __init obdfilter_init(void)
1401 {
1402         printk(KERN_INFO "Filtering OBD driver  v0.001, braam@clusterfs.com\n");
1403         return obd_register_type(&filter_obd_ops, OBD_FILTER_DEVICENAME);
1404 }
1405
1406 static void __exit obdfilter_exit(void)
1407 {
1408         obd_unregister_type(OBD_FILTER_DEVICENAME);
1409 }
1410
1411 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
1412 MODULE_DESCRIPTION("Lustre Filtering OBD driver v1.0");
1413 MODULE_LICENSE("GPL");
1414
1415 module_init(obdfilter_init);
1416 module_exit(obdfilter_exit);