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