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