Whamcloud - gitweb
- CROW-related fixes from b_hd_mdref
[fs/lustre-release.git] / lustre / obdfilter / filter.c
index 048112a..80a55f7 100644 (file)
 #include <linux/dcache.h>
 #include <linux/init.h>
 #include <linux/version.h>
+#include <linux/jbd.h>
+#include <linux/ext3_fs.h>
 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
 # include <linux/mount.h>
 # include <linux/buffer_head.h>
+# include <linux/bio.h>
 #endif
 
 #include <linux/obd_class.h>
 #include <linux/obd_lov.h>
+#include <linux/obd_ost.h>
 #include <linux/lustre_dlm.h>
 #include <linux/lustre_fsfilt.h>
 #include <linux/lprocfs_status.h>
 #include <linux/lustre_log.h>
 #include <linux/lustre_commit_confd.h>
+#include <libcfs/list.h>
 
+#include <linux/lustre_smfs.h>
+#include <linux/lustre_sec.h>
 #include "filter_internal.h"
 
+/* Group 0 is no longer a legal group, to catch uninitialized IDs */
+#define FILTER_MIN_GROUPS 3
+
 static struct lvfs_callback_ops filter_lvfs_ops;
 
 static int filter_destroy(struct obd_export *exp, struct obdo *oa,
                           struct lov_stripe_md *ea, struct obd_trans_info *);
+struct obd_llogs *filter_grab_llog_for_group(struct obd_device *,
+                                             int, struct obd_export *);
 
 static void filter_commit_cb(struct obd_device *obd, __u64 transno,
                              void *cb_data, int error)
@@ -67,7 +79,6 @@ static void filter_commit_cb(struct obd_device *obd, __u64 transno,
         obd_transno_commit_cb(obd, transno, error);
 }
 
-
 /* Assumes caller has already pushed us into the kernel context. */
 int filter_finish_transno(struct obd_export *exp, struct obd_trans_info *oti,
                           int rc)
