Whamcloud - gitweb
Fixes and cleanups in lmv.
[fs/lustre-release.git] / lustre / mds / handler.c
index 4f5f6e3..dfdad07 100644 (file)
@@ -4,7 +4,7 @@
  *  lustre/mds/handler.c
  *  Lustre Metadata Server (mds) request handler
  *
- *  Copyright (c) 2001, 2002 Cluster File Systems, Inc.
+ *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
  *   Author: Peter Braam <braam@clusterfs.com>
  *   Author: Andreas Dilger <adilger@clusterfs.com>
  *   Author: Phil Schwan <phil@clusterfs.com>
@@ -26,7 +26,9 @@
  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
-#define EXPORT_SYMTAB
+#ifndef EXPORT_SYMTAB
+# define EXPORT_SYMTAB
+#endif
 #define DEBUG_SUBSYSTEM S_MDS
 
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/obd_class.h>
 #include <linux/random.h>
-#include <linux/locks.h>
+#include <linux/fs.h>
+#include <linux/jbd.h>
+#include <linux/ext3_fs.h>
 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
-#include <linux/buffer_head.h>
+# include <linux/smp_lock.h>
+# include <linux/buffer_head.h>
+# include <linux/workqueue.h>
+# include <linux/mount.h>
+#else
+# include <linux/locks.h>
 #endif
 #include <linux/obd_lov.h>
+#include <linux/lustre_mds.h>
+#include <linux/lustre_fsfilt.h>
 #include <linux/lprocfs_status.h>
+#include <linux/lustre_commit_confd.h>
 
-static kmem_cache_t *mds_file_cache;
-
-extern int mds_get_lovtgts(struct mds_obd *obd, int tgt_count,
-                           obd_uuid_t *uuidarray);
-extern int mds_get_lovdesc(struct mds_obd  *obd, struct lov_desc *desc);
-extern void mds_start_transno(struct mds_obd *mds);
-extern int mds_finish_transno(struct mds_obd *mds, void *handle,
-                              struct ptlrpc_request *req, int rc);
-static int mds_cleanup(struct obd_device * obddev);
-
-extern struct lprocfs_vars status_var_nm_1[];
-extern struct lprocfs_vars status_class_var[];
-
-inline struct mds_obd *mds_req2mds(struct ptlrpc_request *req)
-{
-        return &req->rq_export->exp_obd->u.mds;
-}
-
-static int mds_bulk_timeout(void *data)
-{
-        struct ptlrpc_bulk_desc *desc = data;
+#include "mds_internal.h"
 
-        ENTRY;
-        recovd_conn_fail(desc->bd_connection);
-        RETURN(1);
-}
+static int mds_intent_policy(struct ldlm_namespace *ns,
+                             struct ldlm_lock **lockp, void *req_cookie,
+                             ldlm_mode_t mode, int flags, void *data);
+static int mds_postsetup(struct obd_device *obd);
+static int mds_cleanup(struct obd_device *obd, int flags);
 
 /* Assumes caller has already pushed into the kernel filesystem context */
 static int mds_sendpage(struct ptlrpc_request *req, struct file *file,
-                        __u64 offset)
+                        loff_t offset, int count)
 {
-        int rc = 0;
-        struct mds_obd *mds = mds_req2mds(req);
         struct ptlrpc_bulk_desc *desc;
-        struct ptlrpc_bulk_page *bulk;
         struct l_wait_info lwi;
-        char *buf;
+        struct page **pages;
+        int rc = 0, npages, i, tmpcount, tmpsize = 0;
         ENTRY;
 
-        desc = ptlrpc_prep_bulk(req->rq_connection);
-        if (desc == NULL)
+        LASSERT((offset & (PAGE_SIZE - 1)) == 0); /* I'm dubious about this */
+
+        npages = (count + PAGE_SIZE - 1) >> PAGE_SHIFT;
+        OBD_ALLOC(pages, sizeof(*pages) * npages);
+        if (!pages)
                 GOTO(out, rc = -ENOMEM);
 
-        bulk = ptlrpc_prep_bulk_page(desc);
-        if (bulk == NULL)
-                GOTO(cleanup_bulk, rc = -ENOMEM);
+        desc = ptlrpc_prep_bulk_exp(req, npages, BULK_PUT_SOURCE,
+                                    MDS_BULK_PORTAL);
+        if (desc == NULL)
+                GOTO(out_free, rc = -ENOMEM);
+
+        for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
+                tmpsize = tmpcount > PAGE_SIZE ? PAGE_SIZE : tmpcount;
+
+                pages[i] = alloc_pages(GFP_KERNEL, 0);
+                if (pages[i] == NULL)
+                        GOTO(cleanup_buf, rc = -ENOMEM);
+
+                ptlrpc_prep_bulk_page(desc, pages[i], 0, tmpsize);
+        }
 
-        OBD_ALLOC(buf, PAGE_SIZE);
-        if (buf == NULL)
-                GOTO(cleanup_bulk, rc = -ENOMEM);
+        for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
+                tmpsize = tmpcount > PAGE_SIZE ? PAGE_SIZE : tmpcount;
+                CDEBUG(D_EXT2, "reading %u@%llu from dir %lu (size %llu)\n",
+                       tmpsize, offset, file->f_dentry->d_inode->i_ino,
+                       file->f_dentry->d_inode->i_size);
 
-        rc = mds_fs_readpage(mds, file, buf, PAGE_SIZE, (loff_t *)&offset);
+                rc = fsfilt_readpage(req->rq_export->exp_obd, file,
+                                     kmap(pages[i]), tmpsize, &offset);
+                kunmap(pages[i]);
 
-        if (rc != PAGE_SIZE)
-                GOTO(cleanup_buf, rc = -EIO);
+                if (rc != tmpsize)
+                        GOTO(cleanup_buf, rc = -EIO);
+        }
 
-        bulk->bp_xid = req->rq_xid;
-        bulk->bp_buf = buf;
-        bulk->bp_buflen = PAGE_SIZE;
-        desc->bd_portal = MDS_BULK_PORTAL;
+        LASSERT(desc->bd_nob == count);
 
-        rc = ptlrpc_send_bulk(desc);
+        rc = ptlrpc_start_bulk_transfer(desc);
         if (rc)
                 GOTO(cleanup_buf, rc);
 
         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
                 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
                        OBD_FAIL_MDS_SENDPAGE, rc);
-                ptlrpc_abort_bulk(desc);
-                GOTO(cleanup_buf, rc);
+                GOTO(abort_bulk, rc);
         }
 
-        lwi = LWI_TIMEOUT(obd_timeout * HZ, mds_bulk_timeout, desc);
-        rc = l_wait_event(desc->bd_waitq, desc->bd_flags & PTL_BULK_FL_SENT,
-                          &lwi);
-        if (rc) {
-                if (rc != -ETIMEDOUT)
-                        LBUG();
-                GOTO(cleanup_buf, rc);
+        lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
+        rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc), &lwi);
+        LASSERT (rc == 0 || rc == -ETIMEDOUT);
+
+        if (rc == 0) {
+                if (desc->bd_success &&
+                    desc->bd_nob_transferred == count)
+                        GOTO(cleanup_buf, rc);
+
+                rc = -ETIMEDOUT; /* XXX should this be a different errno? */
         }
 
+        DEBUG_REQ(D_ERROR, req, "bulk failed: %s %d(%d), evicting %s@%s\n",
+                  (rc == -ETIMEDOUT) ? "timeout" : "network error",
+                  desc->bd_nob_transferred, count,
+                  req->rq_export->exp_client_uuid.uuid,
+                  req->rq_export->exp_connection->c_remote_uuid.uuid);
+
+        ptlrpc_fail_export(req->rq_export);
+
         EXIT;
+ abort_bulk:
+        ptlrpc_abort_bulk (desc);
  cleanup_buf:
-        OBD_FREE(buf, PAGE_SIZE);
- cleanup_bulk:
+        for (i = 0; i < npages; i++)
+                if (pages[i])
+                        __free_pages(pages[i], 0);
+
         ptlrpc_free_bulk(desc);
+ out_free:
+        OBD_FREE(pages, sizeof(*pages) * npages);
  out:
         return rc;
 }
 
-/*
- * Look up a named entry in a directory, and get an LDLM lock on it.
- * 'dir' is a inode for which an LDLM lock has already been taken.
- *
- * If we do not need an exclusive or write lock on this entry (e.g.
- * a read lock for attribute lookup only) then we do not hold the
- * directory on return.  It is up to the caller to know what type
- * of lock it is getting, and clean up appropriately.
- */
-struct dentry *mds_name2locked_dentry(struct obd_device *obd,
-                                      struct dentry *dir, struct vfsmount **mnt,
-                                      char *name, int namelen, int lock_mode,
-                                      struct lustre_handle *lockh,
-                                      int dir_lock_mode)
+int mds_lock_mode_for_dir(struct obd_device *obd,
+                              struct dentry *dentry, int mode)
 {
-        struct dentry *dchild;
-        int flags = 0, rc;
-        __u64 res_id[3] = {0};
-        ENTRY;
-
-        down(&dir->d_inode->i_sem);
-        dchild = lookup_one_len(name, dir, namelen);
-        if (IS_ERR(dchild)) {
-                CERROR("child lookup error %ld\n", PTR_ERR(dchild));
-                up(&dir->d_inode->i_sem);
-                LBUG();
-                RETURN(dchild);
-        }
-        if (dir_lock_mode != LCK_EX && dir_lock_mode != LCK_PW) {
-                up(&dir->d_inode->i_sem);
-                ldlm_lock_decref(lockh, dir_lock_mode);
-        }
-
-        if (lock_mode == 0 || !dchild->d_inode)
-                RETURN(dchild);
-
-        res_id[0] = dchild->d_inode->i_ino;
-        res_id[1] = dchild->d_inode->i_generation;
-        rc = ldlm_match_or_enqueue(NULL, NULL, obd->obd_namespace, NULL,
-                                   res_id, LDLM_PLAIN, NULL, 0, lock_mode,
-                                   &flags, ldlm_completion_ast,
-                                   mds_blocking_ast, NULL, 0, lockh);
-        if (rc != ELDLM_OK) {
-                l_dput(dchild);
-                up(&dir->d_inode->i_sem);
-                RETURN(ERR_PTR(-ENOLCK)); /* XXX translate ldlm code */
+        int ret_mode;
+
+        /* any dir access needs couple locks:
+         * 1) on part of dir we gonna lookup/modify in
+         * 2) on a whole dir to protect it from concurrent splitting
+         *    and to flush client's cache for readdir()
+         * so, for a given mode and dentry this routine decides what
+         * lock mode to use for lock #2:
+         * 1) if caller's gonna lookup in dir then we need to protect
+         *    dir from being splitted only - LCK_CR
+         * 2) if caller's gonna modify dir then we need to protect
+         *    dir from being splitted and to flush cache - LCK_CW
+         * 3) if caller's gonna modify dir and that dir seems ready
+         *    for splitting then we need to protect it from any
+         *    type of access (lookup/modify/split) - LCK_EX -bzzz */
+
+        if (mode == LCK_PR) {
+                ret_mode = LCK_CR;
+        } else if (mode == LCK_PW) {
+                /* caller gonna modify directory.we use concurrent
+                   write lock here to retract client's cache for readdir */
+                ret_mode = LCK_CW;
+                if (mds_splitting_expected(obd, dentry)) {
+                        /* splitting possible. serialize any access */
+                        CERROR("%s: gonna split %lu/%lu\n",
+                               obd->obd_name,
+                               (unsigned long) dentry->d_inode->i_ino,
+                               (unsigned long) dentry->d_inode->i_generation);
+                        ret_mode = LCK_EX;
+                }
+        } else {
+                CWARN("unexpected lock mode %d\n", mode);
+                ret_mode = LCK_EX;
         }
-
-        RETURN(dchild);
+        return ret_mode;
 }
 
+/* only valid locked dentries or errors should be returned */
 struct dentry *mds_fid2locked_dentry(struct obd_device *obd, struct ll_fid *fid,
                                      struct vfsmount **mnt, int lock_mode,
-                                     struct lustre_handle *lockh)
+                                     struct lustre_handle *lockh, int *mode,
+                                     char *name, int namelen, __u64 lockpart)
 {
         struct mds_obd *mds = &obd->u.mds;
         struct dentry *de = mds_fid2dentry(mds, fid, mnt), *retval = de;
+        struct ldlm_res_id res_id = { .name = {0} };
         int flags = 0, rc;
-        __u64 res_id[3] = {0};
+        ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
+
         ENTRY;
 
         if (IS_ERR(de))
                 RETURN(de);
 
-        res_id[0] = de->d_inode->i_ino;
-        res_id[1] = de->d_inode->i_generation;
-        rc = ldlm_match_or_enqueue(NULL, NULL, obd->obd_namespace, NULL,
-                                   res_id, LDLM_PLAIN, NULL, 0, lock_mode,
-                                   &flags, ldlm_completion_ast,
-                                   mds_blocking_ast, NULL, 0, lockh);
+        res_id.name[0] = de->d_inode->i_ino;
+        res_id.name[1] = de->d_inode->i_generation;
+        lockh[1].cookie = 0;
+#ifdef S_PDIROPS
+        if (name && IS_PDIROPS(de->d_inode)) {
+                ldlm_policy_data_t cpolicy =
+                        { .l_inodebits = { MDS_INODELOCK_UPDATE } };
+                LASSERT(mode != NULL);
+                *mode = mds_lock_mode_for_dir(obd, de, lock_mode);
+                rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
+                                res_id, LDLM_IBITS,
+                                &cpolicy, *mode, &flags,
+                                mds_blocking_ast,
+                                ldlm_completion_ast, NULL, NULL,
+                                NULL, 0, NULL, lockh + 1);
+                if (rc != ELDLM_OK) {
+                        l_dput(de);
+                        RETURN(ERR_PTR(-ENOLCK));
+                }
+                flags = 0;
+
+                res_id.name[2] = full_name_hash(name, namelen);
+
+                CDEBUG(D_INFO, "take lock on %lu:%u:"LPX64"\n",
+                       de->d_inode->i_ino, de->d_inode->i_generation,
+                       res_id.name[2]);
+        }
+#endif
+        rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, res_id,
+                              LDLM_IBITS, &policy, lock_mode, &flags,
+                              mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
+                              NULL, 0, NULL, lockh);
         if (rc != ELDLM_OK) {
                 l_dput(de);
-                retval = ERR_PTR(-ENOLCK); /* XXX translate ldlm code */
+                retval = ERR_PTR(-EIO); /* XXX translate ldlm code */
+#ifdef S_PDIROPS
+                if (lockh[1].cookie)
+                        ldlm_lock_decref(lockh + 1, LCK_CW);
+#endif
         }
 
         RETURN(retval);
