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