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