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