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