@@ -214,66 +260,60 @@ struct dentry *mds_fid2locked_dentry(struct obd_device *obd, struct ll_fid *fid,
 #define DCACHE_DISCONNECTED DCACHE_NFSD_DISCONNECTED
 #endif
 
+
 /* Look up an entry by inode number. */
+/* this function ONLY returns valid dget'd dentries with an initialized inode
+   or errors */
 struct dentry *mds_fid2dentry(struct mds_obd *mds, struct ll_fid *fid,
                               struct vfsmount **mnt)
 {
-        /* stolen from NFS */
-        struct super_block *sb = mds->mds_sb;
+        char fid_name[32];
         unsigned long ino = fid->id;
         __u32 generation = fid->generation;
         struct inode *inode;
-        struct list_head *lp;
         struct dentry *result;
 
         if (ino == 0)
                 RETURN(ERR_PTR(-ESTALE));
 
-        inode = iget(sb, ino);
-        if (inode == NULL)
-                RETURN(ERR_PTR(-ENOMEM));
+        snprintf(fid_name, sizeof(fid_name), "0x%lx", ino);
+
+        CDEBUG(D_DENTRY, "--> mds_fid2dentry: ino/gen %lu/%u, sb %p\n",
+               ino, generation, mds->mds_sb);
 
-        CDEBUG(D_DENTRY, "--> mds_fid2dentry: sb %p\n", inode->i_sb);
+        /* under ext3 this is neither supposed to return bad inodes
+           nor NULL inodes. */
+        result = ll_lookup_one_len(fid_name, mds->mds_fid_de, strlen(fid_name));
+        if (IS_ERR(result))
+                RETURN(result);
+
+        inode = result->d_inode;
+        if (!inode)
+                RETURN(ERR_PTR(-ENOENT));
 
-        if (is_bad_inode(inode) ||
-            (generation && inode->i_generation != generation)) {
+#if 0
+        /* here we disabled generation check, as root inode i_generation
+         * of cache mds and real mds are different. */
+        if (generation && inode->i_generation != generation) {
                 /* we didn't find the right inode.. */
-                CERROR("bad inode %lu, link: %d ct: %d or version  %u/%u\n",
-                       inode->i_ino, inode->i_nlink,
+                CERROR("bad inode %lu, link: %lu ct: %d or generation %u/%u\n",
+                       inode->i_ino, (unsigned long)inode->i_nlink,
                        atomic_read(&inode->i_count), inode->i_generation,
                        generation);
-                iput(inode);
+                dput(result);
                 RETURN(ERR_PTR(-ENOENT));
         }
+#endif
 
-        /* now to find a dentry. If possible, get a well-connected one */
-        if (mnt)
+        if (mnt) {
                 *mnt = mds->mds_vfsmnt;
-        spin_lock(&dcache_lock);
-        list_for_each(lp, &inode->i_dentry) {
-                result = list_entry(lp, struct dentry, d_alias);
-                if (!(result->d_flags & DCACHE_DISCONNECTED)) {
-                        dget_locked(result);
-                        result->d_vfs_flags |= DCACHE_REFERENCED;
-                        spin_unlock(&dcache_lock);
-                        iput(inode);
-                        if (mnt)
-                                mntget(*mnt);
-                        return result;
-                }
-        }
-        spin_unlock(&dcache_lock);
-        result = d_alloc_root(inode);
-        if (result == NULL) {
-                iput(inode);
-                return ERR_PTR(-ENOMEM);
-        }
-        if (mnt)
                 mntget(*mnt);
-        result->d_flags |= DCACHE_DISCONNECTED;
-        return result;
+        }
+
+        RETURN(result);
 }
 
+
 /* Establish a connection to the MDS.
  *
  * This will set up an export structure for the client to hold state data
@@ -281,75 +321,23 @@ struct dentry *mds_fid2dentry(struct mds_obd *mds, struct ll_fid *fid,
  * on the server, etc.
  */
 static int mds_connect(struct lustre_handle *conn, struct obd_device *obd,
-                       obd_uuid_t cluuid, struct recovd_obd *recovd,
-                       ptlrpc_recovery_cb_t recover)
+                       struct obd_uuid *cluuid)
 {
         struct obd_export *exp;
-        struct mds_export_data *med;
+        struct mds_export_data *med; /*  */
         struct mds_client_data *mcd;
-        struct list_head *p;
-        int rc;
+        int rc, abort_recovery;
         ENTRY;
 
         if (!conn || !obd || !cluuid)
                 RETURN(-EINVAL);
 
-        MOD_INC_USE_COUNT;
-
-        spin_lock(&obd->obd_dev_lock);
-        list_for_each(p, &obd->obd_exports) {
-                exp = list_entry(p, struct obd_export, exp_obd_chain);
-                mcd = exp->exp_mds_data.med_mcd;
-                if (!mcd) {
-                        CERROR("FYI: NULL mcd - simultaneous connects\n");
-                        continue;
-                }
-                if (!memcmp(cluuid, mcd->mcd_uuid, sizeof mcd->mcd_uuid)) {
-                        /* XXX make handle-found-export a subroutine */
-                        LASSERT(exp->exp_obd == obd);
-
-                        spin_unlock(&obd->obd_dev_lock);
-                        if (exp->exp_connection) {
-                                struct lustre_handle *hdl;
-                                hdl = &exp->exp_ldlm_data.led_import.imp_handle;
-                                /* Might be a re-connect after a partition. */
-                                if (!memcmp(conn, hdl, sizeof *conn)) {
-                                        CERROR("%s reconnecting\n", cluuid);
-                                        conn->addr = (__u64) (unsigned long)exp;
-                                        conn->cookie = exp->exp_cookie;
-                                        rc = EALREADY;
-                                } else {
-                                        CERROR("%s reconnecting from %s, "
-                                               "handle mismatch (ours %Lx/%Lx, "
-                                               "theirs %Lx/%Lx)\n", cluuid,
-                                               exp->exp_connection->
-                                               c_remote_uuid, hdl->addr,
-                                               hdl->cookie, conn->addr,
-                                               conn->cookie);
-                                        /* XXX disconnect them here? */
-                                        memset(conn, 0, sizeof *conn);
-                                        rc = -EALREADY;
-                                }
-                                MOD_DEC_USE_COUNT;
-                                RETURN(rc);
-                        }
-                        conn->addr = (__u64) (unsigned long)exp;
-                        conn->cookie = exp->exp_cookie;
-                        CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
-                               cluuid, exp);
-                        CDEBUG(D_IOCTL,"connect: addr %Lx cookie %Lx\n",
-                               (long long)conn->addr, (long long)conn->cookie);
-                        RETURN(0);
-                }
-        }
-        spin_unlock(&obd->obd_dev_lock);
-
-        if (obd->u.mds.mds_recoverable_clients != 0) {
-                CERROR("denying connection for new client %s: in recovery\n",
-                       cluuid);
-                MOD_DEC_USE_COUNT;
-                RETURN(-EBUSY);
-        }
+        /* Check for aborted recovery. */
+        spin_lock_bh(&obd->obd_processing_task_lock);
+        abort_recovery = obd->obd_abort_recovery;
+        spin_unlock_bh(&obd->obd_processing_task_lock);
+        if (abort_recovery)
+                target_abort_recovery(obd);
 
         /* XXX There is a small race between checking the list and adding a
          * new connection for the same UUID, but the real threat (list
@@ -363,7 +351,7 @@ static int mds_connect(struct lustre_handle *conn, struct obd_device *obd,
          */
         rc = class_connect(conn, obd, cluuid);
         if (rc)
-                GOTO(out_dec, rc);
+                RETURN(rc);
         exp = class_conn2export(conn);
         LASSERT(exp);
         med = &exp->exp_mds_data;
@@ -371,92 +359,145 @@ static int mds_connect(struct lustre_handle *conn, struct obd_device *obd,
         OBD_ALLOC(mcd, sizeof(*mcd));
         if (!mcd) {
                 CERROR("mds: out of memory for client data\n");
-                GOTO(out_export, rc = -ENOMEM);
+                GOTO(out, rc = -ENOMEM);
         }
 
         memcpy(mcd->mcd_uuid, cluuid, sizeof(mcd->mcd_uuid));
         med->med_mcd = mcd;
 
-        INIT_LIST_HEAD(&med->med_open_head);
-        spin_lock_init(&med->med_open_lock);
-
-        rc = mds_client_add(&obd->u.mds, med, -1);
-        if (rc)
-                GOTO(out_mcd, rc);
-
-        RETURN(0);
-
-out_mcd:
-        OBD_FREE(mcd, sizeof(*mcd));
-out_export:
-        class_disconnect(conn);
-out_dec:
-        MOD_DEC_USE_COUNT;
+        rc = mds_client_add(obd, &obd->u.mds, med, -1);
+        if (rc == 0)
+                EXIT;
+out:
+        if (rc) {
+                OBD_FREE(mcd, sizeof(*mcd));
+                class_disconnect(exp, 0);
+        }
+        class_export_put(exp);
 
         return rc;
 }
 
-/* Call with med->med_open_lock held, please. */
-inline int mds_close_mfd(struct mds_file_data *mfd, struct mds_export_data *med)
+static int mds_init_export(struct obd_export *exp)
 {
-        struct file *file = mfd->mfd_file;
-        LASSERT(file->private_data == mfd);
-
-        list_del(&mfd->mfd_list);
-        mfd->mfd_servercookie = DEAD_HANDLE_MAGIC;
-        kmem_cache_free(mds_file_cache, mfd);
+        struct mds_export_data *med = &exp->exp_mds_data;
 
-        return filp_close(file, 0);
+        INIT_LIST_HEAD(&med->med_open_head);
+        spin_lock_init(&med->med_open_lock);
+        RETURN(0);
 }
 
-static int mds_disconnect(struct lustre_handle *conn)
+static int mds_destroy_export(struct obd_export *export)
 {
-        struct obd_export *export = class_conn2export(conn);
-        struct list_head *tmp, *n;
-        struct mds_export_data *med = &export->exp_mds_data;
-        int rc;
+        struct mds_export_data *med;
+        struct obd_device *obd = export->exp_obd;
+        struct lvfs_run_ctxt saved;
+        int rc = 0;
         ENTRY;
 
-        /*
-         * Close any open files.
-         */
+        med = &export->exp_mds_data;
+        target_destroy_export(export);
+
+        if (obd_uuid_equals(&export->exp_client_uuid, &obd->obd_uuid))
+                GOTO(out, 0);
+
+        push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
+
+        /* Close any open files (which may also cause orphan unlinking). */
         spin_lock(&med->med_open_lock);
-        list_for_each_safe(tmp, n, &med->med_open_head) {
+        while (!list_empty(&med->med_open_head)) {
+                struct list_head *tmp = med->med_open_head.next;
                 struct mds_file_data *mfd =
                         list_entry(tmp, struct mds_file_data, mfd_list);
-                rc = mds_close_mfd(mfd, med);
-                if (rc) {
-                        /* XXX better diagnostics, with file path and stuff */
-                        CDEBUG(D_INODE, "Error %d closing mfd %p\n", rc, mfd);
-                }
+                BDEVNAME_DECLARE_STORAGE(btmp);
+
+                /* bug 1579: fix force-closing for 2.5 */
+                struct dentry *dentry = mfd->mfd_dentry;
+
+                list_del(&mfd->mfd_list);
+                spin_unlock(&med->med_open_lock);
+
+                /* If you change this message, be sure to update
+                 * replay_single:test_46 */
+                CERROR("force closing client file handle for %*s (%s:%lu)\n",
+                       dentry->d_name.len, dentry->d_name.name,
+                       ll_bdevname(dentry->d_inode->i_sb, btmp),
+                       dentry->d_inode->i_ino);
+                rc = mds_mfd_close(NULL, obd, mfd,
+                                   !(export->exp_flags & OBD_OPT_FAILOVER));
+
+                if (rc)
+                        CDEBUG(D_INODE, "Error closing file: %d\n", rc);
+                spin_lock(&med->med_open_lock);
         }
         spin_unlock(&med->med_open_lock);
+        pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
 
-        ldlm_cancel_locks_for_export(export);
-        mds_client_free(export);
-
-        rc = class_disconnect(conn);
-        if (!rc)
-                MOD_DEC_USE_COUNT;
+out:
+        mds_client_free(export, !(export->exp_flags & OBD_OPT_FAILOVER));
 
         RETURN(rc);
 }
 
-/*
- * XXX This is NOT guaranteed to flush all transactions to disk (even though
- *     it is equivalent to calling sync()) because it only _starts_ the flush
- *     and does not wait for completion.  It's better than nothing though.
- *     What we really want is a mild form of fsync_dev_lockfs(), but it is
- *     non-standard, or enabling do_sync_supers in ext3, just for this call.
- */
-static void mds_fsync_super(struct super_block *sb)
+static int mds_disconnect(struct obd_export *exp, int flags)
 {
-        lock_kernel();
-        lock_super(sb);
-        if (sb->s_dirt && sb->s_op && sb->s_op->write_super)
-                sb->s_op->write_super(sb);
-        unlock_super(sb);
-        unlock_kernel();
+        struct obd_device *obd;
+        struct mds_obd *mds;
+        unsigned long irqflags;
+        int rc;
+        ENTRY;
+
+        LASSERT(exp);
+        class_export_get(exp);
+
+        obd = class_exp2obd(exp);
+        if (obd == NULL) {
+                CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
+                       exp->exp_handle.h_cookie);
+                RETURN(-EINVAL);
+        }
+        mds = &obd->u.mds;
+
+        if (!(exp->exp_flags & OBD_OPT_REAL_CLIENT)
+                        && !atomic_read(&mds->mds_real_clients)) {
+                /* there was no client at all */
+                mds_lmv_disconnect(obd, flags);
+        }
+
+        if ((exp->exp_flags & OBD_OPT_REAL_CLIENT)
+                        && atomic_dec_and_test(&mds->mds_real_clients)) {
+                /* time to drop LMV connections */
+                CDEBUG(D_OTHER, "%s: last real client %s disconnected.  "
+                       "Disconnnect from LMV now\n",
+                       obd->obd_name, exp->exp_client_uuid.uuid);
+                mds_lmv_disconnect(obd, flags);
+        }
+
+        spin_lock_irqsave(&exp->exp_lock, irqflags);
+        exp->exp_flags = flags;
+        spin_unlock_irqrestore(&exp->exp_lock, irqflags);
+
+        /* Disconnect early so that clients can't keep using export */
+        rc = class_disconnect(exp, flags);
+        ldlm_cancel_locks_for_export(exp);
+
+        /* complete all outstanding replies */
+        spin_lock_irqsave(&exp->exp_lock, irqflags);
+        while (!list_empty(&exp->exp_outstanding_replies)) {
+                struct ptlrpc_reply_state *rs =
+                        list_entry(exp->exp_outstanding_replies.next,
+                                   struct ptlrpc_reply_state, rs_exp_list);
+                struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
+
+                spin_lock(&svc->srv_lock);
+                list_del_init(&rs->rs_exp_list);
+                ptlrpc_schedule_difficult_reply(rs);
+                spin_unlock(&svc->srv_lock);
+        }
+        spin_unlock_irqrestore(&exp->exp_lock, irqflags);
+
+        class_export_put(exp);
+        RETURN(rc);
 }
 
 static int mds_getstatus(struct ptlrpc_request *req)
@@ -466,21 +507,14 @@ static int mds_getstatus(struct ptlrpc_request *req)
         int rc, size = sizeof(*body);
         ENTRY;
 
-        rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
+        rc = lustre_pack_reply(req, 1, &size, NULL);
         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
                 CERROR("mds: out of memory for message: size=%d\n", size);
-                req->rq_status = -ENOMEM;
-                RETURN(0);
+                req->rq_status = -ENOMEM;       /* superfluous? */
+                RETURN(-ENOMEM);
         }
 
-        /* Flush any outstanding transactions to disk so the client will
-         * get the latest last_committed value and can drop their local
-         * requests if they have any.  This would be fsync_super() if it
-         * was exported.
-         */
-        mds_fsync_super(mds->mds_sb);
-
-        body = lustre_msg_buf(req->rq_repmsg, 0);
+        body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
         memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
 
         /* the last_committed and last_xid fields are filled in for all
@@ -489,56 +523,8 @@ static int mds_getstatus(struct ptlrpc_request *req)
         RETURN(0);
 }
 
-static int mds_getlovinfo(struct ptlrpc_request *req)
-{
-        struct mds_obd *mds = mds_req2mds(req);
-        struct mds_status_req *streq;
-        struct lov_desc *desc;
-        int tgt_count;
-        int rc, size[2] = {sizeof(*desc)};
-        ENTRY;
-
-        streq = lustre_msg_buf(req->rq_reqmsg, 0);
-        streq->flags = NTOH__u32(streq->flags);
-        streq->repbuf = NTOH__u32(streq->repbuf);
-        size[1] = streq->repbuf;
-
-        rc = lustre_pack_msg(2, size, NULL, &req->rq_replen, &req->rq_repmsg);
-        if (rc) {
-                CERROR("mds: out of memory for message: size=%d\n", size[1]);
-                req->rq_status = -ENOMEM;
-                RETURN(0);
-        }
-
-        desc = lustre_msg_buf(req->rq_repmsg, 0);
-        rc = mds_get_lovdesc(mds, desc);
-        if (rc) {
-                CERROR("mds_get_lovdesc error %d", rc);
-                req->rq_status = rc;
-                RETURN(0);
-        }
-
-        tgt_count = le32_to_cpu(desc->ld_tgt_count);
-        if (tgt_count * sizeof(obd_uuid_t) > streq->repbuf) {
-                CERROR("too many targets, enlarge client buffers\n");
-                req->rq_status = -ENOSPC;
-                RETURN(0);
-        }
-
-        /* XXX the MDS should not really know about this */
-        mds->mds_max_mdsize = lov_mds_md_size(tgt_count);
-        rc = mds_get_lovtgts(mds, tgt_count,
-                             lustre_msg_buf(req->rq_repmsg, 1));
-        if (rc) {
-                CERROR("get_lovtgts error %d\n", rc);
-                req->rq_status = rc;
-                RETURN(0);
-        }
-        RETURN(0);
-}
-
 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
-                     void *data, __u32 data_len, int flag)
+                     void *data, int flag)
 {
         int do_ast;
         ENTRY;
@@ -550,6 +536,16 @@ int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
 
         /* XXX layering violation!  -phil */
         l_lock(&lock->l_resource->lr_namespace->ns_lock);
+        /* Get this: if mds_blocking_ast is racing with mds_intent_policy,
+         * such that mds_blocking_ast is called just before l_i_p takes the
+         * ns_lock, then by the time we get the lock, we might not be the
+         * correct blocking function anymore.  So check, and return early, if
+         * so. */
+        if (lock->l_blocking_ast != mds_blocking_ast) {
+                l_unlock(&lock->l_resource->lr_namespace->ns_lock);
+                RETURN(0);
+        }
+
         lock->l_flags |= LDLM_FL_CBPENDING;
         do_ast = (!lock->l_readers && !lock->l_writers);
         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
@@ -563,51 +559,91 @@ int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
                 rc = ldlm_cli_cancel(&lockh);
                 if (rc < 0)
                         CERROR("ldlm_cli_cancel: %d\n", rc);
-        } else
-                LDLM_DEBUG(lock, "Lock still has references, will be"
+        } else {
+                LDLM_DEBUG(lock, "Lock still has references, will be "
                            "cancelled later");
+        }
         RETURN(0);
 }
 
