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