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