-int mds_pack_md(struct mds_obd *mds, struct ptlrpc_request *req,
-                int offset, struct mds_body *body, struct inode *inode)
+int mds_get_md(struct obd_device *obd, struct inode *inode, void *md,
+               int *size, int lock)
+{
+        int rc = 0;
+        int lmm_size;
+
+        if (lock)
+                down(&inode->i_sem);
+        rc = fsfilt_get_md(obd, inode, md, *size);
+        if (lock)
+                up(&inode->i_sem);
+
+        if (rc < 0) {
+                CERROR("Error %d reading eadata for ino %lu\n",
+                       rc, inode->i_ino);
+        } else if (rc > 0) {
+                lmm_size = rc;
+                
+                if (S_ISREG(inode->i_mode))
+                        rc = mds_convert_lov_ea(obd, inode, md, lmm_size);
+
+                if (rc == 0) {
+                        *size = lmm_size;
+                        rc = lmm_size;
+                } else if (rc > 0) {
+                        *size = rc;
+                }
+        }
+
+        RETURN (rc);
+}
+
+
+/* Call with lock=1 if you want mds_pack_md to take the i_sem.
+ * Call with lock=0 if the caller has already taken the i_sem. */
+int mds_pack_md(struct obd_device *obd, struct lustre_msg *msg, int offset,
+                struct mds_body *body, struct inode *inode, int lock)
 {
-        struct lov_mds_md *lmm;
-        int lmm_size = req->rq_repmsg->buflens[offset];
+        struct mds_obd *mds = &obd->u.mds;
+        void *lmm;
+        int lmm_size;
         int rc;
+        ENTRY;
 
-        if (lmm_size == 0) {
-                CDEBUG(D_INFO, "no space reserved for inode %u MD\n", inode->i_ino);
+        lmm = lustre_msg_buf(msg, offset, 0);
+        if (lmm == NULL) {
+                /* Some problem with getting eadata when I sized the reply
+                 * buffer... */
+                CDEBUG(D_INFO, "no space reserved for inode %lu MD\n",
+                       inode->i_ino);
                 RETURN(0);
         }
-
-        lmm = lustre_msg_buf(req->rq_repmsg, offset);
+        lmm_size = msg->buflens[offset];
 
         /* I don't really like this, but it is a sanity check on the client
          * MD request.  However, if the client doesn't know how much space
-         * to reserve for the MD, this shouldn't be fatal either...
+         * to reserve for the MD, it shouldn't be bad to have too much space.
          */
         if (lmm_size > mds->mds_max_mdsize) {
-                CERROR("Reading MD for inode %u of %d bytes > max %d\n",
+                CWARN("Reading MD for inode %lu of %d bytes > max %d\n",
                        inode->i_ino, lmm_size, mds->mds_max_mdsize);
                 // RETURN(-EINVAL);
         }
 
-        /* We don't need to store the reply size, because this buffer is
-         * discarded right after unpacking, and the LOV can figure out the
-         * size itself from the ost count.
-         */
-        if ((rc = mds_fs_get_md(mds, inode, lmm, lmm_size)) < 0) {
-                CDEBUG(D_INFO, "No md for ino %u: rc = %d\n", inode->i_ino, rc);
-        } else if (rc > 0) {
-                body->valid |= OBD_MD_FLEASIZE;
+        rc = mds_get_md(obd, inode, lmm, &lmm_size, lock);
+        if (rc > 0) {
+                if (S_ISDIR(inode->i_mode))
+                        body->valid |= OBD_MD_FLDIREA;
+                else
+                        body->valid |= OBD_MD_FLEASIZE;
+                body->eadatasize = lmm_size;
                 rc = 0;
         }
 
-        return rc;
+        RETURN(rc);
 }
 
-static int mds_getattr_internal(struct mds_obd *mds, struct dentry *dentry,
+static int mds_getattr_internal(struct obd_device *obd, struct dentry *dentry,
                                 struct ptlrpc_request *req,
                                 struct mds_body *reqbody, int reply_off)
 {
@@ -616,158 +652,128 @@ static int mds_getattr_internal(struct mds_obd *mds, struct dentry *dentry,
         int rc = 0;
         ENTRY;
 
-        if (inode == NULL)
+        if (inode == NULL && !(dentry->d_flags & DCACHE_CROSS_REF))
                 RETURN(-ENOENT);
 
-        body = lustre_msg_buf(req->rq_repmsg, reply_off);
+        body = lustre_msg_buf(req->rq_repmsg, reply_off, sizeof(*body));
+        LASSERT(body != NULL);                 /* caller prepped reply */
+
+        if (dentry->d_flags & DCACHE_CROSS_REF) {
+                CDEBUG(D_OTHER, "cross reference: %lu/%lu/%lu\n",
+                       (unsigned long) dentry->d_mdsnum,
+                       (unsigned long) dentry->d_inum,
+                       (unsigned long) dentry->d_generation);
+                body->valid |= OBD_MD_FLID | OBD_MD_MDS;
+                body->fid1.id = dentry->d_inum;
+                body->fid1.mds = dentry->d_mdsnum;
+                body->fid1.generation = dentry->d_generation;
+                RETURN(0);
+        }
+        mds_pack_inode2fid(obd, &body->fid1, inode);
+        mds_pack_inode2body(obd, body, inode);
+
+        if ((S_ISREG(inode->i_mode) && (reqbody->valid & OBD_MD_FLEASIZE)) ||
+            (S_ISDIR(inode->i_mode) && (reqbody->valid & OBD_MD_FLDIREA))) {
+                rc = mds_pack_md(obd, req->rq_repmsg, reply_off + 1, body,
+                                 inode, 1);
 
-        mds_pack_inode2fid(&body->fid1, inode);
-        mds_pack_inode2body(body, inode);
+                /* If we have LOV EA data, the OST holds size, atime, mtime */
+                if (!(body->valid & OBD_MD_FLEASIZE) &&
+                    !(body->valid & OBD_MD_FLDIREA))
+                        body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
+                                        OBD_MD_FLATIME | OBD_MD_FLMTIME);
+        } else if (S_ISLNK(inode->i_mode) &&
+                   (reqbody->valid & OBD_MD_LINKNAME) != 0) {
+                char *symname = lustre_msg_buf(req->rq_repmsg, reply_off + 1,0);
+                int len;
 
-        if (S_ISREG(inode->i_mode)) {
-                rc = mds_pack_md(mds, req, reply_off + 1, body, inode);
-        } else if (S_ISLNK(inode->i_mode) && reqbody->valid & OBD_MD_LINKNAME) {
-                char *symname = lustre_msg_buf(req->rq_repmsg, reply_off + 1);
-                int len = req->rq_repmsg->buflens[reply_off + 1];
+                LASSERT (symname != NULL);       /* caller prepped reply */
+                len = req->rq_repmsg->buflens[reply_off + 1];
 
                 rc = inode->i_op->readlink(dentry, symname, len);
                 if (rc < 0) {
                         CERROR("readlink failed: %d\n", rc);
+                } else if (rc != len - 1) {
+                        CERROR ("Unexpected readlink rc %d: expecting %d\n",
+                                rc, len - 1);
+                        rc = -EINVAL;
                 } else {
                         CDEBUG(D_INODE, "read symlink dest %s\n", symname);
                         body->valid |= OBD_MD_LINKNAME;
+                        body->eadatasize = rc + 1;
+                        symname[rc] = 0;        /* NULL terminate */
+                        rc = 0;
                 }
         }
+
         RETURN(rc);
 }
 
-static int mds_getattr_name(int offset, struct ptlrpc_request *req)
+static int mds_getattr_pack_msg_cf(struct ptlrpc_request *req,
+                                        struct dentry *dentry,
+                                        int offset)
 {
-        struct mds_obd *mds = mds_req2mds(req);
-        struct obd_device *obd = req->rq_export->exp_obd;
-        struct obd_run_ctxt saved;
-        struct mds_body *body;
-        struct dentry *de = NULL, *dchild = NULL;
-        struct inode *dir;
-        struct lustre_handle lockh;
-        char *name;
-        int namelen, flags = 0, lock_mode, rc = 0;
-        struct obd_ucred uc;
-        __u64 res_id[3] = {0, 0, 0};
+        int rc = 0, size[1] = {sizeof(struct mds_body)};
         ENTRY;
 
-        LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name, "mds"));
-
-        if (req->rq_reqmsg->bufcount <= offset + 1) {
-                LBUG();
-                GOTO(out_pre_de, rc = -EINVAL);
-        }
-
-        body = lustre_msg_buf(req->rq_reqmsg, offset);
-        name = lustre_msg_buf(req->rq_reqmsg, offset + 1);
-        namelen = req->rq_reqmsg->buflens[offset + 1];
-        /* requests were at offset 2, replies go back at 1 */
-        if (offset)
-                offset = 1;
-
-        uc.ouc_fsuid = body->fsuid;
-        uc.ouc_fsgid = body->fsgid;
-        uc.ouc_cap = body->capability;
-        push_ctxt(&saved, &mds->mds_ctxt, &uc);
-        de = mds_fid2dentry(mds, &body->fid1, NULL);
-        if (IS_ERR(de)) {
-                GOTO(out_pre_de, rc = -ENOENT);
-        }
-
-        dir = de->d_inode;
-        CDEBUG(D_INODE, "parent ino %ld, name %*s\n", dir->i_ino,namelen,name);
-
-        lock_mode = LCK_PR;
-        res_id[0] = dir->i_ino;
-        res_id[1] = dir->i_generation;
-
-        rc = ldlm_lock_match(obd->obd_namespace, res_id, LDLM_PLAIN,
-                             NULL, 0, lock_mode, &lockh);
-        if (rc == 0) {
-                LDLM_DEBUG_NOLOCK("enqueue res "LPU64, res_id[0]);
-                rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
-                                      res_id, LDLM_PLAIN, NULL, 0, lock_mode,
-                                      &flags, ldlm_completion_ast,
-                                      mds_blocking_ast, NULL, 0, &lockh);
-                if (rc != ELDLM_OK) {
-                        CERROR("lock enqueue: err: %d\n", rc);
-                        GOTO(out_create_de, rc = -EIO);
-                }
+        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
+                CERROR("failed MDS_GETATTR_PACK test\n");
+                req->rq_status = -ENOMEM;
+                GOTO(out, rc = -ENOMEM);
         }
-        ldlm_lock_dump((void *)(unsigned long)lockh.addr);
 
-        down(&dir->i_sem);
-        dchild = lookup_one_len(name, de, namelen - 1);
-        if (IS_ERR(dchild)) {
-                CDEBUG(D_INODE, "child lookup error %ld\n", PTR_ERR(dchild));
-                up(&dir->i_sem);
-                GOTO(out_create_dchild, rc = PTR_ERR(dchild));
+        rc = lustre_pack_reply(req, 1, size, NULL);
+        if (rc) {
+                CERROR("out of memory\n");
+                GOTO(out, req->rq_status = rc);
         }
 
-        rc = mds_getattr_internal(mds, dchild, req, body, offset);
-
         EXIT;
-out_create_dchild:
-        l_dput(dchild);
-        up(&dir->i_sem);
-        ldlm_lock_decref(&lockh, lock_mode);
-out_create_de:
-        l_dput(de);
-out_pre_de:
-        req->rq_status = rc;
-        pop_ctxt(&saved);
-        return 0;
+ out:
+        return(rc);
 }
 
-static int mds_getattr(int offset, struct ptlrpc_request *req)
+static int mds_getattr_pack_msg(struct ptlrpc_request *req, struct inode *inode,
+                                int offset)
 {
         struct mds_obd *mds = mds_req2mds(req);
-        struct obd_run_ctxt saved;
-        struct dentry *de;
-        struct inode *inode;
         struct mds_body *body;
-        struct obd_ucred uc;
         int rc = 0, size[2] = {sizeof(*body)}, bufcount = 1;
         ENTRY;
 
-        body = lustre_msg_buf(req->rq_reqmsg, offset);
-        uc.ouc_fsuid = body->fsuid;
-        uc.ouc_fsgid = body->fsgid;
-        uc.ouc_cap = body->capability;
-        push_ctxt(&saved, &mds->mds_ctxt, &uc);
-        de = mds_fid2dentry(mds, &body->fid1, NULL);
-        if (IS_ERR(de)) {
-                rc = req->rq_status = -ENOENT;
-                GOTO(out_pop, PTR_ERR(de));
-        }
+        body = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*body));
+        LASSERT(body != NULL);                 /* checked by caller */
+        LASSERT_REQSWABBED(req, offset);       /* swabbed by caller */
 
-        inode = de->d_inode;
-        if (S_ISREG(body->fid1.f_type)) {
-                int rc = mds_fs_get_md(mds, inode, NULL, 0);
-                CDEBUG(D_INODE, "got %d bytes MD data for inode %u\n",
+        if ((S_ISREG(inode->i_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
+            (S_ISDIR(inode->i_mode) && (body->valid & OBD_MD_FLDIREA))) {
+                int rc;
+                down(&inode->i_sem);
+                rc = fsfilt_get_md(req->rq_export->exp_obd, inode, NULL, 0);
+                up(&inode->i_sem);
+                CDEBUG(D_INODE, "got %d bytes MD data for inode %lu\n",
                        rc, inode->i_ino);
                 if (rc < 0) {
                         if (rc != -ENODATA)
-                                CERROR("error getting inode %u MD: rc = %d\n",
+                                CERROR("error getting inode %lu MD: rc = %d\n",
                                        inode->i_ino, rc);
                         size[bufcount] = 0;
                 } else if (rc > mds->mds_max_mdsize) {
                         size[bufcount] = 0;
                         CERROR("MD size %d larger than maximum possible %u\n",
                                rc, mds->mds_max_mdsize);
-                } else
+                } else {
                         size[bufcount] = rc;
+                }
                 bufcount++;
-        } else if (body->valid & OBD_MD_LINKNAME) {
-                size[bufcount] = MIN(inode->i_size + 1, body->size);
+        } else if (S_ISLNK(inode->i_mode) && (body->valid & OBD_MD_LINKNAME)) {
+                if (inode->i_size + 1 != body->eadatasize)
+                        CERROR("symlink size: %Lu, reply space: %d\n",
+                               inode->i_size + 1, body->eadatasize);
+                size[bufcount] = min_t(int, inode->i_size+1, body->eadatasize);
                 bufcount++;
-                CDEBUG(D_INODE, "symlink size: %d, reply space: %d\n",
-                       inode->i_size + 1, body->size);
+                CDEBUG(D_INODE, "symlink size: %Lu, reply space: %d\n",
+                       inode->i_size + 1, body->eadatasize);
         }
 
         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
@@ -776,493 +782,843 @@ static int mds_getattr(int offset, struct ptlrpc_request *req)
                 GOTO(out, rc = -ENOMEM);
         }
 
-        rc = lustre_pack_msg(bufcount, size, NULL, &req->rq_replen,
-                             &req->rq_repmsg);
+        rc = lustre_pack_reply(req, bufcount, size, NULL);
         if (rc) {
-                CERROR("out of memoryK\n");
-                req->rq_status = rc;
-                GOTO(out, rc);
+                CERROR("out of memory\n");
+                GOTO(out, req->rq_status = rc);
         }
 
-        req->rq_status = mds_getattr_internal(mds, de, req, body, 0);
+        EXIT;
+ out:
+        return(rc);
+}
 
-out:
-        l_dput(de);
-out_pop:
-        pop_ctxt(&saved);
+int mds_check_mds_num(struct obd_device *obd, struct inode* inode,
+                      char *name, int namelen)
+{
+        struct mea *mea = NULL;
+        int mea_size, rc = 0;
+        ENTRY;
+                                                                                                                                                                                                     
+        rc = mds_get_lmv_attr(obd, inode, &mea, &mea_size);
+        if (rc)
+                RETURN(rc);
+        if (mea != NULL) {
+                /* dir is already splitted, check is requested filename
+                 * should live at this MDS or at another one */
+                int i;
+                i = mea_name2idx(mea, name, namelen - 1);
+                if (mea->mea_master != i) {
+                        CERROR("inapropriate MDS(%d) for %s. should be %d\n",
+                                mea->mea_master, name, i);
+                        rc = -ERESTART;
+                }
+        }
+                                                                                                                                                                                                     
+        if (mea)
+                OBD_FREE(mea, mea_size);
         RETURN(rc);
 }
 
-static int mds_statfs(struct ptlrpc_request *req)
+static int mds_getattr_name(int offset, struct ptlrpc_request *req,
+                            struct lustre_handle *child_lockh, int child_part)
 {
-        struct mds_obd *mds = mds_req2mds(req);
-        struct obd_statfs *osfs;
-        struct statfs sfs;
-        int rc, size = sizeof(*osfs);
+        struct obd_device *obd = req->rq_export->exp_obd;
+        struct ldlm_reply *rep = NULL;
+        struct lvfs_run_ctxt saved;
+        struct mds_body *body;
+        struct dentry *dparent = NULL, *dchild = NULL;
+        struct lvfs_ucred uc;
+        struct lustre_handle parent_lockh[2];
+        int namesize, update_mode;
+        int rc = 0, cleanup_phase = 0, resent_req = 0;
+        char *name;
         ENTRY;
 
-        rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
-        if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
-                CERROR("mds: statfs lustre_pack_msg failed: rc = %d\n", rc);
-                GOTO(out, rc);
+        LASSERT(!strcmp(obd->obd_type->typ_name, LUSTRE_MDS_NAME));
+
+        /* Swab now, before anyone looks inside the request */
+
+        body = lustre_swab_reqbuf(req, offset, sizeof(*body),
+                                  lustre_swab_mds_body);
+        if (body == NULL) {
+                CERROR("Can't swab mds_body\n");
+                GOTO(cleanup, rc = -EFAULT);
         }
 
-        rc = mds_fs_statfs(mds, &sfs);
-        if (rc) {
-                CERROR("mds: statfs failed: rc %d\n", rc);
-                GOTO(out, rc);
+        LASSERT_REQSWAB(req, offset + 1);
+        name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
+        if (name == NULL) {
+                CERROR("Can't unpack name\n");
+                GOTO(cleanup, rc = -EFAULT);
         }
-        osfs = lustre_msg_buf(req->rq_repmsg, 0);
-        memset(osfs, 0, size);
-        statfs_pack(osfs, &sfs);
-        obd_statfs_pack(osfs, osfs);
-
-out:
-        req->rq_status = rc;
-        RETURN(0);
-}
+        namesize = req->rq_reqmsg->buflens[offset + 1];
 
-static struct mds_file_data *mds_handle2mfd(struct lustre_handle *handle)
-{
-        struct mds_file_data *mfd = NULL;
+        LASSERT (offset == 0 || offset == 2);
+        /* if requests were at offset 2, the getattr reply goes back at 1 */
+        if (offset) {
+                rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
+                offset = 1;
+        }
 
-        if (!handle || !handle->addr)
-                RETURN(NULL);
+        uc.luc_fsuid = body->fsuid;
+        uc.luc_fsgid = body->fsgid;
+        uc.luc_cap = body->capability;
+        uc.luc_suppgid1 = body->suppgid;
+        uc.luc_suppgid2 = -1;
+        push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
+        cleanup_phase = 1; /* kernel context */
+        intent_set_disposition(rep, DISP_LOOKUP_EXECD);
+
+        LASSERT(namesize > 0);
+        if (namesize == 1) {
+                /* we have no dentry here, drop LOOKUP bit */
+                child_part &= ~MDS_INODELOCK_LOOKUP;
+                CDEBUG(D_OTHER, "%s: request to retrieve attrs for %lu/%lu\n",
+                       obd->obd_name, (unsigned long) body->fid1.id,
+                       (unsigned long) body->fid1.generation);
+                dchild = mds_fid2locked_dentry(obd, &body->fid1, NULL, LCK_PR,
+                                               parent_lockh, &update_mode, 
+                                               NULL, 0, child_part);
+                if (IS_ERR(dchild)) {
+                        CERROR("can't find inode: %d\n", (int) PTR_ERR(dchild));
+                        GOTO(cleanup, rc = PTR_ERR(dchild));
+                }
+                memcpy(child_lockh, parent_lockh, sizeof(parent_lockh[0]));
+#ifdef S_PDIROPS
+                if (parent_lockh[1].cookie)
+                        ldlm_lock_decref(parent_lockh + 1, update_mode);
+#endif
+                cleanup_phase = 2;
+                goto fill_inode;
+        }
+        
+        /* FIXME: handle raw lookup */
+#if 0
+        if (body->valid == OBD_MD_FLID) {
+                struct mds_body *mds_reply;
+                int size = sizeof(*mds_reply);
+                ino_t inum;
+                // The user requested ONLY the inode number, so do a raw lookup
+                rc = lustre_pack_reply(req, 1, &size, NULL);
+                if (rc) {
+                        CERROR("out of memory\n");
+                        GOTO(cleanup, rc);
+                }
 
-        mfd = (struct mds_file_data *)(unsigned long)(handle->addr);
-        if (!kmem_cache_validate(mds_file_cache, mfd))
-                RETURN(NULL);
+                rc = dir->i_op->lookup_raw(dir, name, namesize - 1, &inum);
 
-        if (mfd->mfd_servercookie != handle->cookie)
-                RETURN(NULL);
+                mds_reply = lustre_msg_buf(req->rq_repmsg, offset,
+                                           sizeof(*mds_reply));
+                mds_reply->fid1.id = inum;
+                mds_reply->valid = OBD_MD_FLID;
+                GOTO(cleanup, rc);
+        }
+#endif
 
-        return mfd;
-}
+        if (child_lockh->cookie != 0) {
+                LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
+                resent_req = 1;
+        }
 
-static int mds_store_md(struct mds_obd *mds, struct ptlrpc_request *req,
-                        int offset, struct mds_body *body, struct inode *inode)
-{
-        struct lov_mds_md *lmm = lustre_msg_buf(req->rq_reqmsg, offset);
-        int lmm_size = req->rq_reqmsg->buflens[offset];
-        struct obd_run_ctxt saved;
-        struct obd_ucred uc;
-        void *handle;
-        int rc, rc2;
-        ENTRY;
+        if (resent_req == 0) {
+                rc = mds_get_parent_child_locked(obd, &obd->u.mds, &body->fid1,
+                                                 parent_lockh, &dparent,
+                                                 LCK_PR, MDS_INODELOCK_LOOKUP,
+                                                 &update_mode, name, namesize,
+                                                 child_lockh, &dchild, LCK_PR,
+                                                 child_part);
+                if (rc)
+                        GOTO(cleanup, rc);
+        } else {
+                struct ldlm_lock *granted_lock;
+                struct ll_fid child_fid;
+                struct ldlm_resource *res;
+                DEBUG_REQ(D_DLMTRACE, req, "resent, not enqueuing new locks");
+                granted_lock = ldlm_handle2lock(child_lockh);
+                LASSERT(granted_lock);
 
-        /* I don't really like this, but it is a sanity check on the client
-         * MD request.
-         */
-        if (lmm_size > mds->mds_max_mdsize) {
-                CERROR("Saving MD for inode %u of %d bytes > max %d\n",
-                       inode->i_ino, lmm_size, mds->mds_max_mdsize);
-                //RETURN(-EINVAL);
+                res = granted_lock->l_resource;
+                child_fid.id = res->lr_name.name[0];
+                child_fid.generation = res->lr_name.name[1];
+                dchild = mds_fid2dentry(&obd->u.mds, &child_fid, NULL);
+                LASSERT(dchild);
+                LDLM_LOCK_PUT(granted_lock);
         }
 
-        CDEBUG(D_INODE, "storing %d bytes MD for inode %u\n",
-               lmm_size, inode->i_ino);
-        uc.ouc_fsuid = body->fsuid;
-        uc.ouc_fsgid = body->fsgid;
-        uc.ouc_cap = body->capability;
-        push_ctxt(&saved, &mds->mds_ctxt, &uc);
-        mds_start_transno(mds);
-        handle = mds_fs_start(mds, inode, MDS_FSOP_SETATTR);
-        if (IS_ERR(handle)) {
-                rc = PTR_ERR(handle);
-                mds_finish_transno(mds, handle, req, rc);
-                GOTO(out_ea, rc);
-        }
+        cleanup_phase = 2; /* dchild, dparent, locks */
 
-        rc = mds_fs_set_md(mds, inode, handle, lmm, lmm_size);
-        rc = mds_finish_transno(mds, handle, req, rc);
+        /* let's make sure this name should leave on this mds node */
+        rc = mds_check_mds_num(obd, dparent->d_inode, name, namesize);
+        if (rc)
+                GOTO(cleanup, rc);
 
-        rc2 = mds_fs_commit(mds, inode, handle);
-        if (rc2 && !rc)
-                rc = rc2;
-out_ea:
-        pop_ctxt(&saved);
+fill_inode:
 
-        RETURN(rc);
+        if (!DENTRY_VALID(dchild)) {
+                intent_set_disposition(rep, DISP_LOOKUP_NEG);
+                /* in the intent case, the policy clears this error:
+                   the disposition is enough */
+                rc = -ENOENT;
+                GOTO(cleanup, rc);
+        } else {
+                intent_set_disposition(rep, DISP_LOOKUP_POS);
+        }
+
+        if (req->rq_repmsg == NULL) {
+                if (dchild->d_flags & DCACHE_CROSS_REF)
+                        rc = mds_getattr_pack_msg_cf(req, dchild, offset);
+                else
+                        rc = mds_getattr_pack_msg(req, dchild->d_inode, offset);
+                if (rc != 0) {
+                        CERROR ("mds_getattr_pack_msg: %d\n", rc);
+                        GOTO (cleanup, rc);
+                }
+        }
+
+        rc = mds_getattr_internal(obd, dchild, req, body, offset);
+        GOTO(cleanup, rc); /* returns the lock to the client */
+
+ cleanup:
+        switch (cleanup_phase) {
+        case 2:
+                if (resent_req == 0) {
+                        if (rc && DENTRY_VALID(dchild))
+                                ldlm_lock_decref(child_lockh, LCK_PR);
+                        if (dparent) {
+                                ldlm_lock_decref(parent_lockh, LCK_PR);
+#ifdef S_PDIROPS
+                                if (parent_lockh[1].cookie != 0)
+                                        ldlm_lock_decref(parent_lockh + 1,
+                                                         update_mode);
+#endif
+                        }
+                        if (dparent)
+                                l_dput(dparent);
+                }
+                l_dput(dchild);
+        case 1:
+                pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
+        default: ;
+        }
+        return rc;
 }
 
-static int mds_open(struct ptlrpc_request *req)
+static int mds_getattr(int offset, struct ptlrpc_request *req)
 {
         struct mds_obd *mds = mds_req2mds(req);
-        struct mds_body *body;
-        struct mds_export_data *med;
-        struct mds_file_data *mfd;
+        struct obd_device *obd = req->rq_export->exp_obd;
+        struct lvfs_run_ctxt saved;
         struct dentry *de;
-        struct file *file;
-        struct vfsmount *mnt;
-        __u32 flags;
-        struct list_head *tmp;
-        int rc, size = sizeof(*body);
+        struct mds_body *body;
+        struct lvfs_ucred uc;
+        int rc = 0;
         ENTRY;
 
-        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
-                CERROR("test case OBD_FAIL_MDS_OPEN_PACK\n");
-                req->rq_status = -ENOMEM;
-                RETURN(-ENOMEM);
+        body = lustre_swab_reqbuf (req, offset, sizeof (*body),
+                                   lustre_swab_mds_body);
+        if (body == NULL) {
+                CERROR ("Can't unpack body\n");
+                RETURN (-EFAULT);
         }
 
-        rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
-        if (rc) {
-                CERROR("mds: pack error: rc = %d\n", rc);
-                req->rq_status = rc;
-                RETURN(rc);
+        uc.luc_fsuid = body->fsuid;
+        uc.luc_fsgid = body->fsgid;
+        uc.luc_cap = body->capability;
+        push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
+        de = mds_fid2dentry(mds, &body->fid1, NULL);
+        if (IS_ERR(de)) {
+                rc = req->rq_status = -ENOENT;
+                GOTO(out_pop, PTR_ERR(de));
         }
 
-        body = lustre_msg_buf(req->rq_reqmsg, 0);
-
-        /* was this animal open already and the client lost the reply? */
-        /* XXX need some way to detect a reopen, to avoid locked list walks */
-        med = &req->rq_export->exp_mds_data;
-        spin_lock(&med->med_open_lock);
-        list_for_each(tmp, &med->med_open_head) {
-                mfd = list_entry(tmp, typeof(*mfd), mfd_list);
-                if (!memcmp(&mfd->mfd_clienthandle, &body->handle,
-                            sizeof(mfd->mfd_clienthandle)) &&
-                    body->fid1.id == mfd->mfd_file->f_dentry->d_inode->i_ino) {
-                        de = mfd->mfd_file->f_dentry;
-                        spin_unlock(&med->med_open_lock);
-                        CERROR("Re opening "LPD64"\n", body->fid1.id);
-                        GOTO(out_pack, rc = 0);
-                }
+        rc = mds_getattr_pack_msg(req, de->d_inode, offset);
+        if (rc != 0) {
+                CERROR ("mds_getattr_pack_msg: %d\n", rc);
+                GOTO (out_pop, rc);
         }
-        spin_unlock(&med->med_open_lock);
 
-        mfd = kmem_cache_alloc(mds_file_cache, GFP_KERNEL);
-        if (!mfd) {
-                CERROR("mds: out of memory\n");
-                req->rq_status = -ENOMEM;
-                RETURN(0);
-        }
+        req->rq_status = mds_getattr_internal(obd, de, req, body, 0);
 
-        de = mds_fid2dentry(mds, &body->fid1, &mnt);
-        if (IS_ERR(de))
-                GOTO(out_free, rc = PTR_ERR(de));
+        l_dput(de);
+        GOTO(out_pop, rc);
+out_pop:
+        pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
+        return rc;
+}
 
-        /* check if this inode has seen a delayed object creation */
-        if (lustre_msg_get_op_flags(req->rq_reqmsg) & MDS_OPEN_HAS_EA) {
-                rc = mds_store_md(mds, req, 1, body, de->d_inode);
-                if (rc) {
-                        l_dput(de);
-                        mntput(mnt);
-                        GOTO(out_free, rc);
-                }
-        }
 
-        flags = body->flags;
-        /* dentry_open does a dput(de) and mntput(mnt) on error */
-        file = dentry_open(de, mnt, flags & ~O_DIRECT);
-        if (IS_ERR(file)) {
-                rc = PTR_ERR(file);
-                GOTO(out_free, 0);
-        }
+static int mds_obd_statfs(struct obd_device *obd, struct obd_statfs *osfs,
+                          unsigned long max_age)
+{
+        int rc;
 
-        file->private_data = mfd;
-        mfd->mfd_file = file;
-        memcpy(&mfd->mfd_clienthandle, &body->handle, sizeof(body->handle));
-        get_random_bytes(&mfd->mfd_servercookie, sizeof(mfd->mfd_servercookie));
-        spin_lock(&med->med_open_lock);
-        list_add(&mfd->mfd_list, &med->med_open_head);
-        spin_unlock(&med->med_open_lock);
+        spin_lock(&obd->obd_osfs_lock);
+        rc = fsfilt_statfs(obd, obd->u.mds.mds_sb, max_age);
+        if (rc == 0)
+                memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
+        spin_unlock(&obd->obd_osfs_lock);
 
-out_pack:
-        body = lustre_msg_buf(req->rq_repmsg, 0);
-        mds_pack_inode2fid(&body->fid1, de->d_inode);
-        mds_pack_inode2body(body, de->d_inode);
-        body->handle.addr = (__u64)(unsigned long)mfd;
-        body->handle.cookie = mfd->mfd_servercookie;
-        CDEBUG(D_INODE, "llite file "LPX64": addr %p, cookie "LPX64"\n",
-               mfd->mfd_clienthandle.addr, mfd, mfd->mfd_servercookie);
-        RETURN(0);
+        return rc;
+}
+
+static int mds_statfs(struct ptlrpc_request *req)
+{
+        struct obd_device *obd = req->rq_export->exp_obd;
+        int rc, size = sizeof(struct obd_statfs);
+        ENTRY;
+
+        rc = lustre_pack_reply(req, 1, &size, NULL);
+        if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
+                CERROR("mds: statfs lustre_pack_reply failed: rc = %d\n", rc);
+                GOTO(out, rc);
+        }
+
+        /* We call this so that we can cache a bit - 1 jiffie worth */
+        rc = mds_obd_statfs(obd, lustre_msg_buf(req->rq_repmsg, 0, size),
+                            jiffies - HZ);
+        if (rc) {
+                CERROR("mds_obd_statfs failed: rc %d\n", rc);
+                GOTO(out, rc);
+        }
 
-out_free:
-        mfd->mfd_servercookie = DEAD_HANDLE_MAGIC;
-        kmem_cache_free(mds_file_cache, mfd);
+        EXIT;
+out:
         req->rq_status = rc;
-        RETURN(0);
+        return 0;
 }
 
-static int mds_close(struct ptlrpc_request *req)
+static int mds_sync(struct ptlrpc_request *req)
 {
-        struct mds_export_data *med = &req->rq_export->exp_mds_data;
+        struct obd_device *obd = req->rq_export->exp_obd;
+        struct mds_obd *mds = &obd->u.mds;
         struct mds_body *body;
-        struct mds_file_data *mfd;
-        int rc;
+        int rc, size = sizeof(*body);
         ENTRY;
 
-        body = lustre_msg_buf(req->rq_reqmsg, 0);
+        body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
+        if (body == NULL)
+                GOTO(out, rc = -EPROTO);
 
-        mfd = mds_handle2mfd(&body->handle);
-        if (!mfd) {
-                CERROR("no handle for file close "LPD64
-                       ": addr "LPX64", cookie "LPX64"\n",
-                       body->fid1.id, body->handle.addr, body->handle.cookie);
-                RETURN(-ESTALE);
+        rc = lustre_pack_reply(req, 1, &size, NULL);
+        if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK)) {
+                CERROR("fsync lustre_pack_reply failed: rc = %d\n", rc);
+                GOTO(out, rc);
         }
 
-        spin_lock(&med->med_open_lock);
-        req->rq_status = mds_close_mfd(mfd, med);
-        spin_unlock(&med->med_open_lock);
+        if (body->fid1.id == 0) {
+                /* a fid of zero is taken to mean "sync whole filesystem" */
+                rc = fsfilt_sync(obd, mds->mds_sb);
+                if (rc)
+                        GOTO(out, rc);
+        } else {
+                /* just any file to grab fsync method - "file" arg unused */
+                struct file *file = mds->mds_rcvd_filp;
+                struct dentry *de;
 
-        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
-                CERROR("test case OBD_FAIL_MDS_CLOSE_PACK\n");
-                req->rq_status = -ENOMEM;
-                RETURN(-ENOMEM);
-        }
+                de = mds_fid2dentry(mds, &body->fid1, NULL);
+                if (IS_ERR(de))
+                        GOTO(out, rc = PTR_ERR(de));
 
-        rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
-        if (rc) {
-                CERROR("mds: lustre_pack_msg: rc = %d\n", rc);
-                req->rq_status = rc;
-        }
+                rc = file->f_op->fsync(NULL, de, 1);
+                l_dput(de);
+                if (rc)
+                        GOTO(out, rc);
 
-        RETURN(0);
+                body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
+                mds_pack_inode2fid(obd, &body->fid1, de->d_inode);
+                mds_pack_inode2body(obd, body, de->d_inode);
+        }
+out:
+        req->rq_status = rc;
+        return 0;
 }
 
+/* mds_readpage does not take a DLM lock on the inode, because the client must
+ * already have a PR lock.
+ *
+ * If we were to take another one here, a deadlock will result, if another
+ * thread is already waiting for a PW lock. */
 static int mds_readpage(struct ptlrpc_request *req)
 {
-        struct mds_obd *mds = mds_req2mds(req);
+        struct obd_device *obd = req->rq_export->exp_obd;
         struct vfsmount *mnt;
         struct dentry *de;
         struct file *file;
         struct mds_body *body, *repbody;
-        struct obd_run_ctxt saved;
-        int rc, size = sizeof(*body);
-        struct obd_ucred uc;
+        struct lvfs_run_ctxt saved;
+        int rc, size = sizeof(*repbody);
+        struct lvfs_ucred uc;
         ENTRY;
 
-        rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
+        rc = lustre_pack_reply(req, 1, &size, NULL);
         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
                 CERROR("mds: out of memory\n");
                 GOTO(out, rc = -ENOMEM);
         }
 
-        body = lustre_msg_buf(req->rq_reqmsg, 0);
-        uc.ouc_fsuid = body->fsuid;
-        uc.ouc_fsgid = body->fsgid;
-        uc.ouc_cap = body->capability;
-        push_ctxt(&saved, &mds->mds_ctxt, &uc);
-        de = mds_fid2dentry(mds, &body->fid1, &mnt);
+        body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
+        if (body == NULL)
+                GOTO (out, rc = -EFAULT);
+
+        uc.luc_fsuid = body->fsuid;
+        uc.luc_fsgid = body->fsgid;
+        uc.luc_cap = body->capability;
+        push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
+        de = mds_fid2dentry(&obd->u.mds, &body->fid1, &mnt);
         if (IS_ERR(de))
                 GOTO(out_pop, rc = PTR_ERR(de));
 
-        CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
+        CDEBUG(D_INODE, "ino %lu\n", de->d_inode->i_ino);
 
         file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
         /* note: in case of an error, dentry_open puts dentry */
         if (IS_ERR(file))
                 GOTO(out_pop, rc = PTR_ERR(file));
 
-        repbody = lustre_msg_buf(req->rq_repmsg, 0);
+        /* body->size is actually the offset -eeb */
+        if ((body->size & (de->d_inode->i_blksize - 1)) != 0) {
+                CERROR("offset "LPU64" not on a block boundary of %lu\n",
+                       body->size, de->d_inode->i_blksize);
+                GOTO(out_file, rc = -EFAULT);
+        }
+
+        /* body->nlink is actually the #bytes to read -eeb */
+        if (body->nlink & (de->d_inode->i_blksize - 1)) {
+                CERROR("size %u is not multiple of blocksize %lu\n",
+                       body->nlink, de->d_inode->i_blksize);
+                GOTO(out_file, rc = -EFAULT);
+        }
+
+        repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*repbody));
         repbody->size = file->f_dentry->d_inode->i_size;
         repbody->valid = OBD_MD_FLSIZE;
 
         /* to make this asynchronous make sure that the handling function
            doesn't send a reply when this function completes. Instead a
            callback function would send the reply */
-        /* note: in case of an error, dentry_open puts dentry */
-        rc = mds_sendpage(req, file, body->size);
+        /* body->size is actually the offset -eeb */
+        rc = mds_sendpage(req, file, body->size, body->nlink);
 
+out_file:
         filp_close(file, 0);
 out_pop:
-        pop_ctxt(&saved);
+        pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
 out:
         req->rq_status = rc;
         RETURN(0);
 }
 
-int mds_reint(struct ptlrpc_request *req, int offset)
+int mds_reint(struct ptlrpc_request *req, int offset,
+              struct lustre_handle *lockh)
 {
+        struct mds_update_record *rec; /* 116 bytes on the stack?  no sir! */
         int rc;
-        struct mds_update_record rec;
+        ENTRY;
+
+        OBD_ALLOC(rec, sizeof(*rec));
+        if (rec == NULL)
+                RETURN(-ENOMEM);
 
-        rc = mds_update_unpack(req, offset, &rec);
+        rc = mds_update_unpack(req, offset, rec);
         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
                 CERROR("invalid record\n");
-                req->rq_status = -EINVAL;
-                RETURN(0);
+                GOTO(out, req->rq_status = -EINVAL);
         }
         /* rc will be used to interrupt a for loop over multiple records */
-        rc = mds_reint_rec(&rec, offset, req);
-        return rc;
+        rc = mds_reint_rec(rec, offset, req, lockh);
+ out:
+        OBD_FREE(rec, sizeof(*rec));
+        RETURN(rc);
 }
 
-/* forward declaration */
-int mds_handle(struct ptlrpc_request *req);
-
-static int check_for_next_transno(struct mds_obd *mds)
+static int mds_filter_recovery_request(struct ptlrpc_request *req,
+                                       struct obd_device *obd, int *process)
 {
-        struct ptlrpc_request *req;
-        req = list_entry(mds->mds_recovery_queue.next, 
-                         struct ptlrpc_request, rq_list);
-        return req->rq_reqmsg->transno == mds->mds_next_recovery_transno;
+        switch (req->rq_reqmsg->opc) {
+        case MDS_CONNECT: /* This will never get here, but for completeness. */
+        case OST_CONNECT: /* This will never get here, but for completeness. */
+        case MDS_DISCONNECT:
+        case OST_DISCONNECT:
+               *process = 1;
+               RETURN(0);
+
+        case MDS_CLOSE:
+        case MDS_SYNC: /* used in unmounting */
+        case OBD_PING:
+        case MDS_REINT:
+        case LDLM_ENQUEUE:
+        case OST_CREATE:
+                *process = target_queue_recovery_request(req, obd);
+                RETURN(0);
+
+        default:
+                DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
+                *process = 0;
+                /* XXX what should we set rq_status to here? */
+                req->rq_status = -EAGAIN;
+                RETURN(ptlrpc_error(req));
+        }
 }
 
-static void process_recovery_queue(struct mds_obd *mds)
+static char *reint_names[] = {
+        [REINT_SETATTR] "setattr",
+        [REINT_CREATE]  "create",
+        [REINT_LINK]    "link",
+        [REINT_UNLINK]  "unlink",
+        [REINT_RENAME]  "rename",
+        [REINT_OPEN]    "open",
+};
+
+#define FILTER_VALID_FLAGS (OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLGENER  |\
+                            OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ|\
+                            OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME|\
+                            OBD_MD_FLID) 
+
+static void reconstruct_create(struct ptlrpc_request *req)
 {
-        struct ptlrpc_request *req;
-        
-        for (;;) {
-                spin_lock(&mds->mds_processing_task_lock);
-                req = list_entry(mds->mds_recovery_queue.next, 
-                                 struct ptlrpc_request, rq_list);
-
-                if (req->rq_reqmsg->transno != mds->mds_next_recovery_transno) {
-                        spin_unlock(&mds->mds_processing_task_lock);
-                        wait_event(mds->mds_next_transno_waitq,
-                                   check_for_next_transno(mds));
-                        continue;
-                }
-                list_del(&req->rq_list);
-                spin_unlock(&mds->mds_processing_task_lock);
+        struct mds_export_data *med = &req->rq_export->exp_mds_data;
+        struct mds_client_data *mcd = med->med_mcd;
+        struct ost_body *body;
 
-                DEBUG_REQ(D_HA, req, "");
-                mds_handle(req);
-                
-                if (list_empty(&mds->mds_recovery_queue))
-                        break;
-        }
+        body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
+
+        /* copy rc, transno and disp; steal locks */
+        mds_req_from_mcd(req, mcd);
+        CERROR("reconstruct reply for x"LPU64"\n", req->rq_xid);
 }
 
