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