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