-static int queue_recovery_request(struct ptlrpc_request *req,
-                                  struct mds_obd *mds)
+static int mdt_obj_create(struct ptlrpc_request *req)
 {
-        struct list_head *tmp;
-        int inserted = 0, transno = req->rq_reqmsg->transno;
+        struct obd_device *obd = req->rq_export->exp_obd;
+        struct ldlm_res_id res_id = { .name = {0} };
+        struct mds_obd *mds = &obd->u.mds;
+        struct ost_body *body, *repbody;
+        int rc, size = sizeof(*repbody);
+        char fidname[LL_FID_NAMELEN];
+        struct inode *parent_inode;
+        struct lustre_handle lockh;
+        struct lvfs_run_ctxt saved;
+        ldlm_policy_data_t policy;
+        struct dentry *new = NULL;
+        struct dentry_params dp;
+        int mealen, flags = 0;
+        unsigned int tmpname;
+        struct lvfs_ucred uc;
+        struct mea *mea;
+        void *handle;
+        ENTRY;
+       
+        DEBUG_REQ(D_HA, req, "create remote object");
 
-        if (!transno) {
-                DEBUG_REQ(D_HA, req, "not queueing");
-                return 1;
-        }
+        parent_inode = mds->mds_objects_dir->d_inode;
 
-        spin_lock(&mds->mds_processing_task_lock);
+        body = lustre_swab_reqbuf(req, 0, sizeof(*body),
+                                  lustre_swab_ost_body);
+        if (body == NULL)
+                RETURN(-EFAULT);
 
-        if (mds->mds_processing_task == current->pid) {
-                /* Processing the queue right now, don't re-add. */
-                spin_unlock(&mds->mds_processing_task_lock);
-                return 1;
-        }
+        MDS_CHECK_RESENT(req, reconstruct_create(req));
 
-        /* XXX O(n^2) */
-        list_for_each(tmp, &mds->mds_recovery_queue) {
-                struct ptlrpc_request *reqiter = 
-                        list_entry(tmp, struct ptlrpc_request, rq_list);
-                if (reqiter->rq_reqmsg->transno > transno) {
-                        list_add_tail(&req->rq_list, &reqiter->rq_list);
-                        inserted = 1;
-                        break;
+        uc.luc_fsuid = body->oa.o_uid;
+        uc.luc_fsgid = body->oa.o_gid;
+
+        push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
+        
+        rc = lustre_pack_reply(req, 1, &size, NULL);
+        if (rc)
+                RETURN(rc);
+
+        repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
+
+        if (body->oa.o_flags & OBD_FL_RECREATE_OBJS) {
+                /* this is re-create request from MDS holding directory name.
+                 * we have to lookup given ino/generation first. if it exists
+                 * (good case) then there is nothing to do. if it does not
+                 * then we have to recreate it */
+                struct ll_fid fid;
+                fid.id = body->oa.o_id;
+                fid.generation = body->oa.o_generation;
+                new = mds_fid2dentry(mds, &fid, NULL);
+                if (!IS_ERR(new) && new->d_inode) {
+                        CWARN("mkdir() repairing is on its way: %lu/%lu\n",
+                              (unsigned long) fid.id,
+                              (unsigned long) fid.generation);
+                        obdo_from_inode(&repbody->oa, new->d_inode,
+                                        FILTER_VALID_FLAGS);
+                        repbody->oa.o_id = new->d_inode->i_ino;
+                        repbody->oa.o_generation = new->d_inode->i_generation;
+                        repbody->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
+                        GOTO(cleanup2, rc = 0);
                 }
+                CWARN("hmm. for some reason dir %lu/%lu (or reply) got lost\n",
+                      (unsigned long) fid.id, (unsigned long) fid.generation);
+                LASSERT(new->d_inode == NULL ||
+                        new->d_inode->i_generation != fid.generation);
+                l_dput(new); 
         }
+        
+        down(&parent_inode->i_sem);
+        handle = fsfilt_start(obd, parent_inode, FSFILT_OP_MKDIR, NULL);
+        LASSERT(!IS_ERR(handle));
+
+repeat:
+        tmpname = ll_insecure_random_int();
+        rc = sprintf(fidname, "%u", tmpname);
+        new = lookup_one_len(fidname, mds->mds_objects_dir, rc);
+        if (IS_ERR(new)) {
+                CERROR("%s: can't lookup new inode (%s) for mkdir: %d\n",
+                       obd->obd_name, fidname, (int) PTR_ERR(new));
+                fsfilt_commit(obd, mds->mds_sb, new->d_inode, handle, 0);
+                up(&parent_inode->i_sem);
+                RETURN(PTR_ERR(new));
+        } else if (new->d_inode) {
+                CERROR("%s: name exists. repeat\n", obd->obd_name);
+                goto repeat;
+        }
+
+        new->d_fsdata = (void *) &dp;
+        dp.p_inum = 0;
+        dp.p_ptr = req;
+
+        if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
+                DEBUG_REQ(D_HA, req, "replay create obj %lu/%lu",
+                          (unsigned long) body->oa.o_id,
+                          (unsigned long) body->oa.o_generation);
+                dp.p_inum = body->oa.o_id;
+                dp.p_generation = body->oa.o_generation;
+        }
+        rc = vfs_mkdir(parent_inode, new, body->oa.o_mode);
+        if (rc == 0) {
+                obdo_from_inode(&repbody->oa, new->d_inode, FILTER_VALID_FLAGS);
+                repbody->oa.o_id = new->d_inode->i_ino;
+                repbody->oa.o_generation = new->d_inode->i_generation;
+                repbody->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
 
-        if (!inserted)
-                list_add_tail(&req->rq_list, &mds->mds_recovery_queue);
+                rc = fsfilt_del_dir_entry(obd, new);
+                up(&parent_inode->i_sem);
 
-        if (mds->mds_processing_task != 0) {
-                /* Someone else is processing this queue, we'll leave it to
-                 * them.
-                 */
-                spin_unlock(&mds->mds_processing_task_lock);
-                if (transno == mds->mds_next_recovery_transno)
-                        wake_up(&mds->mds_next_transno_waitq);
-                return 0;
+                if (rc) {
+                        CERROR("can't remove name for object: %d\n", rc);
+                        GOTO(cleanup, rc);
+                }
+                        
+                /* this lock should be taken to serialize MDS modifications
+                 * in failure case */
+                res_id.name[0] = new->d_inode->i_ino;
+                res_id.name[1] = new->d_inode->i_generation;
+                policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
+                rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
+                                res_id, LDLM_IBITS, &policy,
+                                LCK_EX, &flags, mds_blocking_ast,
+                                ldlm_completion_ast, NULL, NULL,
+                                NULL, 0, NULL, &lockh);
+                if (rc != ELDLM_OK)
+                        GOTO(cleanup, rc);
+
+                CDEBUG(D_OTHER, "created dirobj: %lu/%lu mode %o\n",
+                                (unsigned long) new->d_inode->i_ino,
+                                (unsigned long) new->d_inode->i_generation,
+                                (unsigned) new->d_inode->i_mode);
+        } else {
+                up(&parent_inode->i_sem);
+                CERROR("%s: can't create dirobj: %d\n", obd->obd_name, rc);
+        }
+
+        if (rc == 0 && body->oa.o_valid & OBD_MD_FLID) {
+                /* this is new object for splitted dir. we have to
+                 * prevent recursive splitting on it -bzzz */
+                mealen = obd_size_diskmd(mds->mds_lmv_exp, NULL);
+                OBD_ALLOC(mea, mealen);
+                if (mea == NULL)
+                        GOTO(cleanup, rc = -ENOMEM);
+                mea->mea_count = 0;
+                down(&new->d_inode->i_sem);
+                rc = fsfilt_set_md(obd, new->d_inode, handle, mea, mealen);
+                up(&new->d_inode->i_sem);
+                OBD_FREE(mea, mealen);
+        }
+
+cleanup:
+        rc = mds_finish_transno(mds, parent_inode, handle, req, rc, 0);
+        if (rc == 0)
+                ptlrpc_save_lock(req, &lockh, LCK_EX);
+        else
+                ldlm_lock_decref(&lockh, LCK_EX);
+cleanup2:
+        l_dput(new);
+        pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
+        RETURN(rc);
+}
+
+static int mdt_get_info(struct ptlrpc_request *req)
+{
+        char *key;
+        struct obd_export *exp = req->rq_export;
+        int keylen, rc = 0, size = sizeof(obd_id);
+        obd_id *reply;
+        ENTRY;
+
+        key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
+        if (key == NULL) {
+                DEBUG_REQ(D_HA, req, "no get_info key");
+                RETURN(-EFAULT);
         }
+        keylen = req->rq_reqmsg->buflens[0];
 
-        /* Nobody is processing, and we know there's (at least) one to process
-         * now, so we'll do the honours.
-         */
-        mds->mds_processing_task = current->pid;
-        spin_unlock(&mds->mds_processing_task_lock);
+        if (keylen < strlen("mdsize") || memcmp(key, "mdsize", 6) != 0)
+                RETURN(-EPROTO);
 
-        process_recovery_queue(mds);
-        return 0;
+        rc = lustre_pack_reply(req, 1, &size, NULL);
+        if (rc)
+                RETURN(rc);
+
+        reply = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*reply));
+        rc = obd_get_info(exp, keylen, key, &size, reply);
+        req->rq_repmsg->status = 0;
+        RETURN(rc);
 }
 
