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