@@ -93,7 +104,7 @@ int filter_finish_transno(struct obd_export *exp, struct obd_trans_info *oti,
                 filter->fo_fsd->fsd_last_transno = cpu_to_le64(last_rcvd);
                 spin_unlock(&filter->fo_translock);
                 oti->oti_transno = last_rcvd;
-        } else { 
+        } else {
                 spin_lock(&filter->fo_translock);
                 last_rcvd = oti->oti_transno;
                 if (last_rcvd > le64_to_cpu(filter->fo_fsd->fsd_last_transno))
@@ -101,15 +112,17 @@ int filter_finish_transno(struct obd_export *exp, struct obd_trans_info *oti,
                                 cpu_to_le64(last_rcvd);
                 spin_unlock(&filter->fo_translock);
         }
+        
         fcd->fcd_last_rcvd = cpu_to_le64(last_rcvd);
-        fcd->fcd_mount_count = filter->fo_fsd->fsd_mount_count;
 
         /* could get xid from oti, if it's ever needed */
         fcd->fcd_last_xid = 0;
 
         off = fed->fed_lr_off;
-        fsfilt_add_journal_cb(exp->exp_obd, last_rcvd, oti->oti_handle,
-                              filter_commit_cb, NULL);
+
+        fsfilt_add_journal_cb(exp->exp_obd, filter->fo_sb, last_rcvd,
+                              oti->oti_handle, filter_commit_cb, NULL);
+
         err = fsfilt_write_record(exp->exp_obd, filter->fo_rcvd_filp, fcd,
                                   sizeof(*fcd), &off, 0);
         if (err) {
@@ -148,7 +161,7 @@ static int filter_client_add(struct obd_device *obd, struct filter_obd *filter,
         LASSERT(bitmap != NULL);
 
         /* XXX if fcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
-        if (!strcmp(fed->fed_fcd->fcd_uuid, obd->obd_uuid.uuid))
+        if (!strcmp((char *)fed->fed_fcd->fcd_uuid, (char *)obd->obd_uuid.uuid))
                 RETURN(0);
 
         /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
@@ -185,7 +198,7 @@ static int filter_client_add(struct obd_device *obd, struct filter_obd *filter,
                fed->fed_lr_idx, fed->fed_lr_off, fed->fed_fcd->fcd_uuid);
 
         if (new_client) {
-                struct obd_run_ctxt saved;
+                struct lvfs_run_ctxt saved;
                 loff_t off = fed->fed_lr_off;
                 int err;
                 void *handle;
@@ -193,7 +206,7 @@ static int filter_client_add(struct obd_device *obd, struct filter_obd *filter,
                 CDEBUG(D_INFO, "writing client fcd at idx %u (%llu) (len %u)\n",
                        fed->fed_lr_idx,off,(unsigned int)sizeof(*fed->fed_fcd));
 
-                push_ctxt(&saved, &obd->obd_ctxt, NULL);
+                push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
                 /* Transaction needed to fix bug 1403 */
                 handle = fsfilt_start(obd,
                                       filter->fo_rcvd_filp->f_dentry->d_inode,
@@ -206,11 +219,11 @@ static int filter_client_add(struct obd_device *obd, struct filter_obd *filter,
                                                   fed->fed_fcd,
                                                   sizeof(*fed->fed_fcd),
                                                   &off, 1);
-                        fsfilt_commit(obd,
+                        fsfilt_commit(obd, filter->fo_sb,
                                       filter->fo_rcvd_filp->f_dentry->d_inode,
                                       handle, 1);
                 }
-                pop_ctxt(&saved, &obd->obd_ctxt, NULL);
+                pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
 
                 if (err) {
                         CERROR("error writing %s client idx %u: rc %d\n",
@@ -227,7 +240,7 @@ static int filter_client_free(struct obd_export *exp, int flags)
         struct filter_obd *filter = &exp->exp_obd->u.filter;
         struct obd_device *obd = exp->exp_obd;
         struct filter_client_data zero_fcd;
-        struct obd_run_ctxt saved;
+        struct lvfs_run_ctxt saved;
         int rc;
         loff_t off;
         ENTRY;
@@ -239,7 +252,7 @@ static int filter_client_free(struct obd_export *exp, int flags)
                 GOTO(free, 0);
 
         /* XXX if fcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
-        if (strcmp(fed->fed_fcd->fcd_uuid, obd->obd_uuid.uuid ) == 0)
+        if (!strcmp((char *)fed->fed_fcd->fcd_uuid, (char *)obd->obd_uuid.uuid))
                 GOTO(free, 0);
 
         LASSERT(filter->fo_last_rcvd_slots != NULL);
@@ -249,23 +262,35 @@ static int filter_client_free(struct obd_export *exp, int flags)
         CDEBUG(D_INFO, "freeing client at idx %u (%lld) with UUID '%s'\n",
                fed->fed_lr_idx, fed->fed_lr_off, fed->fed_fcd->fcd_uuid);
 
-        if (!test_and_clear_bit(fed->fed_lr_idx, filter->fo_last_rcvd_slots)) {
+        /* Clear the bit _after_ zeroing out the client so we don't
+           race with filter_client_add and zero out new clients.*/
+        if (!test_bit(fed->fed_lr_idx, filter->fo_last_rcvd_slots)) {
                 CERROR("FILTER client %u: bit already clear in bitmap!!\n",
                        fed->fed_lr_idx);
                 LBUG();
         }
 
         memset(&zero_fcd, 0, sizeof zero_fcd);
-        push_ctxt(&saved, &obd->obd_ctxt, NULL);
+        push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
         rc = fsfilt_write_record(obd, filter->fo_rcvd_filp, &zero_fcd,
                                  sizeof(zero_fcd), &off, 1);
-        pop_ctxt(&saved, &obd->obd_ctxt, NULL);
+        if (rc == 0)
+                /* update server's transno */
+                filter_update_server_data(obd, filter->fo_rcvd_filp,
+                                          filter->fo_fsd, 1);
+        pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
 
         CDEBUG(rc == 0 ? D_INFO : D_ERROR,
                "zeroing disconnecting client %s at idx %u (%llu) in %s rc %d\n",
                fed->fed_fcd->fcd_uuid, fed->fed_lr_idx, fed->fed_lr_off,
                LAST_RCVD, rc);
 
+        if (!test_and_clear_bit(fed->fed_lr_idx, filter->fo_last_rcvd_slots)) {
+                CERROR("FILTER client %u: bit already clear in bitmap!!\n",
+                       fed->fed_lr_idx);
+                LBUG();
+        }
+
 free:
         OBD_FREE(fed->fed_fcd, sizeof(*fed->fed_fcd));
 
@@ -276,8 +301,7 @@ static int filter_free_server_data(struct filter_obd *filter)
 {
         OBD_FREE(filter->fo_fsd, sizeof(*filter->fo_fsd));
         filter->fo_fsd = NULL;
-        OBD_FREE(filter->fo_last_rcvd_slots,
-                 FILTER_LR_MAX_CLIENT_WORDS * sizeof(unsigned long));
+        OBD_FREE(filter->fo_last_rcvd_slots, FILTER_LR_MAX_CLIENTS/8);
         filter->fo_last_rcvd_slots = NULL;
         return 0;
 }
@@ -312,6 +336,12 @@ int filter_update_last_objid(struct obd_device *obd, obd_gr group,
         int rc;
         ENTRY;
 
+        if (filter->fo_last_objid_files[group] == NULL) {
+                CERROR("Object group "LPU64" not fully setup; not updating "
+                       "last_objid\n", group);
+                RETURN(0);
+        }
+
         CDEBUG(D_INODE, "server last_objid for group "LPU64": "LPU64"\n",
                group, filter->fo_last_objids[group]);
 
@@ -348,8 +378,7 @@ static int filter_init_server_data(struct obd_device *obd, struct file * filp)
                 RETURN(-ENOMEM);
         filter->fo_fsd = fsd;
 
-        OBD_ALLOC(filter->fo_last_rcvd_slots,
-                  FILTER_LR_MAX_CLIENT_WORDS * sizeof(unsigned long));
+        OBD_ALLOC(filter->fo_last_rcvd_slots, FILTER_LR_MAX_CLIENTS/8);
         if (filter->fo_last_rcvd_slots == NULL) {
                 OBD_FREE(fsd, sizeof(*fsd));
                 RETURN(-ENOMEM);
@@ -373,7 +402,7 @@ static int filter_init_server_data(struct obd_device *obd, struct file * filp)
                                LAST_RCVD, rc);
                         GOTO(err_fsd, rc);
                 }
-                if (strcmp(fsd->fsd_uuid, obd->obd_uuid.uuid) != 0) {
+                if (strcmp((char *)fsd->fsd_uuid, (char *)obd->obd_uuid.uuid)) {
                         CERROR("OBD UUID %s does not match last_rcvd UUID %s\n",
                                obd->obd_uuid.uuid, fsd->fsd_uuid);
                         GOTO(err_fsd, rc = -EINVAL);
@@ -382,13 +411,13 @@ static int filter_init_server_data(struct obd_device *obd, struct file * filp)
                 filter->fo_subdir_count = le16_to_cpu(fsd->fsd_subdir_count);
         }
 
-        if (fsd->fsd_feature_incompat & ~le32_to_cpu(FILTER_INCOMPAT_SUPP)) {
+        if (fsd->fsd_feature_incompat & ~cpu_to_le32(FILTER_INCOMPAT_SUPP)) {
                 CERROR("unsupported feature %x\n",
                        le32_to_cpu(fsd->fsd_feature_incompat) &
                        ~FILTER_INCOMPAT_SUPP);
                 GOTO(err_fsd, rc = -EINVAL);
         }
-        if (fsd->fsd_feature_rocompat & ~le32_to_cpu(FILTER_ROCOMPAT_SUPP)) {
+        if (fsd->fsd_feature_rocompat & ~cpu_to_le32(FILTER_ROCOMPAT_SUPP)) {
                 CERROR("read-only feature %x\n",
                        le32_to_cpu(fsd->fsd_feature_rocompat) &
                        ~FILTER_ROCOMPAT_SUPP);
@@ -398,8 +427,8 @@ static int filter_init_server_data(struct obd_device *obd, struct file * filp)
 
         CDEBUG(D_INODE, "%s: server last_rcvd : "LPU64"\n",
                obd->obd_name, le64_to_cpu(fsd->fsd_last_transno));
-        CDEBUG(D_INODE, "%s: server last_mount: "LPU64"\n",
-               obd->obd_name, mount_count);
+        CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
+               obd->obd_name, mount_count + 1);
         CDEBUG(D_INODE, "%s: server data size: %u\n",
                obd->obd_name, le32_to_cpu(fsd->fsd_server_size));
         CDEBUG(D_INODE, "%s: per-client data start: %u\n",
@@ -409,8 +438,9 @@ static int filter_init_server_data(struct obd_device *obd, struct file * filp)
         CDEBUG(D_INODE, "%s: server subdir_count: %u\n",
                obd->obd_name, le16_to_cpu(fsd->fsd_subdir_count));
         CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
-               last_rcvd_size <= FILTER_LR_CLIENT_START ? 0 :
-               (last_rcvd_size-FILTER_LR_CLIENT_START) /FILTER_LR_CLIENT_SIZE);
+               last_rcvd_size <= le32_to_cpu(fsd->fsd_client_start) ? 0 :
+               (last_rcvd_size - le32_to_cpu(fsd->fsd_client_start)) /
+                le16_to_cpu(fsd->fsd_client_size));
 
         if (!obd->obd_replayable) {
                 CWARN("%s: recovery support OFF\n", obd->obd_name);
@@ -420,7 +450,8 @@ static int filter_init_server_data(struct obd_device *obd, struct file * filp)
         for (cl_idx = 0, off = le32_to_cpu(fsd->fsd_client_start);
              off < last_rcvd_size; cl_idx++) {
                 __u64 last_rcvd;
-                int mount_age;
+                struct obd_export *exp;
+                struct filter_export_data *fed;
 
                 if (!fcd) {
                         OBD_ALLOC(fcd, sizeof(*fcd));
@@ -451,63 +482,64 @@ static int filter_init_server_data(struct obd_device *obd, struct file * filp)
                 /* These exports are cleaned up by filter_disconnect(), so they
                  * need to be set up like real exports as filter_connect() does.
                  */
-                mount_age = mount_count - le64_to_cpu(fcd->fcd_mount_count);
-                if (mount_age < FILTER_MOUNT_RECOV) {
-                        struct obd_export *exp = class_new_export(obd);
-                        struct filter_export_data *fed;
-                        CERROR("RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
-                               " srv lr: "LPU64" mnt: "LPU64" last mount: "
-                               LPU64"\n", fcd->fcd_uuid, cl_idx,
-                               last_rcvd, le64_to_cpu(fsd->fsd_last_transno),
-                               le64_to_cpu(fcd->fcd_mount_count), mount_count);
-                        if (exp == NULL)
-                                GOTO(err_client, rc = -ENOMEM);
-
-                        memcpy(&exp->exp_client_uuid.uuid, fcd->fcd_uuid,
-                               sizeof exp->exp_client_uuid.uuid);
-                        fed = &exp->exp_filter_data;
-                        fed->fed_fcd = fcd;
-                        filter_client_add(obd, filter, fed, cl_idx);
-                        /* create helper if export init gets more complex */
-                        spin_lock_init(&fed->fed_lock);
-
-                        fcd = NULL;
-                        obd->obd_recoverable_clients++;
-                        class_export_put(exp);
-                } else {
-                        CDEBUG(D_INFO, "discarded client %d UUID '%s' count "
-                               LPU64"\n", cl_idx, fcd->fcd_uuid,
-                               le64_to_cpu(fcd->fcd_mount_count));
-                }
+                exp = class_new_export(obd);
+                CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64" "
+                       "srv lr: "LPU64" fcd_group %d \n", fcd->fcd_uuid, cl_idx,
+                       last_rcvd, le64_to_cpu(fsd->fsd_last_transno), 
+                       le32_to_cpu(fcd->fcd_group));
+                if (exp == NULL)
+                        GOTO(err_client, rc = -ENOMEM);
+
+                memcpy(&exp->exp_client_uuid.uuid, fcd->fcd_uuid,
+                       sizeof exp->exp_client_uuid.uuid);
+                fed = &exp->exp_filter_data;
+                fed->fed_fcd = fcd;
+                fed->fed_group = le32_to_cpu(fcd->fcd_group);
+                filter_client_add(obd, filter, fed, cl_idx);
+                /* create helper if export init gets more complex */
+                spin_lock_init(&fed->fed_lock);
+
+                fcd = NULL;
+                exp->exp_connected = 0;
+                exp->exp_req_replay_needed = 1;
+                exp->exp_lock_replay_needed = 1;
+                atomic_inc(&obd->obd_req_replay_clients);
+                atomic_inc(&obd->obd_lock_replay_clients);
+                obd->obd_recoverable_clients++;
+                obd->obd_max_recoverable_clients++;
+                class_export_put(exp);
 
                 CDEBUG(D_OTHER, "client at idx %d has last_rcvd = "LPU64"\n",
                        cl_idx, last_rcvd);
 
-                if (last_rcvd > le64_to_cpu(filter->fo_fsd->fsd_last_transno))
-                        filter->fo_fsd->fsd_last_transno=cpu_to_le64(last_rcvd);
+                if (last_rcvd > le64_to_cpu(fsd->fsd_last_transno))
+                        fsd->fsd_last_transno = cpu_to_le64(last_rcvd);
 
         }
 
-        obd->obd_last_committed = le64_to_cpu(filter->fo_fsd->fsd_last_transno);
+        obd->obd_last_committed = le64_to_cpu(fsd->fsd_last_transno);
 
         if (obd->obd_recoverable_clients) {
-                CERROR("RECOVERY: %d recoverable clients, last_rcvd "
-                       LPU64"\n", obd->obd_recoverable_clients,
-                       le64_to_cpu(filter->fo_fsd->fsd_last_transno));
+                CWARN("RECOVERY: service %s, %d recoverable clients, "
+                      "last_transno "LPU64"\n", obd->obd_name,
+                      obd->obd_recoverable_clients,
+                      le64_to_cpu(fsd->fsd_last_transno));
                 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
-                obd->obd_recovering = 1;
+                target_start_recovery_thread(obd, ost_handle);
         }
 
         if (fcd)
                 OBD_FREE(fcd, sizeof(*fcd));
 
 out:
-        fsd->fsd_mount_count = cpu_to_le64(mount_count + 1);
+        filter->fo_mount_count = mount_count + 1;
+        fsd->fsd_mount_count = cpu_to_le64(filter->fo_mount_count);
 
         /* save it, so mount count and last_transno is current */
         rc = filter_update_server_data(obd, filp, filter->fo_fsd, 1);
-
-        RETURN(rc);
+        if (rc)
+                GOTO(err_client, rc);
+        RETURN(0);
 
 err_client:
         class_disconnect_exports(obd, 0);
@@ -519,57 +551,277 @@ err_fsd:
 static int filter_cleanup_groups(struct obd_device *obd)
 {
         struct filter_obd *filter = &obd->u.filter;
-        int i;
+        struct dentry *dentry;
+        int i, k;
         ENTRY;
 
-        if (filter->fo_subdir_count) {
-                for (i = 0; i < filter->fo_subdir_count; i++) {
-                        struct dentry *dentry = filter->fo_dentry_O_sub[i];
-                        f_dput(dentry);
-                        filter->fo_dentry_O_sub[i] = NULL;
-                }
-                OBD_FREE(filter->fo_dentry_O_sub,
-                         filter->fo_subdir_count *
-                         sizeof(*filter->fo_dentry_O_sub));
-        }
-        if (filter->fo_dentry_O_groups != NULL &&
-            filter->fo_last_objids != NULL &&
-            filter->fo_last_objid_files != NULL) {
-                for (i = 0; i < FILTER_GROUPS; i++) {
-                        struct dentry *dentry = filter->fo_dentry_O_groups[i];
-                        struct file *filp = filter->fo_last_objid_files[i];
-                        if (dentry != NULL) {
+        for (i = 0; i < filter->fo_group_count; i++) {
+                if (filter->fo_subdirs != NULL) {
+                        for (k = 0; k < filter->fo_subdir_count; k++) {
+                                dentry = filter->fo_subdirs[i].dentry[k];
+                                if (dentry == NULL)
+                                        continue;
                                 f_dput(dentry);
-                                filter->fo_dentry_O_groups[i] = NULL;
-                        }
-                        if (filp != NULL) {
-                                filp_close(filp, 0);
-                                filter->fo_last_objid_files[i] = NULL;
+                                filter->fo_subdirs[i].dentry[k] = NULL;
                         }
                 }
+                if (filter->fo_last_objid_files[i] != NULL) {
+                        filp_close(filter->fo_last_objid_files[i], 0);
+                        filter->fo_last_objid_files[i] = NULL;
+                }
+                if (filter->fo_groups[i] != NULL) {
+                        dput(filter->fo_groups[i]);
+                        filter->fo_groups[i] = NULL;
+                }
         }
-        if (filter->fo_dentry_O_groups != NULL)
-                OBD_FREE(filter->fo_dentry_O_groups,
-                         FILTER_GROUPS * sizeof(struct dentry *));
+        if (filter->fo_subdirs != NULL)
+                OBD_FREE(filter->fo_subdirs,
+                         filter->fo_group_count * sizeof(*filter->fo_subdirs));
+        if (filter->fo_groups != NULL)
+                OBD_FREE(filter->fo_groups,
+                         filter->fo_group_count * sizeof(*filter->fo_groups));
         if (filter->fo_last_objids != NULL)
                 OBD_FREE(filter->fo_last_objids,
-                         FILTER_GROUPS * sizeof(__u64));
+                         filter->fo_group_count * sizeof(__u64));
         if (filter->fo_last_objid_files != NULL)
                 OBD_FREE(filter->fo_last_objid_files,
-                         FILTER_GROUPS * sizeof(struct file *));
+                         filter->fo_group_count * sizeof(struct file *));
+        f_dput(filter->fo_dentry_O);
+        RETURN(0);
+}
+
+static int filter_update_last_group(struct obd_device *obd, int group)
+{
+        struct filter_obd *filter = &obd->u.filter;
+        struct file *filp = NULL;
+        int last_group = 0, rc;
+        loff_t off = 0;
+        ENTRY;
+
+        if (group <= filter->fo_committed_group)
+                RETURN(0);
+
+        filp = filp_open("LAST_GROUP", O_RDWR, 0700);
+        if (IS_ERR(filp)) {
+                rc = PTR_ERR(filp);
+                filp = NULL;
+                CERROR("cannot open LAST_GROUP: rc = %d\n", rc);
+                GOTO(cleanup, rc);
+        }
+
+        rc = fsfilt_read_record(obd, filp, &last_group, sizeof(__u32), &off);
+        if (rc) {
+                CDEBUG(D_INODE, "error reading LAST_GROUP: rc %d\n",rc);
+                GOTO(cleanup, rc);
+        }
+        LASSERT(off == 0 || last_group >= FILTER_MIN_GROUPS);
+        CDEBUG(D_INODE, "%s: previous %d, new %d\n",
+               obd->obd_name, last_group, group);
+
+        off = 0;
+        last_group = group;
+        /* must be sync: bXXXX */
+        rc = fsfilt_write_record(obd, filp, &last_group, sizeof(__u32), &off, 1);
+        if (rc) {
+                CDEBUG(D_INODE, "error updating LAST_GROUP: rc %d\n", rc);
+                GOTO(cleanup, rc);
+        }
+
+        filter->fo_committed_group = group;
+cleanup:
+        if (filp)
+                filp_close(filp, 0);
+        RETURN(rc);
+}
+
+static int filter_read_group_internal(struct obd_device *obd, int group,
+                                      int create)
+{
+        struct filter_obd *filter = &obd->u.filter;
+        __u64 *new_objids = NULL;
+        struct filter_subdirs *new_subdirs = NULL, *tmp_subdirs = NULL;
+        struct dentry **new_groups = NULL;
+        struct file **new_files = NULL;
+        struct dentry *dentry;
+        struct file *filp;
+        int old_count = filter->fo_group_count, rc, stage = 0, i;
+        char name[25];
+        __u64 last_objid;
+        loff_t off = 0;
+
+        snprintf(name, 24, "%d", group);
+        name[24] = '\0';
+
+        if (!create) {
+                dentry = ll_lookup_one_len(name, filter->fo_dentry_O,
+                                           strlen(name));
+                if (IS_ERR(dentry)) {
+                        CERROR("Cannot lookup expected object group %d: %ld\n",
+                               group, PTR_ERR(dentry));
+                        RETURN(PTR_ERR(dentry));
+                }
+        } else {
+                dentry = simple_mkdir(filter->fo_dentry_O, name, 0700, 1);
+                if (IS_ERR(dentry)) {
+                        CERROR("cannot lookup/create O/%s: rc = %ld\n", name,
+                               PTR_ERR(dentry));
+                        RETURN(PTR_ERR(dentry));
+                }
+        }
+        stage = 1;
+
+        snprintf(name, 24, "O/%d/LAST_ID", group);
+        name[24] = '\0';
+        filp = filp_open(name, O_CREAT | O_RDWR, 0700);
+        if (IS_ERR(filp)) {
+                CERROR("cannot create %s: rc = %ld\n", name, PTR_ERR(filp));
+                GOTO(cleanup, rc = PTR_ERR(filp));
+        }
+        stage = 2;
+
+        rc = fsfilt_read_record(obd, filp, &last_objid, sizeof(__u64), &off);
+        if (rc) {
+                CDEBUG(D_INODE, "error reading %s: rc %d\n", name, rc);
+                GOTO(cleanup, rc);
+        }
+
+        if (filter->fo_subdir_count) {
+                OBD_ALLOC(tmp_subdirs, sizeof(*tmp_subdirs));
+                if (tmp_subdirs == NULL)
+                        GOTO(cleanup, rc = -ENOMEM);
+                stage = 3;
+
+                for (i = 0; i < filter->fo_subdir_count; i++) {
+                        char dir[20];
+                        snprintf(dir, sizeof(dir), "d%u", i);
+
+                        tmp_subdirs->dentry[i] =
+                                simple_mkdir(dentry, dir, 0700, 1);
+                        if (IS_ERR(tmp_subdirs->dentry[i])) {
+                                rc = PTR_ERR(tmp_subdirs->dentry[i]);
+                                CERROR("can't lookup/create O/%d/%s: rc = %d\n",
+                                       group, dir, rc);
+                                GOTO(cleanup, rc);
+                        }
+                        CDEBUG(D_INODE, "got/created O/%d/%s: %p\n", group, dir,
+                               tmp_subdirs->dentry[i]);
+                }
+        }
+
+        /* 'group' is an index; we need an array of length 'group + 1' */
+        if (group + 1 > old_count) {
+                int len = group + 1;
+                OBD_ALLOC(new_objids, len * sizeof(*new_objids));
+                OBD_ALLOC(new_subdirs, len * sizeof(*new_subdirs));
+                OBD_ALLOC(new_groups, len * sizeof(*new_groups));
+                OBD_ALLOC(new_files, len * sizeof(*new_files));
+                stage = 4;
+                if (new_objids == NULL || new_subdirs == NULL ||
+                    new_groups == NULL || new_files == NULL)
+                        GOTO(cleanup, rc = -ENOMEM);
+
+                memcpy(new_objids, filter->fo_last_objids,
+                       old_count * sizeof(*new_objids));
+                memcpy(new_subdirs, filter->fo_subdirs,
+                       old_count * sizeof(*new_subdirs));
+                memcpy(new_groups, filter->fo_groups,
+                       old_count * sizeof(*new_groups));
+                memcpy(new_files, filter->fo_last_objid_files,
+                       old_count * sizeof(*new_files));
+
+                if (old_count) {
+                        OBD_FREE(filter->fo_last_objids,
+                                 old_count * sizeof(*new_objids));
+                        OBD_FREE(filter->fo_subdirs,
+                                 old_count * sizeof(*new_subdirs));
+                        OBD_FREE(filter->fo_groups,
+                                 old_count * sizeof(*new_groups));
+                        OBD_FREE(filter->fo_last_objid_files,
+                                 old_count * sizeof(*new_files));
+                }
+                filter->fo_last_objids = new_objids;
+                filter->fo_subdirs = new_subdirs;
+                filter->fo_groups = new_groups;
+                filter->fo_last_objid_files = new_files;
+                filter->fo_group_count = len;
+        }
+
+        filter->fo_groups[group] = dentry;
+        filter->fo_last_objid_files[group] = filp;
+        if (filter->fo_subdir_count) {
+                filter->fo_subdirs[group] = *tmp_subdirs;
+                OBD_FREE(tmp_subdirs, sizeof(*tmp_subdirs));
+        }
+
+        filter_update_last_group(obd, group);
+        
+        if (filp->f_dentry->d_inode->i_size == 0) {
+                filter->fo_last_objids[group] = FILTER_INIT_OBJID;
+                RETURN(0);
+        }
+
+        filter->fo_last_objids[group] = le64_to_cpu(last_objid);
+        CDEBUG(D_INODE, "%s: server last_objid group %d: "LPU64"\n",
+               obd->obd_name, group, last_objid);
         RETURN(0);
+ cleanup:
+        switch (stage) {
+        case 4:
+                if (new_objids != NULL)
+                        OBD_FREE(new_objids, group * sizeof(*new_objids));
+                if (new_subdirs != NULL)
+                        OBD_FREE(new_subdirs, group * sizeof(*new_subdirs));
+                if (new_groups != NULL)
+                        OBD_FREE(new_groups, group * sizeof(*new_groups));
+                if (new_files != NULL)
+                        OBD_FREE(new_files, group * sizeof(*new_files));
+        case 3:
+                if (filter->fo_subdir_count) {
+                        for (i = 0; i < filter->fo_subdir_count; i++) {
+                                if (tmp_subdirs->dentry[i] != NULL)
+                                        dput(tmp_subdirs->dentry[i]);
+                        }
+                        OBD_FREE(tmp_subdirs, sizeof(*tmp_subdirs));
+                }
+        case 2:
+                filp_close(filp, 0);
+        case 1:
+                dput(dentry);
+        }
+        RETURN(rc);
+}
+
+static int filter_read_groups(struct obd_device *obd, int last_group,
+                              int create)
+{
+        struct filter_obd *filter = &obd->u.filter;
+        int old_count, group, rc = 0;
+
+        down(&filter->fo_init_lock);
+        old_count = filter->fo_group_count;
+        for (group = old_count; group <= last_group; group++) {
+                if (group == 0)
+                        continue; /* no group zero */
+
+                rc = filter_read_group_internal(obd, group, create);
+                if (rc != 0)
+                        break;
+        }
+        up(&filter->fo_init_lock);
+        return rc;
 }
 
-/* FIXME: object groups */
 static int filter_prep_groups(struct obd_device *obd)
 {
         struct filter_obd *filter = &obd->u.filter;
         struct dentry *dentry, *O_dentry;
-        struct file *filp;
-        int i, rc = 0, cleanup_phase = 0;
+        int rc = 0, cleanup_phase = 0;
+        struct file *filp = NULL;
+        int last_group;
+        loff_t off = 0;
         ENTRY;
 
-        O_dentry = simple_mkdir(current->fs->pwd, "O", 0700);
+        O_dentry = simple_mkdir(current->fs->pwd, "O", 0700, 1);
         CDEBUG(D_INODE, "got/created O: %p\n", O_dentry);
         if (IS_ERR(O_dentry)) {
                 rc = PTR_ERR(O_dentry);
@@ -618,104 +870,51 @@ static int filter_prep_groups(struct obd_device *obd)
                 GOTO(cleanup_O0, rc);
 
         cleanup_O0:
-                dput(O0_dentry);
+                f_dput(O0_dentry);
         cleanup_R:
-                dput(dentry);
+                f_dput(dentry);
                 if (rc)
                         GOTO(cleanup, rc);
         } else {
-                dput(dentry);
+                f_dput(dentry);
         }
 
-        OBD_ALLOC(filter->fo_last_objids, FILTER_GROUPS * sizeof(__u64));
-        if (filter->fo_last_objids == NULL)
-                GOTO(cleanup, rc = -ENOMEM);
         cleanup_phase = 2; /* groups */
 
-        OBD_ALLOC(filter->fo_dentry_O_groups, FILTER_GROUPS * sizeof(dentry));
-        if (filter->fo_dentry_O_groups == NULL)
-                GOTO(cleanup, rc = -ENOMEM);
-        OBD_ALLOC(filter->fo_last_objid_files, FILTER_GROUPS * sizeof(filp));
-        if (filter->fo_last_objid_files == NULL)
-                GOTO(cleanup, rc = -ENOMEM);
-
-        for (i = 0; i < FILTER_GROUPS; i++) {
-                char name[25];
-                loff_t off = 0;
-
-                sprintf(name, "%d", i);
-                dentry = simple_mkdir(O_dentry, name, 0700);
-                CDEBUG(D_INODE, "got/created O/%s: %p\n", name, dentry);
-                if (IS_ERR(dentry)) {
-                        rc = PTR_ERR(dentry);
-                        CERROR("cannot create O/%s: rc = %d\n", name, rc);
-                        GOTO(cleanup, rc);
-                }
-                filter->fo_dentry_O_groups[i] = dentry;
-
-                sprintf(name, "O/%d/LAST_ID", i);
-                filp = filp_open(name, O_CREAT | O_RDWR, 0700);
-                if (IS_ERR(dentry)) {
-                        rc = PTR_ERR(dentry);
-                        CERROR("cannot create %s: rc = %d\n", name, rc);
-                        GOTO(cleanup, rc);
-                }
-                filter->fo_last_objid_files[i] = filp;
-
-                if (filp->f_dentry->d_inode->i_size == 0) {
-                        if (i == 0 && filter->fo_fsd->fsd_unused != 0) {
-                                /* OST conversion, remove sometime post 1.0 */
-                                filter->fo_last_objids[i] =
-                                        le64_to_cpu(filter->fo_fsd->fsd_unused);
-                                CWARN("saving old objid "LPU64" to LAST_ID\n",
-                                      filter->fo_last_objids[i]);
-                                rc = filter_update_last_objid(obd, 0, 1);
-                                if (rc)
-                                        GOTO(cleanup, rc);
-                        } else {
-                                filter->fo_last_objids[i] = FILTER_INIT_OBJID;
-                        }
-                        continue;
-                }
-
-                rc = fsfilt_read_record(obd, filp, &filter->fo_last_objids[i],
-                                        sizeof(__u64), &off);
-                if (rc) {
-                        CDEBUG(D_INODE,"OBD filter: error reading %s: rc %d\n",
-                               name, rc);
-                        GOTO(cleanup, rc);
-                }
-                filter->fo_last_objids[i] =
-                        le64_to_cpu(filter->fo_last_objids[i]);
-                CDEBUG(D_INODE, "%s: server last_objid group %d: "LPU64"\n",
-                       obd->obd_name, i, filter->fo_last_objids[i]);
+        /* we have to initialize all groups before first connections from
+         * clients because they may send create/destroy for any group -bzzz */
+        filp = filp_open("LAST_GROUP", O_CREAT | O_RDWR, 0700);
+        if (IS_ERR(filp)) {
+                CERROR("cannot create LAST_GROUP: rc = %ld\n", PTR_ERR(filp));
+                GOTO(cleanup, rc = PTR_ERR(filp));
         }
+        cleanup_phase = 3; /* filp */
 
-        if (filter->fo_subdir_count) {
-                O_dentry = filter->fo_dentry_O_groups[0];
-                OBD_ALLOC(filter->fo_dentry_O_sub,
-                          filter->fo_subdir_count * sizeof(dentry));
-                if (filter->fo_dentry_O_sub == NULL)
-                        GOTO(cleanup, rc = -ENOMEM);
-
-                for (i = 0; i < filter->fo_subdir_count; i++) {
-                        char dir[20];
-                        snprintf(dir, sizeof(dir), "d%u", i);
-
-                        dentry = simple_mkdir(O_dentry, dir, 0700);
-                        CDEBUG(D_INODE, "got/created O/0/%s: %p\n", dir,dentry);
-                        if (IS_ERR(dentry)) {
-                                rc = PTR_ERR(dentry);
-                                CERROR("can't create O/0/%s: rc = %d\n",dir,rc);
-                                GOTO(cleanup, rc);
-                        }
-                        filter->fo_dentry_O_sub[i] = dentry;
-                }
+        rc = fsfilt_read_record(obd, filp, &last_group, sizeof(__u32), &off);
+        if (rc) {
+                CDEBUG(D_INODE, "error reading LAST_GROUP: rc %d\n", rc);
+                GOTO(cleanup, rc);
+        }
+        if (off == 0) {
+                last_group = FILTER_MIN_GROUPS;
+        } else {
+                LASSERT(last_group >= FILTER_MIN_GROUPS);
         }
+
+        CWARN("%s: initialize groups [%d,%d]\n", obd->obd_name,
+              FILTER_MIN_GROUPS, last_group);
+        filter->fo_committed_group = last_group;
+        rc = filter_read_groups(obd, last_group, 1);
+        if (rc)
+                GOTO(cleanup, rc);
+        
+        filp_close(filp, 0);
         RETURN(0);
 
  cleanup:
         switch (cleanup_phase) {
+        case 3:
+                filp_close(filp, 0);
         case 2:
                 filter_cleanup_groups(obd);
         case 1:
@@ -730,14 +929,14 @@ static int filter_prep_groups(struct obd_device *obd)
 /* setup the object store with correct subdirectories */
 static int filter_prep(struct obd_device *obd)
 {
-        struct obd_run_ctxt saved;
+        struct lvfs_run_ctxt saved;
         struct filter_obd *filter = &obd->u.filter;
         struct file *file;
         struct inode *inode;
         int rc = 0;
         ENTRY;
 
-        push_ctxt(&saved, &obd->obd_ctxt, NULL);
+        push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
         file = filp_open(LAST_RCVD, O_RDWR | O_CREAT | O_LARGEFILE, 0700);
         if (!file || IS_ERR(file)) {
                 rc = PTR_ERR(file);
@@ -770,7 +969,7 @@ static int filter_prep(struct obd_device *obd)
                 GOTO(err_server_data, rc);
 
  out:
-        pop_ctxt(&saved, &obd->obd_ctxt, NULL);
+        pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
 
         return(rc);
 
@@ -787,7 +986,7 @@ static int filter_prep(struct obd_device *obd)
 /* cleanup the filter: write last used object id to status file */
 static void filter_post(struct obd_device *obd)
 {
-        struct obd_run_ctxt saved;
+        struct lvfs_run_ctxt saved;
         struct filter_obd *filter = &obd->u.filter;
         int rc, i;
 
@@ -795,58 +994,48 @@ static void filter_post(struct obd_device *obd)
          * best to start a transaction with h_sync, because we removed this
          * from lastobjid */
 
-        push_ctxt(&saved, &obd->obd_ctxt, NULL);
+        push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
         rc = filter_update_server_data(obd, filter->fo_rcvd_filp,
                                        filter->fo_fsd, 0);
         if (rc)
                 CERROR("error writing server data: rc = %d\n", rc);
 
-        for (i = 0; i < FILTER_GROUPS; i++) {
-                rc = filter_update_last_objid(obd, i, (i == FILTER_GROUPS - 1));
+        for (i = 1; i < filter->fo_group_count; i++) {
+                rc = filter_update_last_objid(obd, i,
+                                             (i == filter->fo_group_count - 1));
                 if (rc)
                         CERROR("error writing group %d lastobjid: rc = %d\n",
                                i, rc);
         }
 
-        filp_close(filter->fo_rcvd_filp, 0);
+        rc = filp_close(filter->fo_rcvd_filp, 0);
         filter->fo_rcvd_filp = NULL;
         if (rc)
                 CERROR("error closing %s: rc = %d\n", LAST_RCVD, rc);
 
         filter_cleanup_groups(obd);
-        f_dput(filter->fo_dentry_O);
         filter_free_server_data(filter);
-        pop_ctxt(&saved, &obd->obd_ctxt, NULL);
+        pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
 }
 
-static void filter_set_last_id(struct filter_obd *filter, struct obdo *oa,
-                               obd_id id)
+static void filter_set_last_id(struct filter_obd *filter, int group, obd_id id)
 {
-        obd_gr group = 0;
         LASSERT(filter->fo_fsd != NULL);
-
-        if (oa != NULL) {
-                LASSERT(oa->o_gr <= FILTER_GROUPS);
-                group = oa->o_gr;
-        }
+        LASSERT(group > 0);
+        LASSERT(group < filter->fo_group_count);
 
         spin_lock(&filter->fo_objidlock);
         filter->fo_last_objids[group] = id;
         spin_unlock(&filter->fo_objidlock);
 }
 
-__u64 filter_last_id(struct filter_obd *filter, struct obdo *oa)
+__u64 filter_last_id(struct filter_obd *filter, int group)
 {
         obd_id id;
-        obd_gr group = 0;
         LASSERT(filter->fo_fsd != NULL);
+        LASSERT(group > 0);
+        LASSERT(group < filter->fo_group_count);
 
-        if (oa != NULL) {
-                LASSERT(oa->o_gr <= FILTER_GROUPS);
-                group = oa->o_gr;
-        }
-
-        /* FIXME: object groups */
         spin_lock(&filter->fo_objidlock);
         id = filter->fo_last_objids[group];
         spin_unlock(&filter->fo_objidlock);
@@ -899,59 +1088,67 @@ static int filter_blocking_ast(struct ldlm_lock *lock,
         RETURN(0);
 }
 
-static int filter_lock_dentry(struct obd_device *obd, struct dentry *de,
-                              ldlm_mode_t lock_mode,struct lustre_handle *lockh)
-{
-        struct ldlm_res_id res_id = { .name = {0} };
-        int flags = 0, rc;
-        ENTRY;
+extern void *lock_dir(struct inode *dir, struct qstr *name);
+extern void unlock_dir(struct inode *dir, void *lock);
 
-        res_id.name[0] = de->d_inode->i_ino;
-        res_id.name[1] = de->d_inode->i_generation;
-        rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
-                              res_id, LDLM_PLAIN, NULL, 0, lock_mode,
-                              &flags, ldlm_completion_ast,
-                              filter_blocking_ast, NULL, lockh);
+static void *filter_lock_dentry(struct obd_device *obd,
+                                struct dentry *dparent,
+                                obd_id id)
+{
+#ifdef S_PDIROPS
+        struct qstr qstr;
+        char name[32];
+        int len;
 
-        RETURN(rc == ELDLM_OK ? 0 : -EIO);  /* XXX translate ldlm code */
+        len = sprintf(name, LPU64, id);
+        qstr_assign(&qstr, (char *)name, len);
+        return lock_dir(dparent->d_inode, &qstr);
+#else
+        down(&dparent->d_inode->i_sem);
+#endif
+        return 0;
 }
 
 /* We never dget the object parent, so DON'T dput it either */
-static void filter_parent_unlock(struct dentry *dparent,
-                                 struct lustre_handle *lockh,
-                                 ldlm_mode_t lock_mode)
+static void filter_parent_unlock(struct dentry *dparent, void *lock)
 {
-        ldlm_lock_decref(lockh, lock_mode);
+#ifdef S_PDIROPS
+        LASSERT(lock != NULL);
+        unlock_dir(dparent->d_inode, lock);
+#else
+        up(&dparent->d_inode->i_sem);
+#endif
 }
 
 /* We never dget the object parent, so DON'T dput it either */
 struct dentry *filter_parent(struct obd_device *obd, obd_gr group, obd_id objid)
 {
         struct filter_obd *filter = &obd->u.filter;
-        LASSERT(group < FILTER_GROUPS); /* FIXME: object groups */
+        LASSERT(group < filter->fo_group_count);
+        LASSERT(group > 0);
 
-        if (group > 0 || filter->fo_subdir_count == 0)
-                return filter->fo_dentry_O_groups[group];
+        if (filter->fo_subdir_count == 0)
+                return filter->fo_groups[group];
 
-        return filter->fo_dentry_O_sub[objid & (filter->fo_subdir_count - 1)];
+        return filter->fo_subdirs[group].dentry[objid & (filter->fo_subdir_count - 1)];
 }
 
 /* We never dget the object parent, so DON'T dput it either */
 struct dentry *filter_parent_lock(struct obd_device *obd, obd_gr group,
-                                  obd_id objid, ldlm_mode_t lock_mode,
-                                  struct lustre_handle *lockh)
+                                  obd_id objid, void **lock)
 {
         unsigned long now = jiffies;
-        struct dentry *de = filter_parent(obd, group, objid);
-        int rc;
+        struct dentry *dparent = filter_parent(obd, group, objid);
+
+        if (IS_ERR(dparent))
+                return dparent;
 
-        if (IS_ERR(de))
-                return de;
+        LASSERT(dparent);
+        LASSERT(dparent->d_inode);
 
-        rc = filter_lock_dentry(obd, de, lock_mode, lockh);
-        if (time_after(jiffies, now + 15 * HZ))
-                CERROR("slow parent lock %lus\n", (jiffies - now) / HZ);
-        return rc ? ERR_PTR(rc) : de;
+        *lock = filter_lock_dentry(obd, dparent, objid);
+        fsfilt_check_slow(now, obd_timeout, "parent lock");
+        return dparent;
 }
 
 /* How to get files, dentries, inodes from object id's.
@@ -961,13 +1158,13 @@ struct dentry *filter_parent_lock(struct obd_device *obd, obd_gr group,
  * dir_dentry is NULL, we do a read lock while we do the lookup to
  * avoid races with create/destroy and such changing the directory
  * internal to the filesystem code. */
-struct dentry *filter_fid2dentry(struct obd_device *obd,
-                                 struct dentry *dir_dentry,
-                                 obd_gr group, obd_id id)
+struct dentry *filter_id2dentry(struct obd_device *obd,
+                                struct dentry *dir_dentry,
+                                obd_gr group, obd_id id)
 {
-        struct lustre_handle lockh;
         struct dentry *dparent = dir_dentry;
         struct dentry *dchild;
+        void *lock = NULL;
         char name[32];
         int len;
         ENTRY;
@@ -979,20 +1176,31 @@ struct dentry *filter_fid2dentry(struct obd_device *obd,
 
         len = sprintf(name, LPU64, id);
         if (dir_dentry == NULL) {
-                dparent = filter_parent_lock(obd, group, id, LCK_PR, &lockh);
-                if (IS_ERR(dparent))
+                dparent = filter_parent_lock(obd, group, id, &lock);
+                if (IS_ERR(dparent)) {
+                        CERROR("%s: error getting object "LPU64":"LPU64
+                               " parent: rc %ld\n", obd->obd_name,
+                               id, group, PTR_ERR(dparent));
                         RETURN(dparent);
+                }
         }
-        CDEBUG(D_INODE, "looking up object O/%*s/%s\n",
+        CDEBUG(D_INODE, "looking up object O/%.*s/%s\n",
                dparent->d_name.len, dparent->d_name.name, name);
-        dchild = ll_lookup_one_len(name, dparent, len);
+        dchild = /*ll_*/lookup_one_len(name, dparent, len);
         if (dir_dentry == NULL)
-                filter_parent_unlock(dparent, &lockh, LCK_PR);
+                filter_parent_unlock(dparent, lock);
         if (IS_ERR(dchild)) {
-                CERROR("child lookup error %ld\n", PTR_ERR(dchild));
+                CERROR("%s: child lookup error %ld\n", obd->obd_name,
+                       PTR_ERR(dchild));
                 RETURN(dchild);
         }
 
+        if (dchild->d_inode != NULL && is_bad_inode(dchild->d_inode)) {
+                CERROR("%s: got bad inode "LPU64"\n", obd->obd_name, id);
+                f_dput(dchild);
+                RETURN(ERR_PTR(-ENOENT));
+        }
+
         CDEBUG(D_INODE, "got child objid %s: %p, count = %d\n",
                name, dchild, atomic_read(&dchild->d_count));
 
@@ -1001,26 +1209,21 @@ struct dentry *filter_fid2dentry(struct obd_device *obd,
         RETURN(dchild);
 }
 
-static int filter_prepare_destroy(struct obd_device *obd, obd_id objid)
+static int filter_prepare_destroy(struct obd_device *obd, obd_id objid,
+                                  obd_id group)
 {
         struct lustre_handle lockh;
         int flags = LDLM_AST_DISCARD_DATA, rc;
-        struct ldlm_res_id res_id = { .name = { objid } };
-        struct ldlm_extent extent = { 0, OBD_OBJECT_EOF };
-        ENTRY;
+        struct ldlm_res_id res_id = { .name = { objid, 0, group, 0 } };
+        ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
 
+        ENTRY;
         /* Tell the clients that the object is gone now and that they should
-         * throw away any cached pages.  If we're the OST at stripe 0 in the
-         * file then this enqueue will communicate the DISCARD to all the
-         * clients.  This assumes that we always destroy all the objects for
-         * a file at a time, as is currently the case.  If we're not the
-         * OST at stripe 0 then we'll harmlessly get a very lonely lock in 
-         * the local DLM and immediately drop it. */
-        rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
-                              res_id, LDLM_EXTENT, &extent,
-                              sizeof(extent), LCK_PW, &flags,
-                              ldlm_completion_ast, filter_blocking_ast,
-                              NULL, &lockh);
+         * throw away any cached pages. */
+        rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, res_id,
+                              LDLM_EXTENT, &policy, LCK_PW,
+                              &flags, filter_blocking_ast, ldlm_completion_ast,
+                              NULL, NULL, NULL, 0, NULL, &lockh);
 
         /* We only care about the side-effects, just drop the lock. */
         if (rc == ELDLM_OK)
@@ -1040,148 +1243,370 @@ static int filter_destroy_internal(struct obd_device *obd, obd_id objid,
         ENTRY;
 
         if (inode->i_nlink != 1 || atomic_read(&inode->i_count) != 1) {
-                CERROR("destroying objid %*s nlink = %lu, count = %d\n",
+                CERROR("destroying objid %.*s nlink = %lu, count = %d\n",
                        dchild->d_name.len, dchild->d_name.name,
-                       (unsigned long)inode->i_nlink, 
+                       (unsigned long)inode->i_nlink,
                        atomic_read(&inode->i_count));
         }
 
         rc = vfs_unlink(dparent->d_inode, dchild);
 
         if (rc)
-                CERROR("error unlinking objid %*s: rc %d\n",
+                CERROR("error unlinking objid %.*s: rc %d\n",
                        dchild->d_name.len, dchild->d_name.name, rc);
 
         RETURN(rc);
 }
 
-/* mount the file system (secretly) */
-int filter_common_setup(struct obd_device *obd, obd_count len, void *buf,
-                        char *option)
+static int filter_intent_policy(struct ldlm_namespace *ns,
+                                struct ldlm_lock **lockp, void *req_cookie,
+                                ldlm_mode_t mode, int flags, void *data)
 {
-        struct lustre_cfg* lcfg = buf;
-        struct filter_obd *filter = &obd->u.filter;
-        struct vfsmount *mnt;
-        int rc = 0;
+        struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
+        struct ptlrpc_request *req = req_cookie;
+        struct ldlm_lock *lock = *lockp, *l = NULL;
+        struct ldlm_resource *res = lock->l_resource;
+        ldlm_processing_policy policy;
+        struct ost_lvb *res_lvb, *reply_lvb;
+        struct ldlm_reply *rep;
+        struct list_head *tmp;
+        ldlm_error_t err;
+        int tmpflags = 0, rc, repsize[2] = {sizeof(*rep), sizeof(*reply_lvb)};
         ENTRY;
 
-        dev_clear_rdonly(2);
+        policy = ldlm_get_processing_policy(res);
+        LASSERT(policy != NULL);
+        LASSERT(req != NULL);
 
-        if (!lcfg->lcfg_inlbuf1 || !lcfg->lcfg_inlbuf2)
-                RETURN(-EINVAL);
+        rc = lustre_pack_reply(req, 2, repsize, NULL);
+        if (rc)
+                RETURN(req->rq_status = rc);
 
-        obd->obd_fsops = fsfilt_get_ops(lcfg->lcfg_inlbuf2);
-        if (IS_ERR(obd->obd_fsops))
-                RETURN(PTR_ERR(obd->obd_fsops));
+        rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*rep));
+        LASSERT(rep != NULL);
 
-        mnt = do_kern_mount(lcfg->lcfg_inlbuf2, MS_NOATIME | MS_NODIRATIME,
-                            lcfg->lcfg_inlbuf1, option);
-        rc = PTR_ERR(mnt);
-        if (IS_ERR(mnt))
-                GOTO(err_ops, rc);
+        reply_lvb = lustre_msg_buf(req->rq_repmsg, 1, sizeof(*reply_lvb));
+        LASSERT(reply_lvb != NULL);
 
-        if (lcfg->lcfg_inllen3 > 0 && lcfg->lcfg_inlbuf3) {
-                if (*lcfg->lcfg_inlbuf3 == 'f') {
-                        obd->obd_replayable = 1;
-                        obd_sync_filter = 1;
-                        CERROR("%s: recovery enabled\n", obd->obd_name);
-                } else {
-                        if (*lcfg->lcfg_inlbuf3 != 'n') {
-                                CERROR("unrecognised flag '%c'\n",
-                                       *lcfg->lcfg_inlbuf3);
-                        }
-                        // XXX Robert? Why do we get errors here
-                        // GOTO(err_mntput, rc = -EINVAL);
-                }
-        }
+        //fixup_handle_for_resent_req(req, lock, &lockh);
 
-        filter->fo_vfsmnt = mnt;
-        filter->fo_sb = mnt->mnt_sb;
-        filter->fo_fstype = mnt->mnt_sb->s_type->name;
-        CDEBUG(D_SUPER, "%s: mnt = %p\n", filter->fo_fstype, mnt);
+        /* If we grant any lock at all, it will be a whole-file read lock.
+         * Call the extent policy function to see if our request can be
+         * granted, or is blocked. */
+        lock->l_policy_data.l_extent.start = 0;
+        lock->l_policy_data.l_extent.end = OBD_OBJECT_EOF;
+        lock->l_req_mode = LCK_PR;
 
-        OBD_SET_CTXT_MAGIC(&obd->obd_ctxt);
-        obd->obd_ctxt.pwdmnt = mnt;
-        obd->obd_ctxt.pwd = mnt->mnt_root;
-        obd->obd_ctxt.fs = get_ds();
-        obd->obd_ctxt.cb_ops = filter_lvfs_ops;
+        l_lock(&res->lr_namespace->ns_lock);
 
-        rc = filter_prep(obd);
-        if (rc)
-                GOTO(err_mntput, rc);
+        res->lr_tmp = &rpc_list;
+        rc = policy(lock, &tmpflags, 0, &err);
+        res->lr_tmp = NULL;
 
-        spin_lock_init(&filter->fo_translock);
-        spin_lock_init(&filter->fo_objidlock);
-        INIT_LIST_HEAD(&filter->fo_export_list);
-        sema_init(&filter->fo_alloc_lock, 1);
+        /* FIXME: we should change the policy function slightly, to not make
+         * this list at all, since we just turn around and free it */
+        while (!list_empty(&rpc_list)) {
+                struct ldlm_ast_work *w =
+                        list_entry(rpc_list.next, struct ldlm_ast_work, w_list);
+                list_del(&w->w_list);
+                LDLM_LOCK_PUT(w->w_lock);
+                OBD_FREE(w, sizeof(*w));
+        }
 
-        obd->obd_namespace = ldlm_namespace_new("filter-tgt",
-                                                LDLM_NAMESPACE_SERVER);
-        if (obd->obd_namespace == NULL)
-                GOTO(err_post, rc = -ENOMEM);
+        if (rc == LDLM_ITER_CONTINUE) {
+                /* The lock met with no resistance; we're finished. */
+                l_unlock(&res->lr_namespace->ns_lock);
+                RETURN(ELDLM_LOCK_REPLACED);
+        }
 
-        ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
-                           "filter_ldlm_cb_client", &obd->obd_ldlm_client);
+        /* Do not grant any lock, but instead send GL callbacks.  The extent
+         * policy nicely created a list of all PW locks for us.  We will choose
+         * the highest of those which are larger than the size in the LVB, if
+         * any, and perform a glimpse callback. */
+        down(&res->lr_lvb_sem);
+        res_lvb = res->lr_lvb_data;
+        LASSERT(res_lvb != NULL);
+        *reply_lvb = *res_lvb;
+        up(&res->lr_lvb_sem);
 
-        RETURN(0);
+        list_for_each(tmp, &res->lr_granted) {
+                struct ldlm_lock *tmplock =
+                        list_entry(tmp, struct ldlm_lock, l_res_link);
 
-err_post:
-        filter_post(obd);
-err_mntput:
-        unlock_kernel();
-        mntput(mnt);
-        filter->fo_sb = 0;
-        lock_kernel();
-err_ops:
-        fsfilt_put_ops(obd->obd_fsops);
-        return rc;
-}
+                if (tmplock->l_granted_mode == LCK_PR)
+                        continue;
 
-static int filter_setup(struct obd_device *obd, obd_count len, void *buf)
-{
-        struct lustre_cfg* lcfg = buf;
-        const char *str = NULL;
-        char *option = NULL;
-        int n = 0;
-        int rc;
+                if (tmplock->l_policy_data.l_extent.end <= reply_lvb->lvb_size)
+                        continue;
 
-        if (!strcmp(lcfg->lcfg_inlbuf2, "ext3")) {
-#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
-        /* bug 1577: implement async-delete for 2.5 */
-                str = "errors=remount-ro,asyncdel";
-#else
-                str = "errors=remount-ro";
-#endif
-                n = strlen(str) + 1;
-                OBD_ALLOC(option, n);
-                if (option == NULL)
-                        RETURN(-ENOMEM);
-                strcpy(option, str);
+                if (l == NULL) {
+                        l = LDLM_LOCK_GET(tmplock);
+                        continue;
+                }
+
+                if (l->l_policy_data.l_extent.start >
+                    tmplock->l_policy_data.l_extent.start)
+                        continue;
+
+                LDLM_LOCK_PUT(l);
+                l = LDLM_LOCK_GET(tmplock);
         }
+        l_unlock(&res->lr_namespace->ns_lock);
 
-        rc = filter_common_setup(obd, len, buf, option);
-        if (option)
-                OBD_FREE(option, n);
-        return rc;
-}
+        /* There were no PW locks beyond the size in the LVB; finished. */
+        if (l == NULL)
+                RETURN(ELDLM_LOCK_ABORTED);
 
-static int filter_postsetup(struct obd_device *obd)
+        if (l->l_glimpse_ast == NULL) {
+                /* We are racing with unlink(); just return -ENOENT */
+                rep->lock_policy_res1 = -ENOENT;
+                goto out;
+        }
+
+        LASSERTF(l->l_glimpse_ast != NULL, "l == %p", l);
+
+        rc = l->l_glimpse_ast(l, NULL); /* this will update the LVB */
+        if (rc != 0 && res->lr_namespace->ns_lvbo &&
+            res->lr_namespace->ns_lvbo->lvbo_update) {
+                res->lr_namespace->ns_lvbo->lvbo_update(res, NULL, 0, 1);
+        }
+
+        down(&res->lr_lvb_sem);
+        *reply_lvb = *res_lvb;
+        up(&res->lr_lvb_sem);
+out:
+        LDLM_LOCK_PUT(l);
+
+        RETURN(ELDLM_LOCK_ABORTED);
+}
+
+static int filter_post_fs_cleanup(struct obd_device *obd)
 {
         int rc = 0;
+
+        rc = fsfilt_post_cleanup(obd);
+
+        RETURN(rc);
+}
+
+#if 0
+static int filter_group_set_fs_flags(struct obd_device *obd, int group)
+{
+        struct filter_obd *filter = &obd->u.filter;
+        int rc = 0, i = 0;
+        ENTRY;        
+        
+        /* zero group is not longer valid. */
+        if (group== 0)
+                RETURN(rc); 
+        for (i = 0; i < filter->fo_subdir_count; i++) {
+                struct dentry *dentry;
+                dentry = (filter->fo_subdirs + group)->dentry[i];
+                rc = fsfilt_set_fs_flags(obd, dentry->d_inode, 
+                                         SM_DO_REC | SM_DO_COW);
+                if (rc)
+                        RETURN(rc);
+        }
+        RETURN(rc);
+}
+#endif
+
+static int filter_post_fs_setup(struct obd_device *obd)
+{
+        struct filter_obd *filter = &obd->u.filter;
+        int rc = 0;
+        
+        rc = fsfilt_post_setup(obd, filter->fo_dentry_O);
+
+        return rc;
+}
+
+/* mount the file system (secretly) */
+int filter_common_setup(struct obd_device *obd, obd_count len, void *buf,
+                        char *option)
+{
+        struct lustre_cfg *lcfg = buf;
+        struct filter_obd *filter = &obd->u.filter;
+        struct lvfs_obd_ctxt *lvfs_ctxt = NULL;
+        struct vfsmount *mnt;
+        char *str;
+        char ns_name[48];
+        int rc = 0, i;
         ENTRY;
 
-        // XXX add a storage location for the logid for size changes
-#ifdef ENABLE_ORPHANS
-        rc = llog_cat_initialize(obd, 1);
+        if ((LUSTRE_CFG_BUFLEN(lcfg, 1)) < 1 || 
+            (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1)) 
+                RETURN(-EINVAL);
+
+        obd->obd_fsops = fsfilt_get_ops(lustre_cfg_string(lcfg, 2));
+        if (IS_ERR(obd->obd_fsops))
+                RETURN(PTR_ERR(obd->obd_fsops));
+
+        rc = lvfs_mount_fs(lustre_cfg_string(lcfg, 1), 
+                           lustre_cfg_string(lcfg, 2), 
+                           option, MS_NOATIME | MS_NODIRATIME, &lvfs_ctxt);
+        if (rc) {
+                CERROR("lvfs_mount_fs failed: rc = %d\n", rc);
+                GOTO(err_ops, rc);
+        }
+        LASSERT(lvfs_ctxt);
+
+        mnt = lvfs_ctxt->loc_mnt;
+        filter->fo_lvfs_ctxt = lvfs_ctxt;
+
+        if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0 && lustre_cfg_buf(lcfg, 3)) {
+                str = lustre_cfg_string(lcfg, 3);
+                if (*str == 'f') {
+                        obd->obd_replayable = 1;
+                        obd_sync_filter = 1;
+                        CWARN("%s: recovery enabled\n", obd->obd_name);
+                } else {
+                        if (*str != 'n') {
+                                CERROR("unrecognised flag '%c'\n",
+                                       *str);
+                        }
+                        // XXX Robert? Why do we get errors here
+                        // GOTO(err_mntput, rc = -EINVAL);
+                }
+        }
+
+        filter->fo_vfsmnt = mnt;
+        filter->fo_sb = mnt->mnt_sb;
+        filter->fo_fstype = mnt->mnt_sb->s_type->name;
+        CDEBUG(D_SUPER, "%s: mnt = %p\n", filter->fo_fstype, mnt);
+
+        OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
+        obd->obd_lvfs_ctxt.pwdmnt = mnt;
+        obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
+        obd->obd_lvfs_ctxt.fs = get_ds();
+        obd->obd_lvfs_ctxt.cb_ops = filter_lvfs_ops;
+
+        ll_clear_rdonly(ll_sbdev(filter->fo_sb));
+        
+        rc = fsfilt_setup(obd, mnt->mnt_sb);
+        if (rc)
+                GOTO(err_mntput, rc);
+
+        sema_init(&filter->fo_init_lock, 1);
+        filter->fo_committed_group = 0;
+        rc = filter_prep(obd);
         if (rc)
+                GOTO(err_mntput, rc);
+
+        filter->fo_destroys_in_progress = 0;
+        for (i = 0; i < 32; i++)
+                sema_init(&filter->fo_create_locks[i], 1);
+
+        spin_lock_init(&filter->fo_translock);
+        spin_lock_init(&filter->fo_objidlock);
+        INIT_LIST_HEAD(&filter->fo_export_list);
+        sema_init(&filter->fo_alloc_lock, 1);
+        spin_lock_init(&filter->fo_r_pages.oh_lock);
+        spin_lock_init(&filter->fo_w_pages.oh_lock);
+        spin_lock_init(&filter->fo_r_discont_pages.oh_lock);
+        spin_lock_init(&filter->fo_w_discont_pages.oh_lock);
+        spin_lock_init(&filter->fo_r_discont_blocks.oh_lock);
+        spin_lock_init(&filter->fo_w_discont_blocks.oh_lock);
+        filter->fo_readcache_max_filesize = FILTER_MAX_CACHE_SIZE;
+
+        INIT_LIST_HEAD(&filter->fo_llog_list);
+        spin_lock_init(&filter->fo_llog_list_lock);
+
+        sprintf(ns_name, "filter-%s", obd->obd_uuid.uuid);
+        obd->obd_namespace = ldlm_namespace_new(ns_name, LDLM_NAMESPACE_SERVER);
+
+        if (obd->obd_namespace == NULL)
+                GOTO(err_post, rc = -ENOMEM);
+        obd->obd_namespace->ns_lvbp = obd;
+        obd->obd_namespace->ns_lvbo = &filter_lvbo;
+        ldlm_register_intent(obd->obd_namespace, filter_intent_policy);
+
+        ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
+                           "filter_ldlm_cb_client", &obd->obd_ldlm_client);
+
+        rc = obd_llog_cat_initialize(obd, &obd->obd_llogs, 1, CATLIST);
+        if (rc) {
                 CERROR("failed to setup llogging subsystems\n");
-#endif
+                GOTO(err_post, rc);
+        }
+        RETURN(0);
+
+err_post:
+        filter_post(obd);
+err_mntput:
+        unlock_kernel();
+        lvfs_umount_fs(filter->fo_lvfs_ctxt);
+        filter->fo_sb = 0;
+        lock_kernel();
+err_ops:
+        fsfilt_put_ops(obd->obd_fsops);
+        return rc;
+}
+
+static int filter_attach(struct obd_device *obd, obd_count len, void *data)
+{
+        struct lprocfs_static_vars lvars;
+        int rc;
+
+        lprocfs_init_vars(filter, &lvars);
+        rc = lprocfs_obd_attach(obd, lvars.obd_vars);
+        if (rc != 0)
+                return rc;
+
+        rc = lprocfs_alloc_obd_stats(obd, LPROC_FILTER_LAST);
+        if (rc != 0)
+                return rc;
+
+        /* Init obdfilter private stats here */
+        lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_READ_BYTES,
+                             LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
+        lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_WRITE_BYTES,
+                             LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
+
+        return lproc_filter_attach_seqstat(obd);
+}
+
+static int filter_detach(struct obd_device *dev)
+{
+        lprocfs_free_obd_stats(dev);
+        return lprocfs_obd_detach(dev);
+}
+
+static int filter_setup(struct obd_device *obd, obd_count len, void *buf)
+{
+        struct filter_obd *filter = &obd->u.filter;
+        struct lustre_cfg *lcfg = buf;
+        unsigned long page;
+        int rc;
+        ENTRY;
+
+        spin_lock_init(&filter->fo_denylist_lock);
+        INIT_LIST_HEAD(&filter->fo_denylist);
+
+        /* 2.6.9 selinux wants a full option page for do_kern_mount (bug6471) */
+        page = get_zeroed_page(GFP_KERNEL);
+        if (!page)
+                RETURN(-ENOMEM);
+
+        memcpy((void *)page, lustre_cfg_buf(lcfg, 4),
+               LUSTRE_CFG_BUFLEN(lcfg, 4));
+        
+        /* all mount options including errors=remount-ro and asyncdel are passed
+         * using 4th lcfg param. And it is good, finally we have got rid of
+         * hardcoded fs types in the code. */
+        rc = filter_common_setup(obd, len, buf, (void *)page);
+        free_page(page);
+        
+        if (rc)
+                RETURN(rc);
+        rc = filter_post_fs_setup(obd);
         RETURN(rc);
 }
 
 static int filter_cleanup(struct obd_device *obd, int flags)
 {
         struct filter_obd *filter = &obd->u.filter;
+        ll_sbdev_type save_dev;
         ENTRY;
 
         if (flags & OBD_OPT_FAILOVER)
@@ -1197,70 +1622,121 @@ static int filter_cleanup(struct obd_device *obd, int flags)
                 }
         }
 
+        target_cleanup_recovery(obd);
+        
         ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
 
         if (filter->fo_sb == NULL)
                 RETURN(0);
 
+        save_dev = ll_sbdev(filter->fo_sb);
+        filter_post_fs_cleanup(obd);
         filter_post(obd);
 
         shrink_dcache_parent(filter->fo_sb->s_root);
         filter->fo_sb = 0;
 
-        if (atomic_read(&filter->fo_vfsmnt->mnt_count) > 1)
-                CERROR("%s: mount point %p busy, mnt_count: %d\n",
-                       obd->obd_name, filter->fo_vfsmnt,
-                       atomic_read(&filter->fo_vfsmnt->mnt_count));
+        spin_lock(&filter->fo_denylist_lock);
+        while (!list_empty(&filter->fo_denylist)) {
+                deny_sec_t *p_deny_sec = list_entry(filter->fo_denylist.next,
+                                                    deny_sec_t, list);
+                list_del(&p_deny_sec->list);
+                OBD_FREE(p_deny_sec, sizeof(*p_deny_sec));
+        }
+        spin_unlock(&filter->fo_denylist_lock);
 
         unlock_kernel();
-        mntput(filter->fo_vfsmnt);
+        lvfs_umount_fs(filter->fo_lvfs_ctxt);
         //destroy_buffers(filter->fo_sb->s_dev);
         filter->fo_sb = NULL;
         fsfilt_put_ops(obd->obd_fsops);
         lock_kernel();
 
-        dev_clear_rdonly(2);
+        ll_clear_rdonly(save_dev);
 
         RETURN(0);
 }
 
-static int filter_attach(struct obd_device *obd, obd_count len, void *data)
+static int filter_process_config(struct obd_device *obd, obd_count len, void *buf)
 {
-        struct lprocfs_static_vars lvars;
-        int rc;
-
-        lprocfs_init_vars(filter, &lvars);
-        rc = lprocfs_obd_attach(obd, lvars.obd_vars);
-        if (rc != 0)
-                return rc;
+        struct lustre_cfg *lcfg = buf;
+        struct filter_obd *filter = &obd->u.filter;
+        int rc = 0;
+        ENTRY;
 
-        rc = lprocfs_alloc_obd_stats(obd, LPROC_FILTER_LAST);
-        if (rc != 0)
-                return rc;
+        switch(lcfg->lcfg_command) {
+        case LCFG_SET_SECURITY: {
+                if ((LUSTRE_CFG_BUFLEN(lcfg, 1) == 0) ||
+                    (LUSTRE_CFG_BUFLEN(lcfg, 2) == 0))
+                        GOTO(out, rc = -EINVAL);
+
+                if (!strcmp(lustre_cfg_string(lcfg, 1), "deny_sec")){
+                        spin_lock(&filter->fo_denylist_lock);
+                        rc = add_deny_security(lustre_cfg_string(lcfg, 2),
+                                               &filter->fo_denylist);
+                        spin_unlock(&filter->fo_denylist_lock);
+                }else {
+                        CERROR("Unrecognized key\n");
+                        rc = -EINVAL;
+                }
+                break;
+        }
+        default: {
+                CERROR("Unknown command: %d\n", lcfg->lcfg_command);
+                GOTO(out, rc = -EINVAL);
 
-        /* Init obdfilter private stats here */
-        lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_READ_BYTES,
-                             LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
-        lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_WRITE_BYTES,
-                             LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
-        return rc;
+        }
+        }
+out:
+        RETURN(rc);
 }
 
-static int filter_detach(struct obd_device *dev)
+static int filter_connect_post(struct obd_export *exp, unsigned initial,
+                               unsigned long connect_flags)
 {
-        lprocfs_free_obd_stats(dev);
-        return lprocfs_obd_detach(dev);
+        struct obd_device *obd = exp->exp_obd;
+        struct filter_export_data *fed;
+        char str[PTL_NALFMT_SIZE];
+        struct obd_llogs *llog;
+        struct llog_ctxt *ctxt;
+        int rc = 0;
+        ENTRY;
+
+        fed = &exp->exp_filter_data;
+        if (fed->fed_group < FILTER_MIN_GROUPS)
+                RETURN(0);
+
+        /* initialize llogs for connections from MDS */
+        llog = filter_grab_llog_for_group(obd, fed->fed_group, exp);
+        LASSERT(llog != NULL);
+
+        ctxt = llog_get_context(llog, LLOG_UNLINK_REPL_CTXT);
+        LASSERT(ctxt != NULL);
+
+        rc = llog_receptor_accept(ctxt, exp->exp_imp_reverse);
+        
+        portals_nid2str(exp->exp_connection->c_peer.peer_ni->pni_number,
+                        exp->exp_connection->c_peer.peer_id.nid, str);
+        
+        CDEBUG(D_OTHER, "%s: init llog ctxt for export "LPX64"/%s, group %d\n",
+               obd->obd_name, exp->exp_connection->c_peer.peer_id.nid,
+               str, fed->fed_group);
+
+        RETURN(rc);
 }
 
 /* nearly identical to mds_connect */
 static int filter_connect(struct lustre_handle *conn, struct obd_device *obd,
-                          struct obd_uuid *cluuid)
+                          struct obd_uuid *cluuid,
+                          struct obd_connect_data *data,
+                          unsigned long connect_flags)
 {
         struct obd_export *exp;
         struct filter_export_data *fed;
         struct filter_client_data *fcd = NULL;
         struct filter_obd *filter = &obd->u.filter;
-        int rc;
+        struct lvfs_run_ctxt saved;
+        int rc, group;
         ENTRY;
 
         if (conn == NULL || obd == NULL || cluuid == NULL)
@@ -1276,21 +1752,55 @@ static int filter_connect(struct lustre_handle *conn, struct obd_device *obd,
 
         spin_lock_init(&fed->fed_lock);
 
-        if (!obd->obd_replayable)
-                GOTO(cleanup, rc = 0);
+       /* connection from MDS */
+        group = connect_flags;
+        if (obd->obd_replayable) {
+                OBD_ALLOC(fcd, sizeof(*fcd));
+                if (!fcd) {
+                        CERROR("filter: out of memory for client data\n");
+                        GOTO(cleanup, rc = -ENOMEM);
+                }
 
-        OBD_ALLOC(fcd, sizeof(*fcd));
-        if (!fcd) {
-                CERROR("filter: out of memory for client data\n");
-                GOTO(cleanup, rc = -ENOMEM);
+                memcpy(fcd->fcd_uuid, cluuid, sizeof(fcd->fcd_uuid));
+                fed->fed_fcd = fcd;
+                fed->fed_fcd->fcd_group = group;
+                rc = filter_client_add(obd, filter, fed, -1);
+                if (rc)
+                        GOTO(cleanup, rc);
         }
-
-        memcpy(fcd->fcd_uuid, cluuid, sizeof(fcd->fcd_uuid));
-        fed->fed_fcd = fcd;
-        fcd->fcd_mount_count = cpu_to_le64(filter->fo_fsd->fsd_mount_count);
-
-        rc = filter_client_add(obd, filter, fed, -1);
-
+        CWARN("%s: Received MDS connection ("LPX64"); group %d\n",
+              obd->obd_name, exp->exp_handle.h_cookie, group);
+        if (group == 0)
+                GOTO(cleanup, rc);
+        
+        if (fed->fed_group != 0 && fed->fed_group != group) {
+                char str[PTL_NALFMT_SIZE];
+                portals_nid2str(exp->exp_connection->c_peer.peer_ni->pni_number,
+                                exp->exp_connection->c_peer.peer_id.nid, str);
+                CERROR("!!! This export (nid "LPX64"/%s) used object group %d "
+                       "earlier; now it's trying to use group %d!  This could "
+                       "be a bug in the MDS.  Tell CFS.\n",
+                       exp->exp_connection->c_peer.peer_id.nid, str,
+                       fed->fed_group, group);
+                GOTO(cleanup, rc = -EPROTO);
+        }
+        fed->fed_group = group;
+
+        push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
+        rc = filter_read_groups(obd, group, 1);
+        pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
+        if (rc != 0) {
+                CERROR("can't read group %u\n", group);
+                GOTO(cleanup, rc);
+        }
+#if 0
+        rc = filter_group_set_fs_flags(obd, group);
+        if (rc != 0) {
+                CERROR("can't set kml flags %u\n", group);
+                GOTO(cleanup, rc);
+        }
+#endif
 cleanup:
         if (rc) {
                 if (fcd)
@@ -1304,52 +1814,244 @@ cleanup:
 
 static int filter_precleanup(struct obd_device *obd, int flags)
 {
+        struct filter_group_llog *log;
+        struct filter_obd *filter;
         int rc = 0;
         ENTRY;
 
-#ifdef ENABLE_ORPHANS
-        rc = obd_llog_finish(obd, 0);
+        filter = &obd->u.filter;
+
+        spin_lock(&filter->fo_llog_list_lock);
+        while (!list_empty(&filter->fo_llog_list)) {
+                log = list_entry(filter->fo_llog_list.next,
+                                 struct filter_group_llog, list);
+                list_del(&log->list);
+                spin_unlock(&filter->fo_llog_list_lock);
+
+                rc = obd_llog_finish(obd, log->llogs, 0);
+                if (rc)
+                        CERROR("failed to cleanup llogging subsystem for %u\n",
+                                log->group);
+                OBD_FREE(log->llogs, sizeof(*(log->llogs)));
+                OBD_FREE(log, sizeof(*log));
+                spin_lock(&filter->fo_llog_list_lock);
+        }
+        spin_unlock(&filter->fo_llog_list_lock);
+
+        rc = obd_llog_finish(obd, &obd->obd_llogs, 0);
         if (rc)
                 CERROR("failed to cleanup llogging subsystem\n");
-#endif
 
         RETURN(rc);
 }
 
+/* Do extra sanity checks for grant accounting.  We do this at connect,
+ * disconnect, and statfs RPC time, so it shouldn't be too bad.  We can
+ * always get rid of it or turn it off when we know accounting is good. */
+static void filter_grant_sanity_check(struct obd_device *obd, const char *func)
+{
+        struct filter_export_data *fed;
+        struct obd_export *exp;
+        obd_size maxsize = obd->obd_osfs.os_blocks * obd->obd_osfs.os_bsize;
+        obd_size tot_dirty = 0, tot_pending = 0, tot_granted = 0;
+        obd_size fo_tot_dirty, fo_tot_pending, fo_tot_granted;
+        int level = D_CACHE;
+
+        if (list_empty(&obd->obd_exports))
+                return;
+
+        spin_lock(&obd->obd_osfs_lock);
+        spin_lock(&obd->obd_dev_lock);
+        list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
+                fed = &exp->exp_filter_data;
+                if (fed->fed_grant < 0 || fed->fed_pending < 0 ||
+                    fed->fed_dirty < 0)
+                        level = D_ERROR;
+                if (maxsize > 0) { /* we may not have done a statfs yet */
+                        LASSERTF(fed->fed_grant + fed->fed_pending <= maxsize,
+                                 "cli %s/%p %ld+%ld > "LPU64"\n",
+                                 exp->exp_client_uuid.uuid, exp,
+                                 fed->fed_grant, fed->fed_pending, maxsize);
+                        LASSERTF(fed->fed_dirty <= maxsize,
+                                 "cli %s/%p %ld > "LPU64"\n",
+                                 exp->exp_client_uuid.uuid, exp,
+                                 fed->fed_dirty, maxsize);
+                }
+                CDEBUG(level, "%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
+                       obd->obd_name, exp->exp_client_uuid.uuid, exp,
+                       fed->fed_dirty, fed->fed_pending, fed->fed_grant);
+                tot_granted += fed->fed_grant + fed->fed_pending;
+                tot_pending += fed->fed_pending;
+                tot_dirty += fed->fed_dirty;
+        }
+        fo_tot_granted = obd->u.filter.fo_tot_granted;
+        fo_tot_pending = obd->u.filter.fo_tot_pending;
+        fo_tot_dirty = obd->u.filter.fo_tot_dirty;
+        spin_unlock(&obd->obd_dev_lock);
+        spin_unlock(&obd->obd_osfs_lock);
+
+        /* Do these assertions outside the spinlocks so we don't kill system */
+        if (tot_granted != fo_tot_granted)
+                CERROR("%s: tot_granted "LPU64" != fo_tot_granted "LPU64"\n",
+                       func, tot_granted, fo_tot_granted);
+        if (tot_pending != fo_tot_pending)
+                CERROR("%s: tot_pending "LPU64" != fo_tot_pending "LPU64"\n",
+                       func, tot_pending, fo_tot_pending);
+        if (tot_dirty != fo_tot_dirty)
+                CERROR("%s: tot_dirty "LPU64" != fo_tot_dirty "LPU64"\n",
+                       func, tot_dirty, fo_tot_dirty);
+        if (tot_pending > tot_granted)
+                CERROR("%s: tot_pending "LPU64" > tot_granted "LPU64"\n",
+                       func, tot_pending, tot_granted);
+        if (tot_granted > maxsize)
+                CERROR("%s: tot_granted "LPU64" > maxsize "LPU64"\n",
+                       func, tot_granted, maxsize);
+        if (tot_dirty > maxsize)
+                CERROR("%s: tot_dirty "LPU64" > maxsize "LPU64"\n",
+                       func, tot_dirty, maxsize);
+}
+
+/* Remove this client from the grant accounting totals.  We also remove
+ * the export from the obd device under the osfs and dev locks to ensure
+ * that the filter_grant_sanity_check() calculations are always valid.
+ * The client should do something similar when it invalidates its import. */
+static void filter_grant_discard(struct obd_export *exp)
+{
+        struct obd_device *obd = exp->exp_obd;
+        struct filter_obd *filter = &obd->u.filter;
+        struct filter_export_data *fed = &exp->exp_filter_data;
+        int level = D_CACHE;
+
+        spin_lock(&obd->obd_osfs_lock);
+        spin_lock(&exp->exp_obd->obd_dev_lock);
+        list_del_init(&exp->exp_obd_chain);
+        spin_unlock(&exp->exp_obd->obd_dev_lock);
+
+        if (fed->fed_dirty < 0 || fed->fed_grant < 0 || fed->fed_pending < 0)
+                level = D_ERROR;
+        CDEBUG(level, "%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
+               obd->obd_name, exp->exp_client_uuid.uuid, exp,
+               fed->fed_dirty, fed->fed_pending, fed->fed_grant);
+
+        LASSERTF(filter->fo_tot_granted >= fed->fed_grant,
+                 "%s: tot_granted "LPU64" cli %s/%p fed_grant %ld\n",
+                 obd->obd_name, filter->fo_tot_granted,
+                 exp->exp_client_uuid.uuid, exp, fed->fed_grant);
+        filter->fo_tot_granted -= fed->fed_grant;
+        LASSERTF(filter->fo_tot_pending >= fed->fed_pending,
+                 "%s: tot_pending "LPU64" cli %s/%p fed_pending %ld\n",
+                 obd->obd_name, filter->fo_tot_pending,
+                 exp->exp_client_uuid.uuid, exp, fed->fed_pending);
+        LASSERTF(filter->fo_tot_dirty >= fed->fed_dirty,
+                 "%s: tot_dirty "LPU64" cli %s/%p fed_dirty %ld\n",
+                 obd->obd_name, filter->fo_tot_dirty,
+                 exp->exp_client_uuid.uuid, exp, fed->fed_dirty);
+        filter->fo_tot_dirty -= fed->fed_dirty;
+        fed->fed_dirty = 0;
+        fed->fed_grant = 0;
+
+        spin_unlock(&obd->obd_osfs_lock);
+}
+
 static int filter_destroy_export(struct obd_export *exp)
 {
         ENTRY;
 
+        if (exp->exp_filter_data.fed_pending)
+                CERROR("%s: cli %s/%p has %lu pending on destroyed export\n",
+                       exp->exp_obd->obd_name, exp->exp_client_uuid.uuid,
+                       exp, exp->exp_filter_data.fed_pending);
+
         target_destroy_export(exp);
 
         if (exp->exp_obd->obd_replayable)
                 filter_client_free(exp, exp->exp_flags);
+
+        filter_grant_discard(exp);
+        if (!(exp->exp_flags & OBD_OPT_FORCE))
+                filter_grant_sanity_check(exp->exp_obd, __FUNCTION__);
+
         RETURN(0);
 }
 
+static void filter_sync_llogs(struct obd_device *obd, struct obd_export *dexp)
+{
+        struct filter_group_llog *fglog, *nlog;
+        struct filter_obd *filter;
+        int worked = 0, group;
+        struct llog_ctxt *ctxt;
+        ENTRY;
+
+        filter = &obd->u.filter;
+
+        /* we can't sync log holding spinlock. also, we do not want to get
+         * into livelock. so we do following: loop over MDS's exports in
+         * group order and skip already synced llogs -bzzz */
+        do {
+                /* look for group with min. number, but > worked */
+                fglog = NULL;
+                group = 1 << 30;
+                spin_lock(&filter->fo_llog_list_lock);
+                list_for_each_entry(nlog, &filter->fo_llog_list, list) {
+                       
+                        if (nlog->group <= worked) {
+                                /* this group is already synced */
+                                continue;
+                        }
+        
+                        if (group < nlog->group) {
+                                /* we have group with smaller number to sync */
+                                continue;
+                        }
+
+                        /* store current minimal group */
+                        fglog = nlog;
+                        group = nlog->group;
+                }
+                spin_unlock(&filter->fo_llog_list_lock);
+
+                if (fglog == NULL)
+                        break;
+
+                worked = fglog->group;
+                if (fglog->exp && (dexp == fglog->exp || dexp == NULL)) {
+                        ctxt = llog_get_context(fglog->llogs,
+                                        LLOG_UNLINK_REPL_CTXT);
+                        LASSERT(ctxt != NULL);
+                        llog_sync(ctxt, fglog->exp);
+                }
+        } while (fglog != NULL);
+}
+
 /* also incredibly similar to mds_disconnect */
-static int filter_disconnect(struct obd_export *exp, int flags)
+static int filter_disconnect(struct obd_export *exp, unsigned long flags)
 {
+        struct obd_device *obd = exp->exp_obd;
         unsigned long irqflags;
-        struct llog_ctxt *ctxt;
         int rc;
         ENTRY;
 
         LASSERT(exp);
-        ldlm_cancel_locks_for_export(exp);
+        class_export_get(exp);
 
         spin_lock_irqsave(&exp->exp_lock, irqflags);
         exp->exp_flags = flags;
         spin_unlock_irqrestore(&exp->exp_lock, irqflags);
 
-        fsfilt_sync(exp->exp_obd, exp->exp_obd->u.filter.fo_sb);
-        /* XXX cleanup preallocated inodes */
-
-        /* flush any remaining cancel messages out to the target */
-        ctxt = llog_get_context(exp->exp_obd, LLOG_UNLINK_REPL_CTXT);
-        llog_sync(ctxt, exp);
+        if (!(flags & OBD_OPT_FORCE))
+                filter_grant_sanity_check(obd, __FUNCTION__);
+        filter_grant_discard(exp);
 
+        /* Disconnect early so that clients can't keep using export */
         rc = class_disconnect(exp, flags);
+
+        ldlm_cancel_locks_for_export(exp);
+
+        fsfilt_sync(obd, obd->u.filter.fo_sb);
+
+        /* flush any remaining cancel messages out to the target */
+        filter_sync_llogs(obd, exp);
+        class_export_put(exp);
         RETURN(rc);
 }
 
@@ -1358,24 +2060,27 @@ struct dentry *__filter_oa2dentry(struct obd_device *obd,
 {
         struct dentry *dchild = NULL;
         obd_gr group = 0;
+        ENTRY;
 
         if (oa->o_valid & OBD_MD_FLGROUP)
                 group = oa->o_gr;
 
-        dchild = filter_fid2dentry(obd, NULL, group, oa->o_id);
+        dchild = filter_id2dentry(obd, NULL, group, oa->o_id);
 
         if (IS_ERR(dchild)) {
-                CERROR("%s error looking up object: "LPU64"\n", what, oa->o_id);
+                CERROR("%s error looking up object: "LPU64"\n",
+                       what, oa->o_id);
                 RETURN(dchild);
         }
 
         if (dchild->d_inode == NULL) {
-                CERROR("%s on non-existent object: "LPU64"\n", what, oa->o_id);
+                CDEBUG(D_INFO, "%s: %s on non-existent object: "
+                       LPU64"\n", obd->obd_name, what, oa->o_id);
                 f_dput(dchild);
                 RETURN(ERR_PTR(-ENOENT));
         }
 
-        return dchild;
+        RETURN(dchild);
 }
 
 static int filter_getattr(struct obd_export *exp, struct obdo *oa,
@@ -1405,64 +2110,106 @@ static int filter_getattr(struct obd_export *exp, struct obdo *oa,
         RETURN(rc);
 }
 
-/* this is called from filter_truncate() until we have filter_punch() */
-static int filter_setattr(struct obd_export *exp, struct obdo *oa,
-                          struct lov_stripe_md *md, struct obd_trans_info *oti)
+int filter_setattr_internal(struct obd_export *exp, struct dentry *dentry,
+                            struct obdo *oa, struct obd_trans_info *oti)
 {
-        struct obd_run_ctxt saved;
         struct filter_obd *filter;
-        struct dentry *dentry;
         struct iattr iattr;
         void *handle;
-        int rc, rc2;
+        int rc, err;
         ENTRY;
 
-        LASSERT(oti != NULL);
-
-        dentry = filter_oa2dentry(exp->exp_obd, oa);
-        if (IS_ERR(dentry))
-                RETURN(PTR_ERR(dentry));
-
+        LASSERT(dentry != NULL);
+        LASSERT(!IS_ERR(dentry));
+        LASSERT(dentry->d_inode != NULL);
+        
         filter = &exp->exp_obd->u.filter;
-
         iattr_from_obdo(&iattr, oa, oa->o_valid);
 
-        push_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
-        lock_kernel();
-
         if (iattr.ia_valid & ATTR_SIZE)
                 down(&dentry->d_inode->i_sem);
-        handle = fsfilt_start(exp->exp_obd, dentry->d_inode, FSFILT_OP_SETATTR,
-                              oti);
+        handle = fsfilt_start(exp->exp_obd, dentry->d_inode,
+                              FSFILT_OP_SETATTR, oti);
         if (IS_ERR(handle))
                 GOTO(out_unlock, rc = PTR_ERR(handle));
 
         /* XXX this could be a rwsem instead, if filter_preprw played along */
         if (iattr.ia_valid & ATTR_ATTR_FLAG)
-                rc = fsfilt_iocontrol(exp->exp_obd, dentry->d_inode, NULL,
-                                      EXT3_IOC_SETFLAGS,
+                rc = fsfilt_iocontrol(exp->exp_obd, dentry->d_inode,
+                                      NULL, EXT3_IOC_SETFLAGS,
                                       (long)&iattr.ia_attr_flags);
         else
-                rc = fsfilt_setattr(exp->exp_obd, dentry, handle, &iattr, 1);
+                rc = fsfilt_setattr(exp->exp_obd, dentry, handle,
+                                    &iattr, 1);
+        
         rc = filter_finish_transno(exp, oti, rc);
-        rc2 = fsfilt_commit(exp->exp_obd, dentry->d_inode, handle, 0);
-        if (rc2) {
-                CERROR("error on commit, err = %d\n", rc2);
+        
+        err = fsfilt_commit(exp->exp_obd, filter->fo_sb,
+                            dentry->d_inode, handle,
+                            exp->exp_sync);
+        if (err) {
+                CERROR("error on commit, err = %d\n", err);
                 if (!rc)
-                        rc = rc2;
+                        rc = err;
         }
+        EXIT;
+out_unlock:
+        if (iattr.ia_valid & ATTR_SIZE)
+                up(&dentry->d_inode->i_sem);
+        return rc;
+}
+
+/* this is called from filter_truncate() until we have filter_punch() */
+int filter_setattr(struct obd_export *exp, struct obdo *oa,
+                   struct lov_stripe_md *md, struct obd_trans_info *oti)
+{
+        struct ldlm_res_id res_id = { .name = { oa->o_id, 0, oa->o_gr, 0 } };
+        struct ldlm_valblock_ops *ns_lvbo;
+        struct lvfs_run_ctxt saved;
+        struct filter_obd *filter;
+        struct ldlm_resource *res;
+        struct dentry *dentry;
+        int rc;
+        ENTRY;
 
+        LASSERT(oti != NULL);
+
+        filter = &exp->exp_obd->u.filter;
+        push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
+
+        /* make sure that object is allocated. */
+        dentry = filter_crow_object(exp->exp_obd,
+                                    oa->o_gr, oa->o_id);
+        if (IS_ERR(dentry))
+                GOTO(out_pop, rc = PTR_ERR(dentry));
+
+        lock_kernel();
+
+        /* setting objects attributes (including owner/group) */
+        rc = filter_setattr_internal(exp, dentry, oa, oti);
+        if (rc)
+                GOTO(out_unlock, rc);
+
+        res = ldlm_resource_get(exp->exp_obd->obd_namespace, NULL,
+                                res_id, LDLM_EXTENT, 0);
+        
+        if (res != NULL) {
+                ns_lvbo = res->lr_namespace->ns_lvbo;
+                if (ns_lvbo && ns_lvbo->lvbo_update)
+                        rc = ns_lvbo->lvbo_update(res, NULL, 0, 0);
+                ldlm_resource_putref(res);
+        }
+        
         oa->o_valid = OBD_MD_FLID;
         obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
 
+        EXIT;
 out_unlock:
-        if (iattr.ia_valid & ATTR_SIZE)
-                up(&dentry->d_inode->i_sem);
         unlock_kernel();
-        pop_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
-
         f_dput(dentry);
-        RETURN(rc);
+out_pop:
+        pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
+        return rc;
 }
 
 /* XXX identical to osc_unpackmd */
@@ -1515,245 +2262,369 @@ static int filter_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
         RETURN(lsm_size);
 }
 
-static void filter_destroy_precreated(struct obd_export *exp, struct obdo *oa,
-                                      struct filter_obd *filter)
-{
-        struct obdo doa; /* XXX obdo on stack */
-        __u64 last, id;
-        ENTRY;
-        LASSERT(oa);
-
-        memset(&doa, 0, sizeof(doa));
-        if (oa->o_valid & OBD_MD_FLGROUP)
-                doa.o_gr = oa->o_gr;
-        else
-                doa.o_gr = 0;
-        doa.o_mode = S_IFREG;
-        last = filter_last_id(filter, &doa); /* FIXME: object groups */
-        CWARN("deleting orphan objects from "LPU64" to "LPU64"\n",
-               oa->o_id + 1, last);
-        for (id = oa->o_id + 1; id <= last; id++) {
-                doa.o_id = id;
-                filter_destroy(exp, &doa, NULL, NULL);
-        }
-        spin_lock(&filter->fo_objidlock);
-        filter->fo_last_objids[0] = oa->o_id; /* FIXME: object groups */
-        spin_unlock(&filter->fo_objidlock);
-        EXIT;
-}
-
-/* returns a negative error or a nonnegative number of files to create */
-static int filter_should_precreate(struct obd_export *exp, struct obdo *oa,
-                                   int group)
+static int filter_statfs(struct obd_device *obd, struct obd_statfs *osfs,
+                         unsigned long max_age)
 {
-        struct obd_device *obd = exp->exp_obd;
         struct filter_obd *filter = &obd->u.filter;
-        int diff, rc;
+        int blockbits = filter->fo_sb->s_blocksize_bits;
+        int rc;
         ENTRY;
 
-        /* only precreate if group == 0 and o_id is specfied */
-        if (group != 0 || oa->o_id == 0)
-                RETURN(1);
+        /* at least try to account for cached pages.  its still racey and
+         * might be under-reporting if clients haven't announced their
+         * caches with brw recently */
+        spin_lock(&obd->obd_osfs_lock);
+        rc = fsfilt_statfs(obd, filter->fo_sb, max_age);
+        memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
+        spin_unlock(&obd->obd_osfs_lock);
 
-        diff = oa->o_id - filter_last_id(filter, oa);
-        CDEBUG(D_INFO, "filter_last_id() = "LPU64" -> diff = %d\n",
-               filter_last_id(filter, oa), diff);
-        if (diff >= 0)
-                RETURN(diff);
+        CDEBUG(D_SUPER | D_CACHE, "blocks cached "LPU64" granted "LPU64
+               " pending "LPU64" free "LPU64" avail "LPU64"\n",
+               filter->fo_tot_dirty, filter->fo_tot_granted,
+               filter->fo_tot_pending,
+               osfs->os_bfree << blockbits, osfs->os_bavail << blockbits);
 
-        if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
-            !(oa->o_flags & OBD_FL_DELORPHAN)) {
-                CERROR("filter asked to delete %d objects, but DELORPHAN flag "
-                       "isn't set!\n", -diff);
-                RETURN(0);
-        }
+        filter_grant_sanity_check(obd, __FUNCTION__);
+
+        osfs->os_bavail -= min(osfs->os_bavail,
+                               (filter->fo_tot_dirty + filter->fo_tot_pending +
+                                osfs->os_bsize -1) >> blockbits);
 
-        /* delete orphan request */
-        filter_destroy_precreated(exp, oa, filter);
-        rc = filter_update_last_objid(obd, group, 0);
-        if (rc)
-                CERROR("unable to write lastobjid, but orphans were deleted\n");
         RETURN(rc);
 }
 
-/* We rely on the fact that only one thread will be creating files in a given
- * group at a time, which is why we don't need an atomic filter_get_new_id.
- * Even if we had that atomic function, the following race would exist:
- *
- * thread 1: gets id x from filter_next_id
- * thread 2: gets id (x + 1) from filter_next_id
- * thread 2: creates object (x + 1)
- * thread 1: tries to create object x, gets -ENOSPC
- */
-static int filter_precreate(struct obd_device *obd, struct obdo *oa,
-                            obd_gr group, int *num)
+int filter_create_object(struct obd_device *obd, struct obdo *oa,
+                         obd_gr group)
 {
-        struct lustre_handle parent_lockh;
+        struct dentry *dparent = NULL;
         struct dentry *dchild = NULL;
         struct filter_obd *filter;
-        struct dentry *dparent;
-        struct iattr attr;
-        int err = 0, rc = 0, i;
-        __u64 next_id;
-        void *handle;
+        struct obd_statfs *osfs;
+        int cleanup_phase = 0;
+        int err = 0, rc = 0;
+        void *handle = NULL;
+        void *lock = NULL;
         ENTRY;
 
         filter = &obd->u.filter;
 
-        for (i = 0; i < *num && err == 0; i++) {
-                next_id = filter_last_id(filter, NULL) + 1;
-                CDEBUG(D_INFO, "precreate objid "LPU64"\n", next_id);
+        OBD_ALLOC(osfs, sizeof(*osfs));
+        if (osfs == NULL)
+                RETURN(-ENOMEM);
+        rc = filter_statfs(obd, osfs, jiffies - HZ);
+        if (rc == 0 && osfs->os_bavail < (osfs->os_blocks >> 10)) {
+                CDEBUG(D_HA, "OST out of space! avail "LPU64"\n",
+                       osfs->os_bavail << filter->fo_sb->s_blocksize_bits);
+                rc = -ENOSPC;
+        }
+        OBD_FREE(osfs, sizeof(*osfs));
+        if (rc)
+                RETURN(rc);
 
-                dparent = filter_parent_lock(obd, group, next_id, LCK_PW,
-                                             &parent_lockh);
-                if (IS_ERR(dparent)) {
-                        rc = PTR_ERR(dparent);
-                        break;
-                }
+        down(&filter->fo_create_locks[group]);
+
+        if (test_bit(group, &filter->fo_destroys_in_progress)) {
+                CWARN("%s: precreate aborted by destroy\n",
+                      obd->obd_name);
+                GOTO(out, rc = -EALREADY);
+        }
 
-                dchild = filter_fid2dentry(obd, dparent, group, next_id);
-                if (IS_ERR(dchild))
-                        GOTO(cleanup_lock, rc = PTR_ERR(dchild));
+        CDEBUG(D_INFO, "precreate objid "LPU64"\n", oa->o_id);
 
-                if (dchild->d_inode != NULL) {
-                        /* This would only happen if lastobjid was bad on disk*/
-                        CERROR("Serious error: objid %*s already exists; is "
-                               "this filesystem corrupt?\n",
-                               dchild->d_name.len, dchild->d_name.name);
-                        GOTO(cleanup_dchild, rc = -EEXIST);
-                }
+        dparent = filter_parent_lock(obd, group, oa->o_id, &lock);
+        if (IS_ERR(dparent))
+                GOTO(cleanup, rc = PTR_ERR(dparent));
+        cleanup_phase = 1;
+
+        dchild = filter_id2dentry(obd, dparent, group, oa->o_id);
+        if (IS_ERR(dchild))
+                GOTO(cleanup, rc = PTR_ERR(dchild));
+        cleanup_phase = 2;
+
+        if (dchild->d_inode != NULL)
+                GOTO(cleanup, rc = 0);
+
+        handle = fsfilt_start_log(obd, dparent->d_inode,
+                                  FSFILT_OP_CREATE, NULL, 1);
+        if (IS_ERR(handle))
+                GOTO(cleanup, rc = PTR_ERR(handle));
+        cleanup_phase = 3;
 
-                handle = fsfilt_start(obd, dparent->d_inode,
-                                      FSFILT_OP_CREATE_LOG, NULL);
-                if (IS_ERR(handle))
-                        GOTO(cleanup_dchild, rc = PTR_ERR(handle));
+        rc = ll_vfs_create(dparent->d_inode, dchild, S_IFREG, NULL);
+        if (rc) {
+                CERROR("create failed rc = %d\n", rc);
+                GOTO(cleanup, rc);
+        }
+
+        fsfilt_set_fs_flags(obd, dparent->d_inode, SM_DO_REC);
 
-                rc = ll_vfs_create(dparent->d_inode, dchild, S_IFREG, NULL);
+        if (oa->o_id > filter_last_id(filter, group)) {
+                /*
+                 * saving last created object id, it will be needed in recovery
+                 * for deleting orphanes.
+                 */
+                filter_set_last_id(filter, group, oa->o_id);
+
+                rc = filter_update_last_objid(obd, group, 0);
                 if (rc) {
-                        CERROR("create failed rc = %d\n", rc);
-                        GOTO(cleanup_commit, rc);
-                } else if (oa != NULL &&
-                           (oa->o_valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME |
-                                           OBD_MD_FLSIZE))) {
-                        iattr_from_obdo(&attr, oa, oa->o_valid);
-                        rc = fsfilt_setattr(obd, dchild, handle, &attr, 1);
-                        if (rc)
-                                CERROR("create setattr failed rc = %d\n", rc);
+                        CERROR("unable to write lastobjid, but "
+                               "orphans were deleted, err = %d\n",
+                               rc);
+                        rc = 0;
                 }
-                filter_set_last_id(filter, NULL, next_id);
-                err = filter_update_last_objid(obd, group, 0);
-                if (err)
-                        CERROR("unable to write lastobjid but file created\n");
-        cleanup_commit:
-                err = fsfilt_commit(obd, dparent->d_inode, handle, 0);
+        }
+cleanup:
+        switch(cleanup_phase) {
+        case 3:
+                err = fsfilt_commit(obd, filter->fo_sb,
+                                    dparent->d_inode, handle, 0);
                 if (err) {
                         CERROR("error on commit, err = %d\n", err);
                         if (!rc)
                                 rc = err;
                 }
-                if (dchild->d_inode != NULL && oa != NULL)
-                        obdo_from_inode(oa, dchild->d_inode,
-                                        FILTER_VALID_FLAGS);
-        cleanup_dchild:
+        case 2:
                 f_dput(dchild);
-        cleanup_lock:
-                filter_parent_unlock(dparent, &parent_lockh, LCK_PW);
-                if (rc)
-                        break;
-                oa = NULL; /* oa applies for first iteration only */
+        case 1:
+                filter_parent_unlock(dparent, lock);
+        case 0:
+                break;
         }
-        *num = i;
 
-        CDEBUG(D_INFO, "filter_precreate() created %d objects\n", i);
+        if (rc)
+                GOTO(out, rc);
+
+out:
+        up(&filter->fo_create_locks[group]);
         RETURN(rc);
 }
 
-static int filter_create(struct obd_export *exp, struct obdo *oa,
-                         struct lov_stripe_md **ea, struct obd_trans_info *oti)
+struct dentry *filter_crow_object(struct obd_device *obd,
+                                  __u64 ogr, __u64 oid)
 {
-        struct obd_device *obd = NULL;
-        struct obd_run_ctxt saved;
-        struct lov_stripe_md *lsm = NULL;
-        obd_gr group = 0;
-        int rc = 0, diff;
+        struct dentry *dentry;
+        struct obdo *oa;
+        int rc = 0;
         ENTRY;
 
-        if (oa->o_valid & OBD_MD_FLGROUP)
-                group = oa->o_gr;
+        /* check if object is already allocated */
+        dentry = filter_id2dentry(obd, NULL, ogr, oid);
+        if (IS_ERR(dentry))
+                RETURN(dentry);
 
-        CDEBUG(D_INFO, "filter_create(od->o_gr="LPU64",od->o_id="LPU64")\n",
-               group, oa->o_id);
-        if (ea != NULL) {
-                lsm = *ea;
-                if (lsm == NULL) {
-                        rc = obd_alloc_memmd(exp, &lsm);
-                        if (rc < 0)
-                                RETURN(rc);
-                }
+        if (dentry->d_inode)
+                RETURN(dentry);
+
+        f_dput(dentry);
+        
+        /* allocate object as it does not exist */
+        oa = obdo_alloc();
+        if (oa == NULL)
+                RETURN(ERR_PTR(-ENOMEM));
+
+        oa->o_id = oid;
+        oa->o_gr = ogr;
+        oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
+
+        CDEBUG(D_INODE, "OSS object "LPU64"/"LPU64
+               " does not exists - allocate now\n",
+               oid, ogr);
+
+        rc = filter_create_object(obd, oa, oa->o_gr);
+        if (rc) {
+                CERROR("cannot create OSS object "LPU64"/"LPU64
+                       ", err = %d\n", oa->o_id, oa->o_gr, rc);
+                GOTO(out_free_oa, dentry = ERR_PTR(rc));
+        }
+
+        /* lookup for just created object and return it to caller */
+        dentry = filter_id2dentry(obd, NULL, ogr, oid);
+        if (IS_ERR(dentry))
+                GOTO(out_free_oa, dentry);
+                
+        if (dentry->d_inode == NULL) {
+                f_dput(dentry);
+                dentry = ERR_PTR(-ENOENT);
+                CERROR("cannot find just created OSS object "
+                       LPU64"/"LPU64" err = %d\n", oid,
+                       ogr, (int)PTR_ERR(dentry));
+                GOTO(out_free_oa, dentry);
         }
 
+        EXIT;
+out_free_oa:
+        obdo_free(oa);
+        return dentry;
+}
+
+static int
+filter_clear_orphans(struct obd_export *exp, struct obdo *oa)
+{
+        struct obd_device *obd = NULL;
+        struct filter_obd *filter;
+        struct obdo *doa = NULL;
+        int rc = 0, orphans;
+        __u64 last, id;
+        ENTRY;
+        
+        LASSERT(oa);
+        LASSERT(oa->o_gr != 0);
+        LASSERT(oa->o_valid & OBD_MD_FLGROUP);
+
         obd = exp->exp_obd;
-        push_ctxt(&saved, &obd->obd_ctxt, NULL);
+        filter = &obd->u.filter;
+
+        last = filter_last_id(filter, oa->o_gr);
+        orphans = last - oa->o_id;
+        
+        if (orphans <= 0)
+                RETURN(0);
+                
+       doa = obdo_alloc();
+        if (doa == NULL)
+                RETURN(-ENOMEM);
+
+        doa->o_gr = oa->o_gr;
+        doa->o_mode = S_IFREG;
+        doa->o_valid = oa->o_valid & (OBD_MD_FLGROUP | OBD_MD_FLID);
 
-        diff = filter_should_precreate(exp, oa, group);
-        if (diff > 0) {
-                oa->o_id = filter_last_id(&obd->u.filter, oa);
-                rc = filter_precreate(obd, oa, group, &diff);
-                oa->o_id += diff;
-                oa->o_valid = OBD_MD_FLID;
+        set_bit(doa->o_gr, &filter->fo_destroys_in_progress);
+        down(&filter->fo_create_locks[doa->o_gr]);
+        if (!test_bit(doa->o_gr, &filter->fo_destroys_in_progress)) {
+                CERROR("%s:["LPU64"] destroy_in_progress already cleared\n",
+                       exp->exp_obd->obd_name, doa->o_gr);
+                up(&filter->fo_create_locks[doa->o_gr]);
+                GOTO(out_free_doa, 0);
         }
 
-        pop_ctxt(&saved, &obd->obd_ctxt, NULL);
-        if (rc && ea != NULL && *ea != lsm) {
-                obd_free_memmd(exp, &lsm);
-        } else if (rc == 0 && ea != NULL) {
-                /* XXX LOV STACKING: the lsm that is passed to us from
-                 * LOV does not have valid lsm_oinfo data structs, so
-                 * don't go touching that.  This needs to be fixed in a
-                 * big way. */
-                lsm->lsm_object_id = oa->o_id;
-                *ea = lsm;
+        CWARN("%s:["LPU64"] deleting orphan objects from "LPU64" to "
+              LPU64"\n", exp->exp_obd->obd_name, doa->o_gr,
+              oa->o_id + 1, last);
+        
+        for (id = oa->o_id + 1; id <= last; id++) {
+                doa->o_id = id;
+                filter_destroy(exp, doa, NULL, NULL);
         }
 
-        RETURN(rc);
+        CDEBUG(D_HA, "%s:["LPU64"] after destroy: set last_objids = "
+               LPU64"\n", exp->exp_obd->obd_name, doa->o_gr, oa->o_id);
+
+        /* return next free id to be used as a new start of sequence -bzzz */
+        oa->o_id = last + 1;
+
+        filter_set_last_id(filter, oa->o_gr, oa->o_id);
+        clear_bit(doa->o_gr, &filter->fo_destroys_in_progress);
+        up(&filter->fo_create_locks[oa->o_gr]);
+
+        EXIT;
+out_free_doa:
+        obdo_free(doa);
+        return rc;
+}
+
+/*
+ * by now this function is only needed as entry point for deleting orphanes on
+ * OSS as objects are created on first write attempt. --umka
+ */
+static int
+filter_create(struct obd_export *exp, struct obdo *oa, void *acl,
+              int acl_size, struct lov_stripe_md **ea,
+              struct obd_trans_info *oti)
+{
+        struct filter_export_data *fed;
+        struct obd_device *obd = NULL;
+        int group = oa->o_gr, rc = 0;
+        struct lvfs_run_ctxt saved;
+        struct filter_obd *filter;
+        char str[PTL_NALFMT_SIZE];
+        ENTRY;
+
+        LASSERT(acl == NULL && acl_size == 0);
+
+        if (!(oa->o_valid & OBD_MD_FLGROUP) || group == 0) {
+                portals_nid2str(exp->exp_connection->c_peer.peer_ni->pni_number,
+                                exp->exp_connection->c_peer.peer_id.nid, str);
+                CERROR("!!! nid "LPX64"/%s sent invalid object group %d\n",
+                       exp->exp_connection->c_peer.peer_id.nid, str, group);
+                RETURN(-EINVAL);
+        }
+
+        obd = exp->exp_obd;
+        fed = &exp->exp_filter_data;
+        filter = &obd->u.filter;
+
+        if (fed->fed_group != group) {
+                portals_nid2str(exp->exp_connection->c_peer.peer_ni->pni_number,
+                                exp->exp_connection->c_peer.peer_id.nid, str);
+                CERROR("!!! this export (nid "LPX64"/%s) used object group %d "
+                       "earlier; now it's trying to use group %d!  This could "
+                       "be a bug in the MDS.  Tell CFS.\n",
+                       exp->exp_connection->c_peer.peer_id.nid, str,
+                       fed->fed_group, group);
+                RETURN(-ENOTUNIQ);
+        }
+
+        CDEBUG(D_INFO, "filter_create(od->o_gr=%d,od->o_id="LPU64")\n",
+               group, oa->o_id);
+
+        obd = exp->exp_obd;
+        push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
+
+        LASSERT((oa->o_valid & OBD_MD_FLFLAGS) &&
+                (oa->o_flags == OBD_FL_DELORPHAN));
+                
+        rc = filter_clear_orphans(exp, oa);
+        if (rc) {
+                CERROR("cannot clear orphanes starting from "
+                       LPU64", err = %d\n", oa->o_id, rc);
+        } else {
+                rc = filter_update_last_objid(obd, group, 0);
+                if (rc) {
+                        CERROR("unable to write lastobjid, but "
+                               "orphans were deleted, err = %d\n",
+                               rc);
+                }
+        }
+        pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
+        
+        RETURN(0);
 }
 
 static int filter_destroy(struct obd_export *exp, struct obdo *oa,
                           struct lov_stripe_md *ea, struct obd_trans_info *oti)
 {
-        struct obd_device *obd;
-        struct filter_obd *filter;
+        int rc, rc2, cleanup_phase = 0, have_prepared = 0;
         struct dentry *dchild = NULL, *dparent = NULL;
-        struct obd_run_ctxt saved;
-        void *handle = NULL;
-        struct lustre_handle parent_lockh;
         struct llog_cookie *fcc = NULL;
-        int rc, rc2, cleanup_phase = 0, have_prepared = 0;
-        obd_gr group = 0;
+        struct lvfs_run_ctxt saved;
+        struct filter_obd *filter;
+        struct obd_device *obd;
+        void *handle = NULL;
+        void *lock = NULL;
+        struct iattr iattr;
         ENTRY;
 
-        if (oa->o_valid & OBD_MD_FLGROUP)
-                group = oa->o_gr;
+        LASSERT(oa->o_valid & OBD_MD_FLGROUP);
 
         obd = exp->exp_obd;
         filter = &obd->u.filter;
 
-        push_ctxt(&saved, &obd->obd_ctxt, NULL);
+        push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
 
  acquire_locks:
-        dparent = filter_parent_lock(obd, group, oa->o_id, LCK_PW,
-                                     &parent_lockh);
+        dparent = filter_parent_lock(obd, oa->o_gr, oa->o_id, &lock);
         if (IS_ERR(dparent))
                 GOTO(cleanup, rc = PTR_ERR(dparent));
         cleanup_phase = 1;
 
-        dchild = filter_fid2dentry(obd, dparent, group, oa->o_id);
+        dchild = filter_id2dentry(obd, dparent, oa->o_gr, oa->o_id);
         if (IS_ERR(dchild))
-                GOTO(cleanup, rc = -ENOENT);
+                GOTO(cleanup, rc = PTR_ERR(dchild));
         cleanup_phase = 2;
 
         if (dchild->d_inode == NULL) {
-                CERROR("destroying non-existent object "LPU64"\n", oa->o_id);
+                CDEBUG(D_INODE, "destroying non-existent object "LPU64"\n",
+                       oa->o_id);
                 GOTO(cleanup, rc = -ENOENT);
         }
 
@@ -1771,18 +2642,13 @@ static int filter_destroy(struct obd_export *exp, struct obdo *oa,
                  * complication of condition the above code to skip it on the
                  * second time through. */
                 f_dput(dchild);
-                filter_parent_unlock(dparent, &parent_lockh, LCK_PW);
+                filter_parent_unlock(dparent, lock);
 
-                filter_prepare_destroy(obd, oa->o_id);
+                filter_prepare_destroy(obd, oa->o_id, oa->o_gr);
                 have_prepared = 1;
                 goto acquire_locks;
         }
 
-        handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK_LOG, oti);
-        if (IS_ERR(handle))
-                GOTO(cleanup, rc = PTR_ERR(handle));
-        cleanup_phase = 3;
-
         /* Our MDC connection is established by the MDS to us */
         if (oa->o_valid & OBD_MD_FLCOOKIE) {
                 OBD_ALLOC(fcc, sizeof(*fcc));
@@ -1790,16 +2656,51 @@ static int filter_destroy(struct obd_export *exp, struct obdo *oa,
                         memcpy(fcc, obdo_logcookie(oa), sizeof(*fcc));
         }
 
+        /* we're gonna truncate it first in order to avoid possible
+         * deadlock:
+         *      P1                      P2
+         * open trasaction      open transaction
+         * down(i_zombie)       down(i_zombie)
+         *                      restart transaction
+         * (see BUG 4180) -bzzz
+         */
+        down(&dchild->d_inode->i_sem);
+        handle = fsfilt_start_log(obd, dparent->d_inode,FSFILT_OP_SETATTR,NULL,1);
+        if (IS_ERR(handle)) {
+                up(&dchild->d_inode->i_sem);
+                GOTO(cleanup, rc = PTR_ERR(handle));
+        }
+
+        iattr.ia_valid = ATTR_SIZE;
+        iattr.ia_size = 0;
+        rc = fsfilt_setattr(obd, dchild, handle, &iattr, 1);
+
+        rc2 = fsfilt_commit(obd, filter->fo_sb, dparent->d_inode, handle, 0);
+        up(&dchild->d_inode->i_sem);
+        if (rc)
+                GOTO(cleanup, rc);
+        if (rc2)
+                GOTO(cleanup, rc = rc2);
+
+        handle = fsfilt_start_log(obd, dparent->d_inode,FSFILT_OP_UNLINK,oti,1);
+        if (IS_ERR(handle))
+                GOTO(cleanup, rc = PTR_ERR(handle));
+
+        cleanup_phase = 3;
+
         rc = filter_destroy_internal(obd, oa->o_id, dparent, dchild);
 
 cleanup:
         switch(cleanup_phase) {
         case 3:
-                if (fcc != NULL)
-                        fsfilt_add_journal_cb(obd, 0, oti->oti_handle,
+                if (fcc != NULL) {
+                        fsfilt_add_journal_cb(obd, filter->fo_sb, 0,
+                                              oti ? oti->oti_handle : handle,
                                               filter_cancel_cookies_cb, fcc);
+                }
                 rc = filter_finish_transno(exp, oti, rc);
-                rc2 = fsfilt_commit(obd, dparent->d_inode, handle, 0);
+                rc2 = fsfilt_commit(obd, filter->fo_sb, dparent->d_inode, 
+                                    handle, exp->exp_sync);
                 if (rc2) {
                         CERROR("error on commit, err = %d\n", rc2);
                         if (!rc)
@@ -1808,15 +2709,9 @@ cleanup:
         case 2:
                 f_dput(dchild);
         case 1:
-                if (rc || oti == NULL) {
-                        filter_parent_unlock(dparent, &parent_lockh, LCK_PW);
-                } else {
-                        memcpy(&oti->oti_ack_locks[0].lock, &parent_lockh,
-                               sizeof(parent_lockh));
-                        oti->oti_ack_locks[0].mode = LCK_PW;
-                }
+                filter_parent_unlock(dparent, lock);
         case 0:
-                pop_ctxt(&saved, &obd->obd_ctxt, NULL);
+                pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
                 break;
         default:
                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
@@ -1839,7 +2734,7 @@ static int filter_truncate(struct obd_export *exp, struct obdo *oa,
                 CERROR("PUNCH not supported, only truncate: end = "LPX64"\n",
                        end);
 
-        CDEBUG(D_INODE, "calling truncate for object "LPU64", valid = %x, "
+        CDEBUG(D_INODE, "calling truncate for object "LPU64", valid = "LPU64", "
                "o_size = "LPD64"\n", oa->o_id, oa->o_valid, start);
         oa->o_size = start;
         error = filter_setattr(exp, oa, NULL, oti);
@@ -1849,28 +2744,31 @@ static int filter_truncate(struct obd_export *exp, struct obdo *oa,
 static int filter_sync(struct obd_export *exp, struct obdo *oa,
                        struct lov_stripe_md *lsm, obd_off start, obd_off end)
 {
-        struct obd_run_ctxt saved;
+        struct obd_device *obd = exp->exp_obd;
+        struct lvfs_run_ctxt saved;
         struct filter_obd *filter;
         struct dentry *dentry;
         int rc, rc2;
         ENTRY;
 
-        filter = &exp->exp_obd->u.filter;
+        filter = &obd->u.filter;
 
         /* an objid of zero is taken to mean "sync whole filesystem" */
-        if (!oa || !oa->o_valid & OBD_MD_FLID) {
-                rc = fsfilt_sync(exp->exp_obd, filter->fo_sb);
-                GOTO(out_exp, rc);
+        if (!oa || !(oa->o_valid & OBD_MD_FLID)) {
+                rc = fsfilt_sync(obd, filter->fo_sb);
+                /* flush any remaining cancel messages out to the target */
+                filter_sync_llogs(obd, NULL);
+                RETURN(rc);
         }
 
-        dentry = filter_oa2dentry(exp->exp_obd, oa);
+        dentry = filter_oa2dentry(obd, oa);
         if (IS_ERR(dentry))
-                GOTO(out_exp, rc = PTR_ERR(dentry));
+                RETURN(PTR_ERR(dentry));
 
-        push_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
+        push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
 
         down(&dentry->d_inode->i_sem);
-        rc = filemap_fdatasync(dentry->d_inode->i_mapping);
+        rc = filemap_fdatawrite(dentry->d_inode->i_mapping);
         if (rc == 0) {
                 /* just any file to grab fsync method - "file" arg unused */
                 struct file *file = filter->fo_rcvd_filp;
@@ -1887,23 +2785,16 @@ static int filter_sync(struct obd_export *exp, struct obdo *oa,
         oa->o_valid = OBD_MD_FLID;
         obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
 
-        pop_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
+        pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
 
         f_dput(dentry);
-out_exp:
         RETURN(rc);
 }
 
-static int filter_statfs(struct obd_device *obd, struct obd_statfs *osfs,
-                         unsigned long max_age)
-{
-        ENTRY;
-        RETURN(fsfilt_statfs(obd, obd->u.filter.fo_sb, osfs));
-}
-
 static int filter_get_info(struct obd_export *exp, __u32 keylen,
                            void *key, __u32 *vallen, void *val)
 {
+        struct filter_export_data *fed = &exp->exp_filter_data;
         struct obd_device *obd;
         ENTRY;
 
@@ -1932,45 +2823,94 @@ static int filter_get_info(struct obd_export *exp, __u32 keylen,
 
         if (keylen >= strlen("last_id") && memcmp(key, "last_id", 7) == 0) {
                 obd_id *last_id = val;
-                /* FIXME: object groups */
-                *last_id = filter_last_id(&obd->u.filter, 0);
+                *last_id = filter_last_id(&obd->u.filter, fed->fed_group);
+                RETURN(0);
+        }
+        if (keylen >= strlen("reint_log") && memcmp(key, "reint_log", 9) == 0) {
+                /*Get log_context handle*/
+                unsigned long *llh_handle = val;
+                *vallen = sizeof(unsigned long);
+                *llh_handle = (unsigned long)obd->obd_llog_ctxt[LLOG_REINT_ORIG_CTXT];
+                RETURN(0);
+        }
+        if (keylen >= strlen("cache_sb") && memcmp(key, "cache_sb", 8) == 0) {
+                /*Get log_context handle*/
+                unsigned long *sb = val;
+                *vallen = sizeof(unsigned long);
+                *sb = (unsigned long)obd->u.filter.fo_sb;
                 RETURN(0);
         }
+
         CDEBUG(D_IOCTL, "invalid key\n");
         RETURN(-EINVAL);
 }
 
-static int filter_set_info(struct obd_export *exp, __u32 keylen,
-                           void *key, __u32 vallen, void *val)
+struct obd_llogs *filter_grab_llog_for_group(struct obd_device *obd, int group,
+                                             struct obd_export *export)
 {
-        struct obd_device *obd;
-        struct lustre_handle conn;
-#ifdef ENABLE_ORPHANS
+        struct filter_group_llog *fglog, *nlog;
+        char name[32] = "CATLIST";
+        struct filter_obd *filter;
         struct llog_ctxt *ctxt;
-#endif
-        int rc = 0;
-        ENTRY;
+        struct list_head *cur;
+        int rc;
 
-        conn.cookie = exp->exp_handle.h_cookie;
+        filter = &obd->u.filter;
 
-        obd = exp->exp_obd;
-        if (obd == NULL) {
-                CDEBUG(D_IOCTL, "invalid exp %p cookie "LPX64"\n",
-                       exp, conn.cookie);
-                RETURN(-EINVAL);
+        spin_lock(&filter->fo_llog_list_lock);
+        list_for_each(cur, &filter->fo_llog_list) {
+                fglog = list_entry(cur, struct filter_group_llog, list);
+                if (fglog->group == group) {
+                        if (!(fglog->exp == NULL || fglog->exp == export || export == NULL))
+                                CWARN("%s: export for group %d changes: 0x%p -> 0x%p\n",
+                                      obd->obd_name, group, fglog->exp, export);
+                        spin_unlock(&filter->fo_llog_list_lock);
+                        goto init;
+                }
         }
+        spin_unlock(&filter->fo_llog_list_lock);
 
-        if (keylen < strlen("mds_conn") ||
-            memcmp(key, "mds_conn", keylen) != 0)
-                RETURN(-EINVAL);
+        if (export == NULL)
+                RETURN(NULL);
 
-        CWARN("Received MDS connection ("LPX64")\n", conn.cookie);
-        memcpy(&obd->u.filter.fo_mdc_conn, &conn, sizeof(conn));
-#ifdef ENABLE_ORPHANS
-        ctxt = llog_get_context(obd, LLOG_UNLINK_REPL_CTXT);
-        rc = llog_receptor_accept(ctxt, exp->exp_imp_reverse);
-#endif
-        RETURN(rc);
+        OBD_ALLOC(fglog, sizeof(*fglog));
+        if (fglog == NULL)
+                RETURN(NULL);
+        fglog->group = group;
+
+        OBD_ALLOC(fglog->llogs, sizeof(struct obd_llogs));
+        if (fglog->llogs == NULL) {
+                OBD_FREE(fglog, sizeof(*fglog));
+                RETURN(NULL);
+        }
+
+        spin_lock(&filter->fo_llog_list_lock);
+        list_for_each(cur, &filter->fo_llog_list) {
+                nlog = list_entry(cur, struct filter_group_llog, list);
+                LASSERT(nlog->group != group);
+        }
+        list_add(&fglog->list, &filter->fo_llog_list);
+        spin_unlock(&filter->fo_llog_list_lock);
+
+        rc = obd_llog_cat_initialize(obd, fglog->llogs, 1, name);
+        if (rc) {
+                OBD_FREE(fglog->llogs, sizeof(*(fglog->llogs)));
+                OBD_FREE(fglog, sizeof(*fglog));
+                RETURN(NULL);
+        }
+
+init:
+        if (export) {
+                fglog->exp = export;
+                ctxt = llog_get_context(fglog->llogs, LLOG_UNLINK_REPL_CTXT);
+                LASSERT(ctxt != NULL);
+
+                llog_receptor_accept(ctxt, export->exp_imp_reverse);
+        }
+        CDEBUG(D_OTHER, "%s: new llog 0x%p for group %u\n",
+               obd->obd_name, fglog->llogs, group);
+
+        RETURN(fglog->llogs);
 }
 
 int filter_iocontrol(unsigned int cmd, struct obd_export *exp,
@@ -1982,8 +2922,7 @@ int filter_iocontrol(unsigned int cmd, struct obd_export *exp,
 
         switch (cmd) {
         case OBD_IOC_ABORT_RECOVERY:
-                CERROR("aborting recovery for device %s\n", obd->obd_name);
-                target_abort_recovery(obd);
+                target_stop_recovery_thread(obd);
                 RETURN(0);
 
         case OBD_IOC_SET_READONLY: {
@@ -1993,33 +2932,32 @@ int filter_iocontrol(unsigned int cmd, struct obd_export *exp,
                 BDEVNAME_DECLARE_STORAGE(tmp);
                 CERROR("setting device %s read-only\n",
                        ll_bdevname(sb, tmp));
-                
+
                 handle = fsfilt_start(obd, inode, FSFILT_OP_MKNOD, NULL);
                 LASSERT(handle);
-                (void)fsfilt_commit(obd, inode, handle, 1);
+                (void)fsfilt_commit(obd, sb, inode, handle, 1);
 
-                dev_set_rdonly(ll_sbdev(obd->u.filter.fo_sb), 2);
+                ll_set_rdonly(ll_sbdev(obd->u.filter.fo_sb));
                 RETURN(0);
         }
 
         case OBD_IOC_CATLOGLIST: {
-                rc = llog_catlog_list(obd, 1, data);
+                rc = llog_catalog_list(obd, 1, data);
                 RETURN(rc);
         }
-
         case OBD_IOC_LLOG_CANCEL:
-        case OBD_IOC_LLOG_REMOVE: 
+        case OBD_IOC_LLOG_REMOVE:
         case OBD_IOC_LLOG_INFO:
         case OBD_IOC_LLOG_PRINT: {
                 /* FIXME to be finished */
                 RETURN(-EOPNOTSUPP);
 /*
                 struct llog_ctxt *ctxt = NULL;
-                
-                push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
+
+                push_ctxt(&saved, &ctxt->loc_ctxt, NULL);
                 rc = llog_ioctl(ctxt, cmd, data);
-                pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
-                
+                pop_ctxt(&saved, &ctxt->loc_ctxt, NULL);
+
                 RETURN(rc);
 */
         }
@@ -2034,134 +2972,180 @@ int filter_iocontrol(unsigned int cmd, struct obd_export *exp,
 static struct llog_operations filter_unlink_repl_logops;
 static struct llog_operations filter_size_orig_logops = {
         lop_setup: llog_obd_origin_setup,
-        lop_cleanup: llog_obd_origin_cleanup,
-        lop_add: llog_obd_origin_add
+        lop_cleanup: llog_catalog_cleanup,
+        lop_add: llog_catalog_add,
 };
 
-static int filter_llog_init(struct obd_device *obd, struct obd_device *tgt,
-                            int count, struct llog_logid *logid) 
+static int filter_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
+                            struct obd_device *tgt, int count,
+                            struct llog_catid *catid)
 {
         struct llog_ctxt *ctxt;
         int rc;
         ENTRY;
-        
+
         filter_unlink_repl_logops = llog_client_ops;
         filter_unlink_repl_logops.lop_cancel = llog_obd_repl_cancel;
         filter_unlink_repl_logops.lop_connect = llog_repl_connect;
         filter_unlink_repl_logops.lop_sync = llog_obd_repl_sync;
 
-        rc = llog_setup(obd, LLOG_UNLINK_REPL_CTXT, tgt, 0, NULL,
+        rc = obd_llog_setup(obd, llogs, LLOG_UNLINK_REPL_CTXT, tgt, 0, NULL,
                         &filter_unlink_repl_logops);
         if (rc)
                 RETURN(rc);
         /* FIXME - assign unlink_cb for filter's recovery */
-        ctxt = llog_get_context(obd, LLOG_UNLINK_REPL_CTXT);
+        ctxt = llog_get_context(llogs, LLOG_UNLINK_REPL_CTXT);
         ctxt->llog_proc_cb = filter_recov_log_unlink_cb;
 
-        rc = llog_setup(obd, LLOG_SIZE_ORIG_CTXT, tgt, 0, NULL,
-                        &filter_size_orig_logops);
+        /* FIXME - count should be 1 to setup size log */
+        rc = obd_llog_setup(obd, llogs, LLOG_SIZE_ORIG_CTXT, tgt, 0, 
+                            &catid->lci_logid, &filter_size_orig_logops);
         RETURN(rc);
 }
 
-static int filter_llog_finish(struct obd_device *obd, int count)
+static int filter_llog_finish(struct obd_device *obd,
+                              struct obd_llogs *llogs, int count)
 {
         int rc;
         ENTRY;
-        
-        rc = llog_cleanup(llog_get_context(obd, LLOG_UNLINK_REPL_CTXT));
+
+        rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_UNLINK_REPL_CTXT));
         if (rc)
                 RETURN(rc);
 
-        rc = llog_cleanup(llog_get_context(obd, LLOG_SIZE_ORIG_CTXT));
+        rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_SIZE_ORIG_CTXT));
+        RETURN(rc);
+}
+
+static int filter_llog_connect(struct obd_export *exp,
+                               struct llogd_conn_body *body) 
+{
+        struct obd_device *obd = exp->exp_obd;
+        struct llog_ctxt *ctxt;
+        struct obd_llogs *llog;
+        int rc;
+        ENTRY;
+
+        CDEBUG(D_OTHER, "handle connect for %s: %u/%u/%u\n", obd->obd_name,
+               (unsigned) body->lgdc_logid.lgl_ogr,
+               (unsigned) body->lgdc_logid.lgl_oid,
+               (unsigned) body->lgdc_logid.lgl_ogen);
+        llog = filter_grab_llog_for_group(obd, body->lgdc_logid.lgl_ogr, exp);
+        LASSERT(llog != NULL);
+        ctxt = llog_get_context(llog, body->lgdc_ctxt_idx);
+        rc = llog_connect(ctxt, 1, &body->lgdc_logid,
+                          &body->lgdc_gen, NULL);
+        if (rc != 0)
+                CERROR("failed to connect\n");
+
         RETURN(rc);
 }
 
-static struct dentry *filter_lvfs_fid2dentry(__u64 id, __u32 gen, __u64 gr,
-                                             void *data)
+static struct dentry *filter_lvfs_id2dentry(__u64 id, __u32 gen, 
+                                           __u64 gr, void *data)
 {
-        return filter_fid2dentry(data, NULL, gr, id);
+        return filter_id2dentry(data, NULL, gr, id);
 }
 
 static struct lvfs_callback_ops filter_lvfs_ops = {
-        l_fid2dentry:     filter_lvfs_fid2dentry,
+        l_id2dentry:     filter_lvfs_id2dentry,
 };
 
 static struct obd_ops filter_obd_ops = {
-        o_owner:          THIS_MODULE,
-        o_attach:         filter_attach,
-        o_detach:         filter_detach,
-        o_get_info:       filter_get_info,
-        o_set_info:       filter_set_info,
-        o_setup:          filter_setup,
-        o_postsetup:      filter_postsetup,
-        o_precleanup:     filter_precleanup,
-        o_cleanup:        filter_cleanup,
-        o_connect:        filter_connect,
-        o_disconnect:     filter_disconnect,
-        o_statfs:         filter_statfs,
-        o_getattr:        filter_getattr,
-        o_unpackmd:       filter_unpackmd,
-        o_create:         filter_create,
-        o_setattr:        filter_setattr,
-        o_destroy:        filter_destroy,
-        o_brw:            filter_brw,
-        o_punch:          filter_truncate,
-        o_sync:           filter_sync,
-        o_preprw:         filter_preprw,
-        o_commitrw:       filter_commitrw,
-        o_destroy_export: filter_destroy_export,
-        o_llog_init:      filter_llog_init,
-        o_llog_finish:    filter_llog_finish,
-        o_iocontrol:      filter_iocontrol,
+        .o_owner          = THIS_MODULE,
+        .o_attach         = filter_attach,
+        .o_detach         = filter_detach,
+        .o_get_info       = filter_get_info,
+        .o_setup          = filter_setup,
+        .o_precleanup     = filter_precleanup,
+        .o_cleanup        = filter_cleanup,
+        .o_process_config = filter_process_config,
+        .o_connect        = filter_connect,
+        .o_connect_post   = filter_connect_post,
+        .o_disconnect     = filter_disconnect,
+        .o_statfs         = filter_statfs,
+        .o_getattr        = filter_getattr,
+        .o_unpackmd       = filter_unpackmd,
+        .o_create         = filter_create,
+        .o_setattr        = filter_setattr,
+        .o_destroy        = filter_destroy,
+        .o_brw            = filter_brw,
+        .o_punch          = filter_truncate,
+        .o_sync           = filter_sync,
+        .o_preprw         = filter_preprw,
+        .o_commitrw       = filter_commitrw,
+        .o_do_cow         = filter_do_cow,
+        .o_write_extents  = filter_write_extents,
+        .o_destroy_export = filter_destroy_export,
+        .o_llog_init      = filter_llog_init,
+        .o_llog_finish    = filter_llog_finish,
+        .o_llog_connect   = filter_llog_connect,
+        .o_iocontrol      = filter_iocontrol,
 };
 
 static struct obd_ops filter_sanobd_ops = {
-        o_owner:          THIS_MODULE,
-        o_attach:         filter_attach,
-        o_detach:         filter_detach,
-        o_get_info:       filter_get_info,
-        o_set_info:       filter_set_info,
-        o_setup:          filter_san_setup,
-        o_precleanup:     filter_precleanup,
-        o_cleanup:        filter_cleanup,
-        o_connect:        filter_connect,
-        o_disconnect:     filter_disconnect,
-        o_statfs:         filter_statfs,
-        o_getattr:        filter_getattr,
-        o_unpackmd:       filter_unpackmd,
-        o_create:         filter_create,
-        o_setattr:        filter_setattr,
-        o_destroy:        filter_destroy,
-        o_brw:            filter_brw,
-        o_punch:          filter_truncate,
-        o_sync:           filter_sync,
-        o_preprw:         filter_preprw,
-        o_commitrw:       filter_commitrw,
-        o_san_preprw:     filter_san_preprw,
-        o_destroy_export: filter_destroy_export,
-        o_llog_init:      filter_llog_init,
-        o_llog_finish:    filter_llog_finish,
-        o_iocontrol:      filter_iocontrol,
+        .o_owner          = THIS_MODULE,
+        .o_attach         = filter_attach,
+        .o_detach         = filter_detach,
+        .o_get_info       = filter_get_info,
+        .o_setup          = filter_san_setup,
+        .o_precleanup     = filter_precleanup,
+        .o_cleanup        = filter_cleanup,
+        .o_connect        = filter_connect,
+        .o_connect_post   = filter_connect_post,
+        .o_disconnect     = filter_disconnect,
+        .o_statfs         = filter_statfs,
+        .o_getattr        = filter_getattr,
+        .o_unpackmd       = filter_unpackmd,
+        .o_create         = filter_create,
+        .o_setattr        = filter_setattr,
+        .o_destroy        = filter_destroy,
+        .o_brw            = filter_brw,
+        .o_punch          = filter_truncate,
+        .o_sync           = filter_sync,
+        .o_preprw         = filter_preprw,
+        .o_commitrw       = filter_commitrw,
+        .o_do_cow         = filter_do_cow,
+        .o_write_extents  = filter_write_extents,
+        .o_san_preprw     = filter_san_preprw,
+        .o_destroy_export = filter_destroy_export,
+        .o_llog_init      = filter_llog_init,
+        .o_llog_finish    = filter_llog_finish,
+        .o_llog_connect   = filter_llog_connect,
+        .o_iocontrol      = filter_iocontrol,
 };
 
 static int __init obdfilter_init(void)
 {
         struct lprocfs_static_vars lvars;
-        int rc;
+        int size, rc;
 
         printk(KERN_INFO "Lustre: Filtering OBD driver; info@clusterfs.com\n");
 
         lprocfs_init_vars(filter, &lvars);
 
-        rc = class_register_type(&filter_obd_ops, lvars.module_vars,
+        size = OBDFILTER_CREATED_SCRATCHPAD_ENTRIES * 
+                sizeof(*obdfilter_created_scratchpad);
+        
+        OBD_ALLOC(obdfilter_created_scratchpad, size);
+        if (obdfilter_created_scratchpad == NULL) {
+                CERROR ("Can't allocate scratchpad\n");
+                return -ENOMEM;
+        }
+
+        rc = class_register_type(&filter_obd_ops, NULL, lvars.module_vars,
                                  OBD_FILTER_DEVICENAME);
-        if (rc)
+        if (rc) {
+                OBD_FREE(obdfilter_created_scratchpad, size);
                 return rc;
+        }
 
-        rc = class_register_type(&filter_sanobd_ops, lvars.module_vars,
+        rc = class_register_type(&filter_sanobd_ops, NULL, lvars.module_vars,
                                  OBD_FILTER_SAN_DEVICENAME);
-        if (rc)
+        if (rc) {
                 class_unregister_type(OBD_FILTER_DEVICENAME);
+                OBD_FREE(obdfilter_created_scratchpad, size);
+        }
         return rc;
 }
 
@@ -2169,6 +3153,9 @@ static void __exit obdfilter_exit(void)
 {
         class_unregister_type(OBD_FILTER_SAN_DEVICENAME);
         class_unregister_type(OBD_FILTER_DEVICENAME);
+        OBD_FREE(obdfilter_created_scratchpad,
+                 OBDFILTER_CREATED_SCRATCHPAD_ENTRIES * 
+                 sizeof(*obdfilter_created_scratchpad));
 }
 
 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");