-static int filter_recovery_request(struct ptlrpc_request *req, 
-                                   struct mds_obd *mds, int *process)
+static int mds_set_info(struct obd_export *exp, __u32 keylen,
+                        void *key, __u32 vallen, void *val)
 {
-        switch (req->rq_reqmsg->opc) {
-        case MDS_CONNECT:
-        case MDS_DISCONNECT:
-        case MDS_OPEN:
-               *process = 1;
-               RETURN(0);
-            
-        case MDS_GETSTATUS: /* used in unmounting */
-        case MDS_REINT:
-        case LDLM_ENQUEUE:
-                *process = queue_recovery_request(req, mds);
+        struct obd_device *obd;
+        struct mds_obd *mds;
+        int    rc = 0;
+        ENTRY;
+
+        obd = class_exp2obd(exp);
+        if (obd == NULL) {
+                CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
+                       exp->exp_handle.h_cookie);
+                RETURN(-EINVAL);
+        }
+
+        mds = &obd->u.mds;
+        if (keylen == strlen("mds_num") &&
+            memcmp(key, "mds_num", keylen) == 0) {
+                int valsize;
+                __u32 group;
+                CDEBUG(D_IOCTL, "set mds num %d\n", *(int*)val);
+                mds->mds_num = *(int*)val;
+                group = FILTER_GROUP_FIRST_MDS + mds->mds_num;
+                valsize = sizeof(group);
+                /*mds number has been changed, so the corresponding obdfilter exp
+                 *need to be changed too*/
+                rc = obd_set_info(mds->mds_osc_exp, strlen("mds_conn"), "mds_conn",
+                          valsize, &group);
+                RETURN(rc);
+        } else if (keylen == strlen("client") &&
+                   memcmp(key, "client", keylen) == 0) {
+                if (!(exp->exp_flags & OBD_OPT_REAL_CLIENT)) {
+                        atomic_inc(&mds->mds_real_clients);
+                        CDEBUG(D_OTHER,"%s: peer from %s is real client (%d)\n",
+                               obd->obd_name, exp->exp_client_uuid.uuid,
+                               atomic_read(&mds->mds_real_clients));
+                        exp->exp_flags |= OBD_OPT_REAL_CLIENT;
+                }
+                if (mds->mds_lmv_name) {
+                        rc = mds_lmv_connect(obd, mds->mds_lmv_name);
+                        LASSERT(rc == 0);
+                }
                 RETURN(0);
-                
-        default:
-                DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
-                *process = 0;
-                RETURN(ptlrpc_error(req->rq_svc, req));
         }
+        CDEBUG(D_IOCTL, "invalid key\n");
+        RETURN(-EINVAL);
 }
 
-static int mds_queue_final_reply(struct ptlrpc_request *req, int rc)
+static int mdt_set_info(struct ptlrpc_request *req)
 {
-        struct mds_obd *mds = mds_req2mds(req);
+        char *key, *val;
+        struct obd_export *exp = req->rq_export;
+        int keylen, rc = 0, vallen;
+        ENTRY;
 
-        if (rc) {
-                /* Just like ptlrpc_error, but without the sending. */
-                lustre_pack_msg(0, NULL, NULL, &req->rq_replen,
-                                &req->rq_repmsg);
-                req->rq_type = PTL_RPC_MSG_ERR;
-        }
-
-        list_add(&req->rq_list, &mds->mds_delayed_reply_queue);
-        if (--mds->mds_recoverable_clients == 0) {
-                struct list_head *tmp, *n;
-
-                CDEBUG(D_HA,
-                       "all clients recovered, sending delayed replies\n");
-                list_for_each_safe(tmp, n, &mds->mds_delayed_reply_queue) {
-                        req = list_entry(tmp, struct ptlrpc_request, rq_list);
-                        DEBUG_REQ(D_HA, req, "delayed:");
-                        ptlrpc_reply(req->rq_svc, req);
-                }
-        } else {
-                CDEBUG(D_HA, "%d recoverable clients remain\n",
-                       mds->mds_recoverable_clients);
+        key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
+        if (key == NULL) {
+                DEBUG_REQ(D_HA, req, "no set_info key");
+                RETURN(-EFAULT);
         }
+        keylen = req->rq_reqmsg->buflens[0];
 
-        return 1;
-}
+        if (keylen == strlen("mds_num") &&
+            memcmp(key, "mds_num", keylen) == 0) {
+                rc = lustre_pack_reply(req, 0, NULL, NULL);
+                if (rc)
+                        RETURN(rc);
+                val = lustre_msg_buf(req->rq_reqmsg, 1, 0);
 
-static char *reint_names[] = {
-        [REINT_SETATTR] "setattr",
-        [REINT_CREATE]  "create",
-        [REINT_LINK]    "link",
-        [REINT_UNLINK]  "unlink",
-        [REINT_RENAME]  "rename"
-};
+                vallen = req->rq_reqmsg->buflens[1];
 
+                rc = obd_set_info(exp, keylen, key, vallen, val);
+                req->rq_repmsg->status = 0;
+                RETURN(rc);
+        } else if (keylen == strlen("client") &&
+                   memcmp(key, "client", keylen) == 0) {
+                rc = lustre_pack_reply(req, 0, NULL, NULL);
+                if (rc)
+                        RETURN(rc);
+                rc = obd_set_info(exp, keylen, key, sizeof(obd_id), NULL);
+                req->rq_repmsg->status = 0;
+                RETURN(rc);
+        } 
+        CDEBUG(D_IOCTL, "invalid key\n");
+        RETURN(-EINVAL);
+}
+
+extern int ost_brw_write(struct ptlrpc_request *, struct obd_trans_info *);
 int mds_handle(struct ptlrpc_request *req)
 {
-        int rc;
-        int should_process;
+        int should_process, fail = OBD_FAIL_MDS_ALL_REPLY_NET;
+        int rc = 0;
         struct mds_obd *mds = NULL; /* quell gcc overwarning */
+        struct obd_device *obd = NULL;
         ENTRY;
 
-        rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
-        if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_HANDLE_UNPACK)) {
-                CERROR("lustre_mds: Invalid request\n");
-                GOTO(out, rc);
-        }
-
-        LASSERT(!strcmp(req->rq_obd->obd_type->typ_name, LUSTRE_MDT_NAME));
+        OBD_FAIL_RETURN(OBD_FAIL_MDS_ALL_REQUEST_NET | OBD_FAIL_ONCE, 0);
 
