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