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