+        LASSERT(current->journal_info == NULL);
+        /* XXX identical to OST */
         if (req->rq_reqmsg->opc != MDS_CONNECT) {
-                if (req->rq_export == NULL)
+                struct mds_export_data *med;
+                int recovering, abort_recovery;
+
+                if (req->rq_export == NULL) {
+                        CERROR("lustre_mds: operation %d on unconnected MDS\n",
+                               req->rq_reqmsg->opc);
+                        req->rq_status = -ENOTCONN;
                         GOTO(out, rc = -ENOTCONN);
+                }
 
-                mds = mds_req2mds(req);
-                if (mds->mds_recoverable_clients != 0) {
-                        rc = filter_recovery_request(req, mds, &should_process);
+                med = &req->rq_export->exp_mds_data;
+                obd = req->rq_export->exp_obd;
+                mds = &obd->u.mds;
+
+                /* sanity check: if the xid matches, the request must
+                 * be marked as a resent or replayed */
+                if (req->rq_xid == med->med_mcd->mcd_last_xid)
+                        LASSERTF(lustre_msg_get_flags(req->rq_reqmsg) &
+                                 (MSG_RESENT | MSG_REPLAY),
+                                 "rq_xid "LPU64" matches last_xid, "
+                                 "expected RESENT flag\n",
+                                 req->rq_xid);
+                /* else: note the opposite is not always true; a
+                 * RESENT req after a failover will usually not match
+                 * the last_xid, since it was likely never
+                 * committed. A REPLAYed request will almost never
+                 * match the last xid, however it could for a
+                 * committed, but still retained, open. */
+
+                /* Check for aborted recovery. */
+                spin_lock_bh(&obd->obd_processing_task_lock);
+                abort_recovery = obd->obd_abort_recovery;
+                recovering = obd->obd_recovering;
+                spin_unlock_bh(&obd->obd_processing_task_lock);
+                if (abort_recovery) {
+                        target_abort_recovery(obd);
+                } else if (recovering) {
+                        rc = mds_filter_recovery_request(req, obd,
+                                                         &should_process);
                         if (rc || !should_process)
                                 RETURN(rc);
                 }
@@ -1272,23 +1628,18 @@ int mds_handle(struct ptlrpc_request *req)
         case MDS_CONNECT:
                 DEBUG_REQ(D_INODE, req, "connect");
                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
-                rc = target_handle_connect(req);
-                /* Make sure that last_rcvd is correct. */
-                if (!rc) {
+                rc = target_handle_connect(req, mds_handle);
+                if (!rc)
                         /* Now that we have an export, set mds. */
                         mds = mds_req2mds(req);
-                        mds_fsync_super(mds->mds_sb);
-                }
                 break;
 
         case MDS_DISCONNECT:
                 DEBUG_REQ(D_INODE, req, "disconnect");
                 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
                 rc = target_handle_disconnect(req);
-                /* Make sure that last_rcvd is correct. */
-                if (!rc)
-                        mds_fsync_super(mds->mds_sb);
-                goto out;
+                req->rq_status = rc;            /* superfluous? */
+                break;
 
         case MDS_GETSTATUS:
                 DEBUG_REQ(D_INODE, req, "getstatus");
@@ -1296,17 +1647,29 @@ int mds_handle(struct ptlrpc_request *req)
                 rc = mds_getstatus(req);
                 break;
 
-        case MDS_GETLOVINFO:
-                DEBUG_REQ(D_INODE, req, "getlovinfo");
-                rc = mds_getlovinfo(req);
-                break;
-
         case MDS_GETATTR:
                 DEBUG_REQ(D_INODE, req, "getattr");
                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
                 rc = mds_getattr(0, req);
                 break;
 
+        case MDS_GETATTR_NAME: {
+                struct lustre_handle lockh;
+                DEBUG_REQ(D_INODE, req, "getattr_name");
+                OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NAME_NET, 0);
+
+                /* If this request gets a reconstructed reply, we won't be
+                 * acquiring any new locks in mds_getattr_name, so we don't
+                 * want to cancel.
+                 */
+                lockh.cookie = 0;
+                rc = mds_getattr_name(0, req, &lockh, MDS_INODELOCK_UPDATE);
+                /* this non-intent call (from an ioctl) is special */
+                req->rq_status = rc;
+                if (rc == 0 && lockh.cookie)
+                        ldlm_lock_decref(&lockh, LCK_PR);
+                break;
+        }
         case MDS_STATFS:
                 DEBUG_REQ(D_INODE, req, "statfs");
                 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
@@ -1314,41 +1677,59 @@ int mds_handle(struct ptlrpc_request *req)
                 break;
 
         case MDS_READPAGE:
-                DEBUG_REQ(D_INODE, req, "readpage\n");
+                DEBUG_REQ(D_INODE, req, "readpage");
                 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
                 rc = mds_readpage(req);
 
-                if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
-                        return 0;
+                if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_SENDPAGE)) {
+                        if (req->rq_reply_state) {
+                                lustre_free_reply_state (req->rq_reply_state);
+                                req->rq_reply_state = NULL;
+                        }
+                        RETURN(0);
+                }
+
                 break;
 
         case MDS_REINT: {
-                int size = sizeof(struct mds_body);
-                int opc = *(u32 *)lustre_msg_buf(req->rq_reqmsg, 0), 
-                        realopc = opc & REINT_OPCODE_MASK;
-                        
-                DEBUG_REQ(D_INODE, req, "reint (%s%s)",
-                          reint_names[realopc],
-                          opc & REINT_REPLAYING ? "|REPLAYING" : "");
-                          
+                __u32 *opcp = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*opcp));
+                __u32  opc;
+                int size[3] = {sizeof(struct mds_body), mds->mds_max_mdsize,
+                               mds->mds_max_cookiesize};
+                int bufcount;
+
+                /* NB only peek inside req now; mds_reint() will swab it */
+                if (opcp == NULL) {
+                        CERROR ("Can't inspect opcode\n");
+                        rc = -EINVAL;
+                        break;
+                }
+                opc = *opcp;
+                if (lustre_msg_swabbed (req->rq_reqmsg))
+                        __swab32s(&opc);
+
+                DEBUG_REQ(D_INODE, req, "reint %d (%s)", opc,
+                          (opc < sizeof(reint_names) / sizeof(reint_names[0]) ||
+                           reint_names[opc] == NULL) ? reint_names[opc] :
+                                                       "unknown opcode");
+
                 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
 
-                rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
-                                     &req->rq_repmsg);
-                if (rc) {
-                        req->rq_status = rc;
+                if (opc == REINT_UNLINK)
+                        bufcount = 3;
+                else if (opc == REINT_OPEN || opc == REINT_RENAME)
+                        bufcount = 2;
+                else
+                        bufcount = 1;
+
+                rc = lustre_pack_reply(req, bufcount, size, NULL);
+                if (rc)
                         break;
-                }
-                rc = mds_reint(req, 0);
-                OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET_REP, 0);
-                break;
-                }
 
-        case MDS_OPEN:
-                DEBUG_REQ(D_INODE, req, "open");
-                OBD_FAIL_RETURN(OBD_FAIL_MDS_OPEN_NET, 0);
-                rc = mds_open(req);
+                rc = mds_reint(req, 0, NULL);
+                fail = OBD_FAIL_MDS_REINT_NET_REP;
                 break;
+        }
 
         case MDS_CLOSE:
                 DEBUG_REQ(D_INODE, req, "close");
@@ -1356,10 +1737,40 @@ int mds_handle(struct ptlrpc_request *req)
                 rc = mds_close(req);
                 break;
 
+        case MDS_DONE_WRITING:
+                DEBUG_REQ(D_INODE, req, "done_writing");
+                OBD_FAIL_RETURN(OBD_FAIL_MDS_DONE_WRITING_NET, 0);
+                rc = mds_done_writing(req);
+                break;
+
+        case MDS_PIN:
+                DEBUG_REQ(D_INODE, req, "pin");
+                OBD_FAIL_RETURN(OBD_FAIL_MDS_PIN_NET, 0);
+                rc = mds_pin(req);
+                break;
+
+        case MDS_SYNC:
+                DEBUG_REQ(D_INODE, req, "sync");
+                OBD_FAIL_RETURN(OBD_FAIL_MDS_SYNC_NET, 0);
+                rc = mds_sync(req);
+                break;
+
+        case OBD_PING:
+                DEBUG_REQ(D_INODE, req, "ping");
+                rc = target_handle_ping(req);
+                break;
+
+        case OBD_LOG_CANCEL:
+                CDEBUG(D_INODE, "log cancel\n");
+                OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
+                rc = -ENOTSUPP; /* la la la */
+                break;
+
         case LDLM_ENQUEUE:
                 DEBUG_REQ(D_INODE, req, "enqueue");
                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
-                rc = ldlm_handle_enqueue(req);
+                rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
+                                         ldlm_server_blocking_ast, NULL);
                 break;
         case LDLM_CONVERT:
                 DEBUG_REQ(D_INODE, req, "convert");
@@ -1373,44 +1784,98 @@ int mds_handle(struct ptlrpc_request *req)
                 LBUG();
                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
                 break;
+        case LLOG_ORIGIN_HANDLE_CREATE:
+                DEBUG_REQ(D_INODE, req, "llog_init");
+                OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
+                rc = llog_origin_handle_create(req);
+                break;
+        case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
+                DEBUG_REQ(D_INODE, req, "llog next block");
+                OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
+                rc = llog_origin_handle_next_block(req);
+                break;
+        case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
+                DEBUG_REQ(D_INODE, req, "llog prev block");
+                OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
+                rc = llog_origin_handle_prev_block(req);
+                break;
+        case LLOG_ORIGIN_HANDLE_READ_HEADER:
+                DEBUG_REQ(D_INODE, req, "llog read header");
+                OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
+                rc = llog_origin_handle_read_header(req);
+                break;
+        case LLOG_ORIGIN_HANDLE_CLOSE:
+                DEBUG_REQ(D_INODE, req, "llog close");
+                OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
+                rc = llog_origin_handle_close(req);
+                break;
+        case OST_CREATE:
+                DEBUG_REQ(D_INODE, req, "ost_create");
+                rc = mdt_obj_create(req);
+                break;
+        case OST_GET_INFO:
+                DEBUG_REQ(D_INODE, req, "get_info");
+                rc = mdt_get_info(req);
+                break;
+        case OST_SET_INFO:
+                DEBUG_REQ(D_INODE, req, "set_info");
+                rc = mdt_set_info(req);
+                break;
+        case OST_WRITE:
+                CDEBUG(D_INODE, "write\n");
+                OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
+                rc = ost_brw_write(req, NULL);
+                LASSERT(current->journal_info == NULL);
+                /* mdt_brw sends its own replies */
+                RETURN(rc);
+                break;
+        case LLOG_CATINFO:
+                DEBUG_REQ(D_INODE, req, "llog catinfo");
+                OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
+                rc = llog_catinfo(req);
+                break;
         default:
-                rc = ptlrpc_error(req->rq_svc, req);
+                req->rq_status = -ENOTSUPP;
+                rc = ptlrpc_error(req);
                 RETURN(rc);
         }
 
+        LASSERT(current->journal_info == NULL);
+
         EXIT;
 
-        if (!rc) {
+        /* If we're DISCONNECTing, the mds_export_data is already freed */
+        if (!rc && req->rq_reqmsg->opc != MDS_DISCONNECT) {
                 struct mds_export_data *med = &req->rq_export->exp_mds_data;
-
+                struct obd_device *obd = list_entry(mds, struct obd_device,
+                                                    u.mds);
                 req->rq_repmsg->last_xid =
-                        HTON__u64(le64_to_cpu(med->med_mcd->mcd_last_xid));
-                req->rq_repmsg->last_committed =
-                        HTON__u64(mds->mds_last_committed);
-                CDEBUG(D_INFO, "last_rcvd ~%Lu, last_committed %Lu, xid %d\n",
-                       (unsigned long long)mds->mds_last_rcvd,
-                       (unsigned long long)mds->mds_last_committed,
-                       cpu_to_le32(req->rq_xid));
+                        le64_to_cpu(med->med_mcd->mcd_last_xid);
+
+                if (!obd->obd_no_transno) {
+                        req->rq_repmsg->last_committed =
+                                obd->obd_last_committed;
+                } else {
+                        DEBUG_REQ(D_IOCTL, req,
+                                  "not sending last_committed update");
+                }
+                CDEBUG(D_INFO, "last_transno "LPU64", last_committed "LPU64
+                       ", xid "LPU64"\n",
+                       mds->mds_last_transno, obd->obd_last_committed,
+                       req->rq_xid);
         }
  out:
 
         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LAST_REPLAY) {
-                struct mds_obd *mds = mds_req2mds(req);
-                LASSERT(mds->mds_recoverable_clients);
-                DEBUG_REQ(D_HA, req, "LAST_REPLAY, queuing reply");
-                return mds_queue_final_reply(req, rc);
-        }
-        
-        /* MDS_CONNECT / EALREADY (note: not -EALREADY!) isn't an error */
-        if (rc && (req->rq_reqmsg->opc != MDS_CONNECT ||
-                   rc != EALREADY)) {
-                CERROR("mds: processing error (opcode %d): %d\n",
-                       req->rq_reqmsg->opc, rc);
-                ptlrpc_error(req->rq_svc, req);
-        } else {
-                CDEBUG(D_NET, "sending reply\n");
-                ptlrpc_reply(req->rq_svc, req);
+                if (obd && obd->obd_recovering) {
+                        DEBUG_REQ(D_HA, req, "LAST_REPLAY, queuing reply");
+                        return target_queue_final_reply(req, rc);
+                }
+                /* Lost a race with recovery; let the error path DTRT. */
+                rc = req->rq_status = -ENOTCONN;
         }
+
+        target_send_reply(req, rc, fail);
         return 0;
 }
 
@@ -1419,422 +1884,825 @@ int mds_handle(struct ptlrpc_request *req)
  * then the server last_rcvd value may be less than that of the clients.
  * This will alert us that we may need to do client recovery.
  *
- * Assumes we are already in the server filesystem context.
- *
- * Also assumes for mds_last_rcvd that we are not modifying it (no locking).
+ * Also assumes for mds_last_transno that we are not modifying it (no locking).
  */
-int mds_update_server_data(struct mds_obd *mds)
+int mds_update_server_data(struct obd_device *obd, int force_sync)
 {
+        struct mds_obd *mds = &obd->u.mds;
         struct mds_server_data *msd = mds->mds_server_data;
         struct file *filp = mds->mds_rcvd_filp;
+        struct lvfs_run_ctxt saved;
         loff_t off = 0;
         int rc;
+        ENTRY;
 
-        msd->msd_last_rcvd = cpu_to_le64(mds->mds_last_rcvd);
-        msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
+        push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
+        msd->msd_last_transno = cpu_to_le64(mds->mds_last_transno);
 
-        CDEBUG(D_SUPER, "MDS mount_count is %Lu, last_rcvd is %Lu\n",
-               (unsigned long long)mds->mds_mount_count,
-               (unsigned long long)mds->mds_last_rcvd);
-        rc = lustre_fwrite(filp, (char *)msd, sizeof(*msd), &off);
-        if (rc != sizeof(*msd)) {
-                CERROR("error writing MDS server data: rc = %d\n", rc);
-                if (rc > 0)
-                        RETURN(-EIO);
-                RETURN(rc);
-        }
-#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
-        rc = fsync_dev(filp->f_dentry->d_inode->i_rdev);
-#else
-        rc = file_fsync(filp,  filp->f_dentry, 1);
-#endif
+        CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
+               mds->mds_mount_count, mds->mds_last_transno);
+        rc = fsfilt_write_record(obd, filp, msd, sizeof(*msd), &off,force_sync);
         if (rc)
-                CERROR("error flushing MDS server data: rc = %d\n", rc);
-
-        return 0;
-}
-
-/* Do recovery actions for the MDS */
-static int mds_recovery_complete(struct obd_device *obddev)
-{
-        struct mds_obd *mds = &obddev->u.mds;
-        struct obd_run_ctxt saved;
-        int rc;
-
-        LASSERT(mds->mds_recoverable_clients == 0);
-
-        /* This happens at the end when recovery is complete */
-        ++mds->mds_mount_count;
-        push_ctxt(&saved, &mds->mds_ctxt, NULL);
-        rc = mds_update_server_data(mds);
-        pop_ctxt(&saved);
+                CERROR("error writing MDS server data: rc = %d\n", rc);
+        pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
 
-        return rc;
+        RETURN(rc);
 }
 
 /* mount the file system (secretly) */
