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