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