-static int mds_setup(struct obd_device *obddev, obd_count len, void *buf)
+static int mds_setup(struct obd_device *obd, obd_count len, void *buf)
 {
-        struct obd_ioctl_data* data = buf;
-        struct mds_obd *mds = &obddev->u.mds;
+        struct lustre_cfg* lcfg = buf;
+        struct mds_obd *mds = &obd->u.mds;
+        char *options = NULL;
         struct vfsmount *mnt;
+        unsigned long page;
         int rc = 0;
         ENTRY;
 
-        MOD_INC_USE_COUNT;
-#ifdef CONFIG_DEV_RDONLY
         dev_clear_rdonly(2);
-#endif
-        if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
-                GOTO(err_dec, rc = -EINVAL);
 
-        mds->mds_fstype = strdup(data->ioc_inlbuf2);
+        if (!lcfg->lcfg_inlbuf1 || !lcfg->lcfg_inlbuf2)
+                RETURN(rc = -EINVAL);
+
+        obd->obd_fsops = fsfilt_get_ops(lcfg->lcfg_inlbuf2);
+        if (IS_ERR(obd->obd_fsops))
+                RETURN(rc = PTR_ERR(obd->obd_fsops));
+
+        mds->mds_max_mdsize = sizeof(struct lov_mds_md);
+
+        page = __get_free_page(GFP_KERNEL);
+        if (!page)
+                RETURN(-ENOMEM);
+
+        options = (char *)page;
+        memset(options, 0, PAGE_SIZE);
+
+        /* here we use "iopen_nopriv" hardcoded, because it affects MDS utility
+         * and the rest of options are passed by mount options. Probably this
+         * should be moved to somewhere else like startup scripts or lconf. */
+        sprintf(options, "iopen_nopriv");
+
+        if (lcfg->lcfg_inllen4 > 0 && lcfg->lcfg_inlbuf4)
+                sprintf(options + strlen(options), ",%s",
+                        lcfg->lcfg_inlbuf4);
+
+        /* we have to know mdsnum before touching underlying fs -bzzz */
+        if (lcfg->lcfg_inllen5 > 0 && lcfg->lcfg_inlbuf5 && 
+            strcmp(lcfg->lcfg_inlbuf5, "dumb")) {
+                class_uuid_t uuid;
+
+                CDEBUG(D_OTHER, "MDS: %s is master for %s\n",
+                       obd->obd_name, lcfg->lcfg_inlbuf5);
+
+                generate_random_uuid(uuid);
+                class_uuid_unparse(uuid, &mds->mds_lmv_uuid);
+
+                OBD_ALLOC(mds->mds_lmv_name, lcfg->lcfg_inllen5);
+                if (mds->mds_lmv_name == NULL) 
+                        RETURN(rc = -ENOMEM);
+
+                memcpy(mds->mds_lmv_name, lcfg->lcfg_inlbuf5,
+                       lcfg->lcfg_inllen5);
+                
+                rc = mds_lmv_connect(obd, mds->mds_lmv_name);
+                if (rc) {
+                        OBD_FREE(mds->mds_lmv_name, lcfg->lcfg_inllen5);
+                        GOTO(err_ops, rc);
+                }
+        }
+        
+        /* FIXME-WANGDI: this should be reworked when we will use lmv along 
+         * with cobd, because correct mdsnum is set in mds_lmv_connect(). */
+        if (lcfg->lcfg_inllen6 > 0 && lcfg->lcfg_inlbuf6 && !mds->mds_lmv_obd &&
+            strcmp(lcfg->lcfg_inlbuf6, "dumb")) {
+                if (!memcmp(lcfg->lcfg_inlbuf6, "master", strlen("master")) &&
+                    mds->mds_num == 0) {
+                        mds->mds_num = REAL_MDS_NUMBER;
+                } else if (!memcmp(lcfg->lcfg_inlbuf6, "cache", strlen("cache")) &&
+                           mds->mds_num == 0) {
+                        mds->mds_num = CACHE_MDS_NUMBER;
+                }     
+        }
+
+        mnt = do_kern_mount(lcfg->lcfg_inlbuf2, 0, 
+                            lcfg->lcfg_inlbuf1, options);
+
+        free_page(page);
 
-        mnt = do_kern_mount(mds->mds_fstype, 0, data->ioc_inlbuf1, NULL);
         if (IS_ERR(mnt)) {
                 rc = PTR_ERR(mnt);
                 CERROR("do_kern_mount failed: rc = %d\n", rc);
-                GOTO(err_kfree, rc);
+                GOTO(err_ops, rc);
         }
 
-        CERROR("%s: mnt is %p\n", data->ioc_inlbuf1, mnt);
-        mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
-        if (!mds->mds_sb)
-                GOTO(err_put, rc = -ENODEV);
+        CDEBUG(D_SUPER, "%s: mnt = %p\n", lcfg->lcfg_inlbuf1, mnt);
 
-        init_MUTEX(&mds->mds_transno_sem);
-        mds->mds_max_mdsize = sizeof(struct lov_mds_md);
-        rc = mds_fs_setup(obddev, mnt);
+        sema_init(&mds->mds_orphan_recovery_sem, 1);
+        sema_init(&mds->mds_epoch_sem, 1);
+        spin_lock_init(&mds->mds_transno_lock);
+        mds->mds_max_cookiesize = sizeof(struct llog_cookie);
+        atomic_set(&mds->mds_open_count, 0);
+        atomic_set(&mds->mds_real_clients, 0);
+
+        obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
+                                                LDLM_NAMESPACE_SERVER);
+        if (obd->obd_namespace == NULL) {
+                mds_cleanup(obd, 0);
+                GOTO(err_put, rc = -ENOMEM);
+        }
+        ldlm_register_intent(obd->obd_namespace, mds_intent_policy);
+
+        rc = mds_fs_setup(obd, mnt);
         if (rc) {
                 CERROR("MDS filesystem method init failed: rc = %d\n", rc);
-                GOTO(err_put, rc);
+                GOTO(err_ns, rc);
         }
 
-        obddev->obd_namespace =
-                ldlm_namespace_new("mds_server", LDLM_NAMESPACE_SERVER);
-        if (obddev->obd_namespace == NULL) {
-                mds_cleanup(obddev);
-                GOTO(err_fs, rc = -ENOMEM);
+        rc = llog_start_commit_thread();
+        if (rc < 0)
+                GOTO(err_fs, rc);
+
+        if (lcfg->lcfg_inllen3 > 0 && lcfg->lcfg_inlbuf3) {
+                class_uuid_t uuid;
+
+                generate_random_uuid(uuid);
+                class_uuid_unparse(uuid, &mds->mds_lov_uuid);
+
+                OBD_ALLOC(mds->mds_profile, lcfg->lcfg_inllen3);
+                if (mds->mds_profile == NULL)
+                        GOTO(err_fs, rc = -ENOMEM);
+
+                memcpy(mds->mds_profile, lcfg->lcfg_inlbuf3,
+                       lcfg->lcfg_inllen3);
         }
 
         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
-                           "mds_ldlm_client", &obddev->obd_ldlm_client);
+                           "mds_ldlm_client", &obd->obd_ldlm_client);
+        obd->obd_replayable = 1;
+
+        rc = mds_postsetup(obd);
+        if (rc)
+                GOTO(err_fs, rc);
 
-        spin_lock_init(&mds->mds_processing_task_lock);
-        mds->mds_processing_task = 0;
-        INIT_LIST_HEAD(&mds->mds_recovery_queue);
-        INIT_LIST_HEAD(&mds->mds_delayed_reply_queue);
-        
         RETURN(0);
 
 err_fs:
-        mds_fs_cleanup(obddev);
+        /* No extra cleanup needed for llog_init_commit_thread() */
+        mds_fs_cleanup(obd, 0);
+err_ns:
+        ldlm_namespace_free(obd->obd_namespace, 0);
+        obd->obd_namespace = NULL;
 err_put:
         unlock_kernel();
         mntput(mds->mds_vfsmnt);
         mds->mds_sb = 0;
         lock_kernel();
-err_kfree:
-        kfree(mds->mds_fstype);
-err_dec:
-        MOD_DEC_USE_COUNT;
-        RETURN(rc);
+err_ops:
+        fsfilt_put_ops(obd->obd_fsops);
+        return rc;
 }
 
-static int mds_cleanup(struct obd_device *obddev)
+static int mds_postsetup(struct obd_device *obd)
 {
-        struct super_block *sb;
-        struct mds_obd *mds = &obddev->u.mds;
-        struct obd_run_ctxt saved;
+        struct mds_obd *mds = &obd->u.mds;
+        int rc = 0;
         ENTRY;
 
-        sb = mds->mds_sb;
-        if (!mds->mds_sb)
-                RETURN(0);
+        rc = obd_llog_setup(obd, &obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT, 
+                            obd, 0, NULL, &llog_lvfs_ops);
+        if (rc)
+                RETURN(rc);
 
-        push_ctxt(&saved, &mds->mds_ctxt, NULL);
-        mds_update_server_data(mds);
+        /* This check for @dumb string is needed to handle mounting MDS 
+           with smfs. Read lconf:MDSDEV.write_conf() for more detail 
+           explanation. */
+        if (mds->mds_profile && strcmp(mds->mds_profile, "dumb")) {
+                struct lvfs_run_ctxt saved;
+                struct lustre_profile *lprof;
+                struct config_llog_instance cfg;
+
+                cfg.cfg_instance = NULL;
+                cfg.cfg_uuid = mds->mds_lov_uuid;
+                push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
+                rc = class_config_parse_llog(llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT),
+                                             mds->mds_profile, &cfg);
+                pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
+                if (rc)
+                        GOTO(err_llog, rc);
 
-        if (mds->mds_rcvd_filp) {
-                int rc = filp_close(mds->mds_rcvd_filp, 0);
-                mds->mds_rcvd_filp = NULL;
+                lprof = class_get_profile(mds->mds_profile);
+                if (lprof == NULL) {
+                        CERROR("No profile found: %s\n", mds->mds_profile);
+                        GOTO(err_cleanup, rc = -ENOENT);
+                }
+                rc = mds_lov_connect(obd, lprof->lp_osc);
+                if (rc)
+                        GOTO(err_cleanup, rc);
 
+                rc = mds_lmv_postsetup(obd);
                 if (rc)
-                        CERROR("last_rcvd file won't close, rc=%d\n", rc);
+                        GOTO(err_cleanup, rc);
+        }
+
+        RETURN(rc);
+
+err_cleanup:
+        mds_lov_clean(obd);
+err_llog:
+        obd_llog_cleanup(llog_get_context(&obd->obd_llogs,
+                                          LLOG_CONFIG_ORIG_CTXT));
+        RETURN(rc);
+}
+
+int mds_postrecov(struct obd_device *obd)
+{
+        struct mds_obd *mds = &obd->u.mds;
+        struct llog_ctxt *ctxt;
+        int rc, item = 0;
+        ENTRY;
+
+        LASSERT(!obd->obd_recovering);
+        ctxt = llog_get_context(&obd->obd_llogs, LLOG_UNLINK_ORIG_CTXT);
+        LASSERT(ctxt != NULL);
+
+        /* set nextid first, so we are sure it happens */
+        rc = mds_lov_set_nextid(obd);
+        if (rc) {
+                CERROR("%s: mds_lov_set_nextid failed\n", obd->obd_name);
+                GOTO(out, rc);
+        }
+
+        /* clean PENDING dir */
+        rc = mds_cleanup_orphans(obd);
+        if (rc < 0)
+                GOTO(out, rc);
+        item = rc;
+
+        rc = llog_connect(ctxt, obd->u.mds.mds_lov_desc.ld_tgt_count,
+                          NULL, NULL, NULL);
+        if (rc) {
+                CERROR("%s: failed at llog_origin_connect: %d\n", 
+                       obd->obd_name, rc);
+                GOTO(out, rc);
         }
-        pop_ctxt(&saved);
+
+        /* remove the orphaned precreated objects */
+        rc = mds_lov_clearorphans(mds, NULL /* all OSTs */);
+        if (rc)
+                GOTO(err_llog, rc);
+
+out:
+        RETURN(rc < 0 ? rc : item);
+
+err_llog:
+        /* cleanup all llogging subsystems */
+        rc = obd_llog_finish(obd, &obd->obd_llogs,
+                             mds->mds_lov_desc.ld_tgt_count);
+        if (rc)
+                CERROR("%s: failed to cleanup llogging subsystems\n",
+                        obd->obd_name);
+        goto out;
+}
+
+int mds_lov_clean(struct obd_device *obd)
+{
+        struct mds_obd *mds = &obd->u.mds;
+
+        if (mds->mds_profile) {
+                char * cln_prof;
+                struct config_llog_instance cfg;
+                struct lvfs_run_ctxt saved;
+                int len = strlen(mds->mds_profile) + sizeof("-clean") + 1;
+
+                OBD_ALLOC(cln_prof, len);
+                sprintf(cln_prof, "%s-clean", mds->mds_profile);
+
+                cfg.cfg_instance = NULL;
+                cfg.cfg_uuid = mds->mds_lov_uuid;
+
+                push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
+                class_config_parse_llog(llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT),
+                                        cln_prof, &cfg);
+                pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
+
+                OBD_FREE(cln_prof, len);
+                OBD_FREE(mds->mds_profile, strlen(mds->mds_profile) + 1);
+                mds->mds_profile = NULL;
+        }
+        RETURN(0);
+}
+
+int mds_lmv_clean(struct obd_device *obd)
+{
+        struct mds_obd *mds = &obd->u.mds;
+
+        if (mds->mds_lmv_name) {
+                OBD_FREE(mds->mds_lmv_name, strlen(mds->mds_lmv_name) + 1);
+                mds->mds_lmv_name = NULL;
+        }
+        RETURN(0);
+}
+
+static int mds_precleanup(struct obd_device *obd, int flags)
+{
+        int rc = 0;
+        ENTRY;
+
+        mds_lmv_clean(obd);
+        mds_lov_disconnect(obd, flags);
+        mds_lov_clean(obd);
+        obd_llog_cleanup(llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT));
+        RETURN(rc);
+}
+
+static int mds_cleanup(struct obd_device *obd, int flags)
+{
+        struct mds_obd *mds = &obd->u.mds;
+        ENTRY;
+
+        if (mds->mds_sb == NULL)
+                RETURN(0);
+
+        mds_update_server_data(obd, 1);
+        if (mds->mds_lov_objids != NULL) {
+                OBD_FREE(mds->mds_lov_objids,
+                         mds->mds_lov_desc.ld_tgt_count * sizeof(obd_id));
+        }
+        mds_fs_cleanup(obd, flags);
 
         unlock_kernel();
+
+        /* 2 seems normal on mds, (may_umount() also expects 2
+          fwiw), but we only see 1 at this point in obdfilter. */
+        if (atomic_read(&obd->u.mds.mds_vfsmnt->mnt_count) > 2)
+                CERROR("%s: mount busy, mnt_count %d != 2\n", obd->obd_name,
+                       atomic_read(&obd->u.mds.mds_vfsmnt->mnt_count));
+
         mntput(mds->mds_vfsmnt);
+
         mds->mds_sb = 0;
-        kfree(mds->mds_fstype);
 
-        ldlm_namespace_free(obddev->obd_namespace);
+        ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
+
+        spin_lock_bh(&obd->obd_processing_task_lock);
+        if (obd->obd_recovering) {
+                target_cancel_recovery_timer(obd);
+                obd->obd_recovering = 0;
+        }
+        spin_unlock_bh(&obd->obd_processing_task_lock);
 
         lock_kernel();
-#ifdef CONFIG_DEV_RDONLY
         dev_clear_rdonly(2);
-#endif
-        mds_fs_cleanup(obddev);
+        fsfilt_put_ops(obd->obd_fsops);
 
-        MOD_DEC_USE_COUNT;
         RETURN(0);
 }
 
-static int ldlm_intent_policy(struct ldlm_lock *lock, void *req_cookie,
-                              ldlm_mode_t mode, int flags, void *data)
+static void fixup_handle_for_resent_req(struct ptlrpc_request *req,
+                                        struct ldlm_lock *new_lock,
+                                        struct lustre_handle *lockh)
 {
-        struct ptlrpc_request *req = req_cookie;
-        int rc = 0;
-        ENTRY;
+        struct obd_export *exp = req->rq_export;
+        struct obd_device *obd = exp->exp_obd;
+        struct ldlm_request *dlmreq =
+                lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*dlmreq));
+        struct lustre_handle remote_hdl = dlmreq->lock_handle1;
+        struct list_head *iter;
+
+        if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
+                return;
+
+        l_lock(&obd->obd_namespace->ns_lock);
+        list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
+                struct ldlm_lock *lock;
+                lock = list_entry(iter, struct ldlm_lock, l_export_chain);
+                if (lock == new_lock)
+                        continue;
+                if (lock->l_remote_handle.cookie == remote_hdl.cookie) {
+                        lockh->cookie = lock->l_handle.h_cookie;
+                        DEBUG_REQ(D_HA, req, "restoring lock cookie "LPX64,
+                                  lockh->cookie);
+                        l_unlock(&obd->obd_namespace->ns_lock);
+                        return;
+                }
+        }
+        l_unlock(&obd->obd_namespace->ns_lock);
 
-        if (!req_cookie)
-                RETURN(0);
+        /* If the xid matches, then we know this is a resent request,
+         * and allow it. (It's probably an OPEN, for which we don't
+         * send a lock */
+        if (req->rq_xid == exp->exp_mds_data.med_mcd->mcd_last_xid)
+                return;
 
-        if (req->rq_reqmsg->bufcount > 1) {
-                /* an intent needs to be considered */
-                struct ldlm_intent *it = lustre_msg_buf(req->rq_reqmsg, 1);
-                struct mds_obd *mds= &req->rq_export->exp_obd->u.mds;
-                struct mds_body *mds_rep;
-                struct ldlm_reply *rep;
-                __u64 new_resid[3] = {0, 0, 0}, old_res;
-                int rc, size[3] = {sizeof(struct ldlm_reply),
-                                                  sizeof(struct mds_body),
-                                                  mds->mds_max_mdsize};
+        /* This remote handle isn't enqueued, so we never received or
+         * processed this request.  Clear MSG_RESENT, because it can
+         * be handled like any normal request now. */
 
-                it->opc = NTOH__u64(it->opc);
+        lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
 
-                LDLM_DEBUG(lock, "intent policy, opc: %s",
-                           ldlm_it2str(it->opc));
+        DEBUG_REQ(D_HA, req, "no existing lock with rhandle "LPX64,
+                  remote_hdl.cookie);
+}
 
-                rc = lustre_pack_msg(3, size, NULL, &req->rq_replen,
-                                     &req->rq_repmsg);
-                if (rc) {
-                        rc = req->rq_status = -ENOMEM;
-                        RETURN(rc);
-                }
+int intent_disposition(struct ldlm_reply *rep, int flag)
+{
+        if (!rep)
+                return 0;
+        return (rep->lock_policy_res1 & flag);
+}
 
-                rep = lustre_msg_buf(req->rq_repmsg, 0);
-                rep->lock_policy_res1 = 1;
+void intent_set_disposition(struct ldlm_reply *rep, int flag)
+{
+        if (!rep)
+                return;
+        rep->lock_policy_res1 |= flag;
+}
 
