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