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