-                /* execute policy */
-                switch ((long)it->opc) {
-                case IT_CREAT|IT_OPEN:
-                        rc = mds_reint(req, 2);
-                        if (rc || (req->rq_status != 0 &&
-                                   req->rq_status != -EEXIST)) {
-                                rep->lock_policy_res2 = req->rq_status;
-                                RETURN(ELDLM_LOCK_ABORTED);
-                        }
-                        break;
-                case IT_CREAT:
-                case IT_MKDIR:
-                case IT_MKNOD:
-                case IT_RENAME2:
-                case IT_LINK2:
-                case IT_RMDIR:
-                case IT_SYMLINK:
-                case IT_UNLINK:
-                        rc = mds_reint(req, 2);
-                        if (rc || (req->rq_status != 0 &&
-                                   req->rq_status != -EISDIR &&
-                                   req->rq_status != -ENOTDIR)) {
-                                rep->lock_policy_res2 = req->rq_status;
-                                RETURN(ELDLM_LOCK_ABORTED);
-                        }
-                        break;
-                case IT_GETATTR:
-                case IT_LOOKUP:
-                case IT_OPEN:
-                case IT_READDIR:
-                case IT_READLINK:
-                case IT_RENAME:
-                case IT_LINK:
-                case IT_SETATTR:
-                        rc = mds_getattr_name(2, req);
-                        /* FIXME: we need to sit down and decide on who should
-                         * set req->rq_status, who should return negative and
-                         * positive return values, and what they all mean. */
-                        if (rc || req->rq_status != 0) {
-                                rep->lock_policy_res2 = req->rq_status;
+static int mds_intent_policy(struct ldlm_namespace *ns,
+                             struct ldlm_lock **lockp, void *req_cookie,
+                             ldlm_mode_t mode, int flags, void *data)
+{
+        struct ptlrpc_request *req = req_cookie;
+        struct ldlm_lock *lock = *lockp;
+        struct ldlm_intent *it;
+        struct mds_obd *mds = &req->rq_export->exp_obd->u.mds;
+        struct ldlm_reply *rep;
+        struct lustre_handle lockh = { 0 };
+        struct ldlm_lock *new_lock;
+        int getattr_part = MDS_INODELOCK_UPDATE;
+        int rc, offset = 2, repsize[4] = {sizeof(struct ldlm_reply),
+                                          sizeof(struct mds_body),
+                                          mds->mds_max_mdsize,
+                                          mds->mds_max_cookiesize};
+        ENTRY;
+
+        LASSERT(req != NULL);
+
+        if (req->rq_reqmsg->bufcount <= 1) {
+                /* No intent was provided */
+                int size = sizeof(struct ldlm_reply);
+                rc = lustre_pack_reply(req, 1, &size, NULL);
+                LASSERT(rc == 0);
+                RETURN(0);
+        }
+
+        it = lustre_swab_reqbuf(req, 1, sizeof(*it), lustre_swab_ldlm_intent);
+        if (it == NULL) {
+                CERROR("Intent missing\n");
+                RETURN(req->rq_status = -EFAULT);
+        }
+
+        LDLM_DEBUG(lock, "intent policy, opc: %s", ldlm_it2str(it->opc));
+
+        rc = lustre_pack_reply(req, it->opc == IT_UNLINK ? 4 : 3, repsize,
+                               NULL);
+        if (rc)
+                RETURN(req->rq_status = rc);
+
+        rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
+        intent_set_disposition(rep, DISP_IT_EXECD);
+
+        fixup_handle_for_resent_req(req, lock, &lockh);
+
+        /* execute policy */
+        switch ((long)it->opc) {
+        case IT_OPEN:
+        case IT_CREAT|IT_OPEN:
+                /* XXX swab here to assert that an mds_open reint
+                 * packet is following */
+                rep->lock_policy_res2 = mds_reint(req, offset, &lockh);
+#if 0
+                /* We abort the lock if the lookup was negative and
+                 * we did not make it to the OPEN portion */
+                if (!intent_disposition(rep, DISP_LOOKUP_EXECD))
+                        RETURN(ELDLM_LOCK_ABORTED);
+                if (intent_disposition(rep, DISP_LOOKUP_NEG) &&
+                    !intent_disposition(rep, DISP_OPEN_OPEN))
+#endif 
+                        if (rep->lock_policy_res2) {
+                                /* mds_open returns ENOLCK where it
+                                 * should return zero, but it has no
+                                 * lock to return */
+                                if (rep->lock_policy_res2 == ENOLCK)
+                                        rep->lock_policy_res2 = 0;
                                 RETURN(ELDLM_LOCK_ABORTED);
                         }
-                        break;
-                case IT_READDIR|IT_OPEN:
-                        LBUG();
-                        break;
-                default:
-                        CERROR("Unhandled intent "LPD64"\n", it->opc);
+                break;
+        case IT_LOOKUP:
+                getattr_part = MDS_INODELOCK_LOOKUP;
+        case IT_CHDIR:
+        case IT_GETATTR:
+                getattr_part |= MDS_INODELOCK_LOOKUP;
+        case IT_READDIR:
+                rep->lock_policy_res2 = mds_getattr_name(offset, req, &lockh,
+                                                         getattr_part);
+                /* FIXME: LDLM can set req->rq_status. MDS sets
+                   policy_res{1,2} with disposition and status.
+                   - replay: returns 0 & req->status is old status
+                   - otherwise: returns req->status */
+                if (intent_disposition(rep, DISP_LOOKUP_NEG))
+                        rep->lock_policy_res2 = 0;
+                if (!intent_disposition(rep, DISP_LOOKUP_POS) ||
+                    rep->lock_policy_res2)
+                        RETURN(ELDLM_LOCK_ABORTED);
+                if (req->rq_status != 0) {
                         LBUG();
+                        rep->lock_policy_res2 = req->rq_status;
+                        RETURN(ELDLM_LOCK_ABORTED);
                 }
+                break;
+        default:
+                CERROR("Unhandled intent "LPD64"\n", it->opc);
+                LBUG();
+        }
 
-                /* We don't bother returning a lock to the client for a file
-                 * or directory we are removing.
-                 *
-                 * As for link and rename, there is no reason for the client
-                 * to get a lock on the target at this point.  If they are
-                 * going to modify the file/directory later they will get a
-                 * lock at that time.
-                 */
-                if (it->opc & (IT_UNLINK | IT_RMDIR | IT_LINK | IT_LINK2 |
-                               IT_RENAME | IT_RENAME2))
-                        RETURN(ELDLM_LOCK_ABORTED);
+        /* By this point, whatever function we called above must have either
+         * filled in 'lockh', been an intent replay, or returned an error.  We
+         * want to allow replayed RPCs to not get a lock, since we would just
+         * drop it below anyways because lock replay is done separately by the
+         * client afterwards.  For regular RPCs we want to give the new lock to
+         * the client instead of whatever lock it was about to get. */
+        new_lock = ldlm_handle2lock(&lockh);
+        if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY))
+                RETURN(0);
 
-                rep->lock_policy_res2 = req->rq_status;
-                mds_rep = lustre_msg_buf(req->rq_repmsg, 1);
+        LASSERT(new_lock != NULL);
 
-                /* If the client is about to open a file that doesn't have an MD
-                 * stripe record, it's going to need a write lock. */
-                if (it->opc & IT_OPEN &&
-                    !(lustre_msg_get_op_flags(req->rq_reqmsg)&MDS_OPEN_HAS_EA)){
-                        LDLM_DEBUG(lock, "open with no EA; returning PW lock");
-                        lock->l_req_mode = LCK_PW;
-                }
+        /* If we've already given this lock to a client once, then we should
+         * have no readers or writers.  Otherwise, we should have one reader
+         * _or_ writer ref (which will be zeroed below) before returning the
+         * lock to a client. */
+        if (new_lock->l_export == req->rq_export) {
+                LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
+        } else {
+                LASSERT(new_lock->l_export == NULL);
+                LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
+        }
 
-                if (flags & LDLM_FL_INTENT_ONLY) {
-                        LDLM_DEBUG(lock, "INTENT_ONLY, aborting lock");
-                        RETURN(ELDLM_LOCK_ABORTED);
-                }
-                /* Give the client a lock on the child object, instead of the
-                 * parent that it requested. */
-                new_resid[0] = NTOH__u32(mds_rep->ino);
-                new_resid[1] = NTOH__u32(mds_rep->generation);
-                if (new_resid[0] == 0)
-                        LBUG();
-                old_res = lock->l_resource->lr_name[0];
+        *lockp = new_lock;
 
-                ldlm_lock_change_resource(lock, new_resid);
-                if (lock->l_resource == NULL) {
-                        LBUG();
-                        RETURN(-ENOMEM);
-                }
-                LDLM_DEBUG(lock, "intent policy, old res %ld",
-                           (long)old_res);
-                RETURN(ELDLM_LOCK_CHANGED);
-        } else {
-                int size = sizeof(struct ldlm_reply);
-                rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
-                                     &req->rq_repmsg);
-                if (rc) {
-                        LBUG();
-                        RETURN(-ENOMEM);
-                }
+        if (new_lock->l_export == req->rq_export) {
+                /* Already gave this to the client, which means that we
+                 * reconstructed a reply. */
+                LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
+                        MSG_RESENT);
+                RETURN(ELDLM_LOCK_REPLACED);
         }
-        RETURN(rc);
+
+        /* Fixup the lock to be given to the client */
+        l_lock(&new_lock->l_resource->lr_namespace->ns_lock);
+        new_lock->l_readers = 0;
+        new_lock->l_writers = 0;
+
+        new_lock->l_export = class_export_get(req->rq_export);
+        list_add(&new_lock->l_export_chain,
+                 &new_lock->l_export->exp_ldlm_data.led_held_locks);
+
+        new_lock->l_blocking_ast = lock->l_blocking_ast;
+        new_lock->l_completion_ast = lock->l_completion_ast;
+
+        memcpy(&new_lock->l_remote_handle, &lock->l_remote_handle,
+               sizeof(lock->l_remote_handle));
+
+        new_lock->l_flags &= ~LDLM_FL_LOCAL;
+
+        LDLM_LOCK_PUT(new_lock);
+        l_unlock(&new_lock->l_resource->lr_namespace->ns_lock);
+
+        RETURN(ELDLM_LOCK_REPLACED);
 }
 
 int mds_attach(struct obd_device *dev, obd_count len, void *data)
 {
-        return lprocfs_reg_obd(dev, status_var_nm_1, dev);
+        struct lprocfs_static_vars lvars;
+
+        lprocfs_init_multi_vars(0, &lvars);
+        return lprocfs_obd_attach(dev, lvars.obd_vars);
 }
 
 int mds_detach(struct obd_device *dev)
 {
-        return lprocfs_dereg_obd(dev);
+        return lprocfs_obd_detach(dev);
+}
+
+int mdt_attach(struct obd_device *dev, obd_count len, void *data)
+{
+        struct lprocfs_static_vars lvars;
+
+        lprocfs_init_multi_vars(1, &lvars);
+        return lprocfs_obd_attach(dev, lvars.obd_vars);
 }
 
-static int mdt_setup(struct obd_device *obddev, obd_count len, void *buf)
+int mdt_detach(struct obd_device *dev)
 {
-        int i;
-        //        struct obd_ioctl_data* data = buf;
-        struct mds_obd *mds = &obddev->u.mds;
+        return lprocfs_obd_detach(dev);
+}
+
+static int mdt_setup(struct obd_device *obd, obd_count len, void *buf)
+{
+        struct mds_obd *mds = &obd->u.mds;
         int rc = 0;
         ENTRY;
 
-        MOD_INC_USE_COUNT;
+        mds->mds_service =
+                ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
+                                MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
+                                mds_handle, "mds", obd->obd_proc_entry);
 
-        mds->mds_service = ptlrpc_init_svc(MDS_NEVENTS, MDS_NBUFS,
-                                           MDS_BUFSIZE, MDS_MAXREQSIZE,
-                                           MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
-                                           "self", mds_handle, "mds");
         if (!mds->mds_service) {
                 CERROR("failed to start service\n");
-                GOTO(err_dec, rc = -EINVAL);
+                RETURN(-ENOMEM);
         }
 
-        for (i = 0; i < MDT_NUM_THREADS; i++) {
-                char name[32];
-                sprintf(name, "ll_mdt_%02d", i);
-                rc = ptlrpc_start_thread(obddev, mds->mds_service, name);
-                if (rc) {
-                        CERROR("cannot start MDT thread #%d: rc %d\n", i, rc);
-                        GOTO(err_thread, rc);
-                }
+        rc = ptlrpc_start_n_threads(obd, mds->mds_service, MDT_NUM_THREADS,
+                                    "ll_mdt");
+        if (rc)
+                GOTO(err_thread, rc);
+
+        mds->mds_setattr_service =
+                ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
+                                MDS_SETATTR_PORTAL, MDC_REPLY_PORTAL,
+                                mds_handle, "mds_setattr",
+                                obd->obd_proc_entry);
+        if (!mds->mds_setattr_service) {
+                CERROR("failed to start getattr service\n");
+                GOTO(err_thread, rc = -ENOMEM);
         }
 
+        rc = ptlrpc_start_n_threads(obd, mds->mds_setattr_service,
+                                    MDT_NUM_THREADS, "ll_mdt_attr");
+        if (rc)
+                GOTO(err_thread2, rc);
+
+        mds->mds_readpage_service =
+                ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
+                                MDS_READPAGE_PORTAL, MDC_REPLY_PORTAL,
+                                mds_handle, "mds_readpage",
+                                obd->obd_proc_entry);
+        if (!mds->mds_readpage_service) {
+                CERROR("failed to start readpage service\n");
+                GOTO(err_thread2, rc = -ENOMEM);
+        }
+
+        rc = ptlrpc_start_n_threads(obd, mds->mds_readpage_service,
+                                    MDT_NUM_THREADS, "ll_mdt_rdpg");
+
+        if (rc)
+                GOTO(err_thread3, rc);
+
         RETURN(0);
 
+err_thread3:
+        ptlrpc_unregister_service(mds->mds_readpage_service);
+err_thread2:
+        ptlrpc_unregister_service(mds->mds_setattr_service);
 err_thread:
-        ptlrpc_stop_all_threads(mds->mds_service);
         ptlrpc_unregister_service(mds->mds_service);
-err_dec:
-        MOD_DEC_USE_COUNT;
-        RETURN(rc);
+        return rc;
 }
 
-
-static int mdt_cleanup(struct obd_device *obddev)
+static int mdt_cleanup(struct obd_device *obd, int flags)
 {
-        struct mds_obd *mds = &obddev->u.mds;
+        struct mds_obd *mds = &obd->u.mds;
         ENTRY;
 
+        ptlrpc_stop_all_threads(mds->mds_readpage_service);
+        ptlrpc_unregister_service(mds->mds_readpage_service);
+
+        ptlrpc_stop_all_threads(mds->mds_setattr_service);
+        ptlrpc_unregister_service(mds->mds_setattr_service);
+
         ptlrpc_stop_all_threads(mds->mds_service);
         ptlrpc_unregister_service(mds->mds_service);
 
-        MOD_DEC_USE_COUNT;
         RETURN(0);
 }
 
-extern int mds_iocontrol(long cmd, struct lustre_handle *conn,
-                         int len, void *karg, void *uarg);
+static struct dentry *mds_lvfs_fid2dentry(__u64 id, __u32 gen, __u64 gr,
+                                          void *data)
+{
+        struct obd_device *obd = data;
+        struct ll_fid fid;
+        fid.id = id;
+        fid.generation = gen;
+        return mds_fid2dentry(&obd->u.mds, &fid, NULL);
+}
+
+static int mds_get_info(struct obd_export *exp, __u32 keylen,
+                        void *key, __u32 *vallen, void *val)
+{
+        struct obd_device *obd;
+        struct mds_obd *mds;
+        ENTRY;
+
+        obd = class_exp2obd(exp);
+        if (obd == NULL) {
+                CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
+                       exp->exp_handle.h_cookie);
+                RETURN(-EINVAL);
+        }
+
+        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.mds.mds_sb;
+                RETURN(0);
+        }
+
+        mds = &obd->u.mds;
+        keylen == strlen("mdsize");
+        if (keylen && memcmp(key, "mdsize", keylen) == 0) {
+                __u32 *mdsize = val;
+                *vallen = sizeof(*mdsize);
+                *mdsize = mds->mds_max_mdsize;
+                RETURN(0);
+        }
+
+        CDEBUG(D_IOCTL, "invalid key\n");
+        RETURN(-EINVAL);
+
+}
+struct lvfs_callback_ops mds_lvfs_ops = {
+        l_fid2dentry:     mds_lvfs_fid2dentry,
+};
+
+int mds_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
+                int objcount, struct obd_ioobj *obj,
+                int niocount, struct niobuf_remote *nb,
+                struct niobuf_local *res,
+                struct obd_trans_info *oti);
+int mds_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
+                 int objcount, struct obd_ioobj *obj, int niocount,
+                 struct niobuf_local *res, struct obd_trans_info *oti,
+                 int rc);
 
 /* use obd ops to offer management infrastructure */
 static struct obd_ops mds_obd_ops = {
-        o_attach:      mds_attach,
-        o_detach:      mds_detach,
-        o_connect:     mds_connect,
-        o_disconnect:  mds_disconnect,
-        o_setup:       mds_setup,
-        o_cleanup:     mds_cleanup,
-        o_iocontrol:   mds_iocontrol
+        .o_owner           = THIS_MODULE,
+        .o_attach          = mds_attach,
+        .o_detach          = mds_detach,
+        .o_connect         = mds_connect,
+        .o_init_export     = mds_init_export,
+        .o_destroy_export  = mds_destroy_export,
+        .o_disconnect      = mds_disconnect,
+        .o_setup           = mds_setup,
+        .o_precleanup      = mds_precleanup,
+        .o_cleanup         = mds_cleanup,
+        .o_postrecov       = mds_postrecov,
+        .o_statfs          = mds_obd_statfs,
+        .o_iocontrol       = mds_iocontrol,
+        .o_create          = mds_obd_create,
+        .o_destroy         = mds_obd_destroy,
+        .o_llog_init       = mds_llog_init,
+        .o_llog_finish     = mds_llog_finish,
+        .o_notify          = mds_notify,
+        .o_get_info        = mds_get_info,
+        .o_set_info        = mds_set_info,
+        .o_preprw          = mds_preprw, 
+        .o_commitrw        = mds_commitrw,
 };
 
 static struct obd_ops mdt_obd_ops = {
-        o_setup:       mdt_setup,
-        o_cleanup:     mdt_cleanup,
+        .o_owner           = THIS_MODULE,
+        .o_attach          = mdt_attach,
+        .o_detach          = mdt_detach,
+        .o_setup           = mdt_setup,
+        .o_cleanup         = mdt_cleanup,
+        .o_attach          = mdt_attach,
+        .o_detach          = mdt_detach,
 };
 
-
 static int __init mds_init(void)
 {
+        struct lprocfs_static_vars lvars;
 
-        mds_file_cache = kmem_cache_create("ll_mds_file_data",
-                                           sizeof(struct mds_file_data),
-                                           0, 0, NULL, NULL);
-        if (mds_file_cache == NULL)
-                return -ENOMEM;
+        lprocfs_init_multi_vars(0, &lvars);
+        class_register_type(&mds_obd_ops, NULL, lvars.module_vars,
+                            LUSTRE_MDS_NAME);
+        lprocfs_init_multi_vars(1, &lvars);
+        class_register_type(&mdt_obd_ops, NULL, lvars.module_vars,
+                            LUSTRE_MDT_NAME);
 
-        class_register_type(&mds_obd_ops, status_class_var, LUSTRE_MDS_NAME);
-        class_register_type(&mdt_obd_ops, 0, LUSTRE_MDT_NAME);
-        ldlm_register_intent(ldlm_intent_policy);
         return 0;
-
 }
 
-static void __exit mds_exit(void)
+static void /*__exit*/ mds_exit(void)
 {
-
-
-        ldlm_unregister_intent();
         class_unregister_type(LUSTRE_MDS_NAME);
         class_unregister_type(LUSTRE_MDT_NAME);
-        if (kmem_cache_destroy(mds_file_cache))
-                CERROR("couldn't free MDS file cache\n");
-
 }
 
-MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
-MODULE_DESCRIPTION("Lustre Metadata Server (MDS) v0.01");
+MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
+MODULE_DESCRIPTION("Lustre Metadata Server (MDS)");
 MODULE_LICENSE("GPL");
 
 module_init(mds_init);