Whamcloud - gitweb
b=20748
[fs/lustre-release.git] / lustre / mds / mds_fs.c
index f0f7552..ce287c6 100644 (file)
 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
  * vim:expandtab:shiftwidth=8:tabstop=8:
  *
- *  mds/mds_fs.c
- *  Lustre Metadata Server (MDS) filesystem interface code
+ * GPL HEADER START
  *
- *  Copyright (C) 2002, 2003 Cluster File Systems, Inc.
- *   Author: Andreas Dilger <adilger@clusterfs.com>
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
- *   This file is part of Lustre, http://www.lustre.org.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
  *
- *   Lustre is free software; you can redistribute it and/or
- *   modify it under the terms of version 2 of the GNU General Public
- *   License as published by the Free Software Foundation.
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
  *
- *   Lustre is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
  *
- *   You should have received a copy of the GNU General Public License
- *   along with Lustre; if not, write to the Free Software
- *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ *
+ * GPL HEADER END
+ */
+/*
+ * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Use is subject to license terms.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
+ *
+ * lustre/mds/mds_fs.c
+ *
+ * Lustre Metadata Server (MDS) filesystem interface code
+ *
+ * Author: Andreas Dilger <adilger@clusterfs.com>
  */
 
-#ifndef EXPORT_SYMTAB
-# define EXPORT_SYMTAB
-#endif
 #define DEBUG_SUBSYSTEM S_MDS
 
 #include <linux/module.h>
 #include <linux/kmod.h>
 #include <linux/version.h>
-#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
+#include <linux/sched.h>
 #include <linux/mount.h>
-#endif
-#include <linux/lustre_mds.h>
-#include <linux/obd_class.h>
-#include <linux/obd_support.h>
-#include <linux/lustre_lib.h>
-#include <linux/lustre_fsfilt.h>
-#include <portals/list.h>
+#include <lustre_mds.h>
+#include <obd_class.h>
+#include <obd_support.h>
+#include <lustre_lib.h>
+#include <lustre_fsfilt.h>
+#include <lustre_disk.h>
+#include <libcfs/list.h>
 
 #include "mds_internal.h"
 
-/* This limit is arbitrary, but for now we fit it in 1 page (32k clients) */
-#define MDS_MAX_CLIENTS (PAGE_SIZE * 8)
-#define MDS_MAX_CLIENT_WORDS (MDS_MAX_CLIENTS / sizeof(unsigned long))
-
-#define LAST_RCVD "last_rcvd"
-#define LOV_OBJID "lov_objid"
-
-/* Add client data to the MDS.  We use a bitmap to locate a free space
- * in the last_rcvd file if cl_off is -1 (i.e. a new client).
- * Otherwise, we have just read the data from the last_rcvd file and
- * we know its offset.
- */
-int mds_client_add(struct obd_device *obd, struct mds_obd *mds,
-                   struct mds_export_data *med, int cl_idx)
-{
-        unsigned long *bitmap = mds->mds_client_bitmap;
-        int new_client = (cl_idx == -1);
-        ENTRY;
-
-        LASSERT(bitmap != NULL);
-
-        /* XXX if mcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
-        if (!strcmp(med->med_mcd->mcd_uuid, obd->obd_uuid.uuid))
-                RETURN(0);
-
-        /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
-         * there's no need for extra complication here
-         */
-        if (new_client) {
-                cl_idx = find_first_zero_bit(bitmap, MDS_MAX_CLIENTS);
-        repeat:
-                if (cl_idx >= MDS_MAX_CLIENTS) {
-                        CERROR("no room for clients - fix MDS_MAX_CLIENTS\n");
-                        return -ENOMEM;
-                }
-                if (test_and_set_bit(cl_idx, bitmap)) {
-                        cl_idx = find_next_zero_bit(bitmap, MDS_MAX_CLIENTS,
-                                                    cl_idx);
-                        goto repeat;
-                }
-        } else {
-                if (test_and_set_bit(cl_idx, bitmap)) {
-                        CERROR("MDS client %d: bit already set in bitmap!!\n",
-                               cl_idx);
-                        LBUG();
-                }
-        }
-
-        CDEBUG(D_INFO, "client at index %d with UUID '%s' added\n",
-               cl_idx, med->med_mcd->mcd_uuid);
-
-        med->med_idx = cl_idx;
-        med->med_off = MDS_LR_CLIENT_START + (cl_idx * MDS_LR_CLIENT_SIZE);
-
-        if (new_client) {
-                struct obd_run_ctxt saved;
-                loff_t off = med->med_off;
-                struct file *file = mds->mds_rcvd_filp;
-                void *handle;
-                int rc, err;
-
-                push_ctxt(&saved, &obd->obd_ctxt, NULL);
-                /* We need to start a transaction here first, to avoid a
-                 * possible ordering deadlock on last_rcvd->i_sem and the
-                 * journal lock. In most places we start the journal handle
-                 * first (because we do compound transactions), and then
-                 * later do the write into last_rcvd, which gets i_sem.
-                 *
-                 * Without this transaction, clients connecting at the same
-                 * time other MDS operations are ongoing get last_rcvd->i_sem
-                 * first (in generic_file_write()) and start the journal
-                 * transaction afterwards, and can deadlock with other ops.
-                 *
-                 * We use FSFILT_OP_SETATTR because it is smallest, but all
-                 * ops include enough space for the last_rcvd update so we
-                 * could use any of them, or maybe an FSFILT_OP_NONE is best?
-                 */
-                handle = fsfilt_start(obd, file->f_dentry->d_inode,
-                                      FSFILT_OP_SETATTR, NULL);
-                if (IS_ERR(handle)) {
-                        rc = PTR_ERR(handle);
-                        CERROR("unable to start transaction: rc %d\n", rc);
-                } else {
-                        rc = fsfilt_write_record(obd, file, med->med_mcd,
-                                                 sizeof(*med->med_mcd),
-                                                 &off, 1);
-                        err = fsfilt_commit(obd, file->f_dentry->d_inode,
-                                            handle, 1);
-                        if (rc == 0)
-                                rc = err;
-                }
-                pop_ctxt(&saved, &obd->obd_ctxt, NULL);
-
-                if (rc)
-                        return rc;
-                CDEBUG(D_INFO, "wrote client mcd at idx %u off %llu (len %u)\n",
-                       med->med_idx, med->med_off,
-                       (unsigned int)sizeof(*med->med_mcd));
-        }
-        return 0;
-}
-
-int mds_client_free(struct obd_export *exp, int clear_client)
-{
-        struct mds_export_data *med = &exp->exp_mds_data;
-        struct mds_obd *mds = &exp->exp_obd->u.mds;
-        struct obd_device *obd = exp->exp_obd;
-        struct mds_client_data zero_mcd;
-        struct obd_run_ctxt saved;
-        int rc;
-        unsigned long *bitmap = mds->mds_client_bitmap;
-
-        LASSERT(bitmap);
-        if (!med->med_mcd)
-                RETURN(0);
-
-        /* XXX if mcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
-        if (!strcmp(med->med_mcd->mcd_uuid, obd->obd_uuid.uuid))
-                GOTO(free_and_out, 0);
-
-        CDEBUG(D_INFO, "freeing client at index %u (%lld)with UUID '%s'\n",
-               med->med_idx, med->med_off, med->med_mcd->mcd_uuid);
-
-        if (!test_and_clear_bit(med->med_idx, bitmap)) {
-                CERROR("MDS client %u: bit already clear in bitmap!!\n",
-                       med->med_idx);
-                LBUG();
-        }
-
-        if (clear_client) {
-                memset(&zero_mcd, 0, sizeof zero_mcd);
-                push_ctxt(&saved, &obd->obd_ctxt, NULL);
-                rc = fsfilt_write_record(obd, mds->mds_rcvd_filp,
-                                              &zero_mcd, sizeof(zero_mcd),
-                                              &med->med_off, 1);
-                pop_ctxt(&saved, &obd->obd_ctxt, NULL);
-
-                CDEBUG(rc == 0 ? D_INFO : D_ERROR,
-                       "zeroing out client %s off %u in %s rc %d\n",
-                       med->med_mcd->mcd_uuid, med->med_idx, LAST_RCVD, rc);
-        }
-
- free_and_out:
-        OBD_FREE(med->med_mcd, sizeof(*med->med_mcd));
-
-        return 0;
-}
-
-static int mds_server_free_data(struct mds_obd *mds)
-{
-        OBD_FREE(mds->mds_client_bitmap,
-                 MDS_MAX_CLIENT_WORDS * sizeof(unsigned long));
-        OBD_FREE(mds->mds_server_data, sizeof(*mds->mds_server_data));
-        mds->mds_server_data = NULL;
-
-        return 0;
-}
-
-static int mds_read_last_rcvd(struct obd_device *obd, struct file *file)
-{
-        struct mds_obd *mds = &obd->u.mds;
-        struct mds_server_data *msd;
-        struct mds_client_data *mcd = NULL;
-        loff_t off = 0;
-        unsigned long last_rcvd_size = file->f_dentry->d_inode->i_size;
-        __u64 last_transno = 0;
-        __u64 mount_count;
-        int cl_idx, rc = 0;
-        ENTRY;
-
-        LASSERT(sizeof(struct mds_client_data) == MDS_LR_CLIENT_SIZE);
-        LASSERT(sizeof(struct mds_server_data) <= MDS_LR_SERVER_SIZE);
-
-        OBD_ALLOC_WAIT(msd, sizeof(*msd));
-        if (!msd)
-                RETURN(-ENOMEM);
-
-        OBD_ALLOC_WAIT(mds->mds_client_bitmap,
-                  MDS_MAX_CLIENT_WORDS * sizeof(unsigned long));
-        if (!mds->mds_client_bitmap) {
-                OBD_FREE(msd, sizeof(*msd));
-                RETURN(-ENOMEM);
-        }
-
-        mds->mds_server_data = msd;
-
-        if (last_rcvd_size == 0) {
-                CWARN("%s: initializing new %s\n", obd->obd_name, LAST_RCVD);
-                memcpy(msd->msd_uuid, obd->obd_uuid.uuid,sizeof(msd->msd_uuid));
-                msd->msd_server_size = cpu_to_le32(MDS_LR_SERVER_SIZE);
-                msd->msd_client_start = cpu_to_le32(MDS_LR_CLIENT_START);
-                msd->msd_client_size = cpu_to_le16(MDS_LR_CLIENT_SIZE);
-                msd->msd_feature_rocompat = cpu_to_le32(MDS_ROCOMPAT_LOVOBJID);
-                rc = fsfilt_write_record(obd, file, msd, sizeof(*msd), &off, 1);
-
-                if (rc == 0)
-                        RETURN(0);
-
-                CERROR("%s: error writing new MSD: %d\n", obd->obd_name, rc);
-                GOTO(err_msd, rc);
-        }
-
-        rc = fsfilt_read_record(obd, file, msd, sizeof(*msd), &off);
-        if (rc) {
-                CERROR("error reading MDS %s: rc = %d\n", LAST_RCVD, rc);
-                GOTO(err_msd, rc);
-        }
-        if (!msd->msd_server_size)
-                msd->msd_server_size = cpu_to_le32(MDS_LR_SERVER_SIZE);
-        if (!msd->msd_client_start)
-                msd->msd_client_start = cpu_to_le32(MDS_LR_CLIENT_START);
-        if (!msd->msd_client_size)
-                msd->msd_client_size = cpu_to_le16(MDS_LR_CLIENT_SIZE);
-
-        if (msd->msd_feature_incompat & ~cpu_to_le32(MDS_INCOMPAT_SUPP)) {
-                CERROR("unsupported incompat feature %x\n",
-                       le32_to_cpu(msd->msd_feature_incompat) &
-                       ~MDS_INCOMPAT_SUPP);
-                GOTO(err_msd, rc = -EINVAL);
-        }
-        /* XXX updating existing b_devel fs only, can be removed in future */
-        msd->msd_feature_rocompat = cpu_to_le32(MDS_ROCOMPAT_LOVOBJID);
-        if (msd->msd_feature_rocompat & ~cpu_to_le32(MDS_ROCOMPAT_SUPP)) {
-                CERROR("unsupported read-only feature %x\n",
-                       le32_to_cpu(msd->msd_feature_rocompat) &
-                       ~MDS_ROCOMPAT_SUPP);
-                /* Do something like remount filesystem read-only */
-                GOTO(err_msd, rc = -EINVAL);
-        }
-
-        last_transno = le64_to_cpu(msd->msd_last_transno);
-        mds->mds_last_transno = last_transno;
-
-        mount_count = le64_to_cpu(msd->msd_mount_count);
-        mds->mds_mount_count = mount_count;
-
-        CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
-               obd->obd_name, last_transno);
-        CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
-               obd->obd_name, mount_count);
-        CDEBUG(D_INODE, "%s: server data size: %u\n",
-               obd->obd_name, le32_to_cpu(msd->msd_server_size));
-        CDEBUG(D_INODE, "%s: per-client data start: %u\n",
-               obd->obd_name, le32_to_cpu(msd->msd_client_start));
-        CDEBUG(D_INODE, "%s: per-client data size: %u\n",
-               obd->obd_name, le32_to_cpu(msd->msd_client_size));
-        CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
-               obd->obd_name, last_rcvd_size);
-        CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
-               last_rcvd_size <= MDS_LR_CLIENT_START ? 0 :
-               (last_rcvd_size - MDS_LR_CLIENT_START) / MDS_LR_CLIENT_SIZE);
-
-        /* When we do a clean MDS shutdown, we save the last_transno into
-         * the header.  If we find clients with higher last_transno values
-         * then those clients may need recovery done. */
-        for (cl_idx = 0, off = le32_to_cpu(msd->msd_client_start);
-             off < last_rcvd_size; cl_idx++) {
-                __u64 last_transno;
-                int mount_age;
-
-                if (!mcd) {
-                        OBD_ALLOC_WAIT(mcd, sizeof(*mcd));
-                        if (!mcd)
-                                GOTO(err_client, rc = -ENOMEM);
-                }
-
-                /* Don't assume off is incremented properly by
-                 * fsfilt_read_record(), in case sizeof(*mcd)
-                 * isn't the same as msd->msd_client_size.  */
-                off = le32_to_cpu(msd->msd_client_start) +
-                        cl_idx * le16_to_cpu(msd->msd_client_size);
-                rc = fsfilt_read_record(obd, file, mcd, sizeof(*mcd), &off);
-                if (rc) {
-                        CERROR("error reading MDS %s idx %d, off %llu: rc %d\n",
-                               LAST_RCVD, cl_idx, off, rc);
-                        break; /* read error shouldn't cause startup to fail */
-                }
-
-                if (mcd->mcd_uuid[0] == '\0') {
-                        CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
-                               cl_idx);
-                        continue;
-                }
-
-                last_transno = le64_to_cpu(mcd->mcd_last_transno);
-
-                /* These exports are cleaned up by mds_disconnect(), so they
-                 * need to be set up like real exports as mds_connect() does.
-                 */
-                mount_age = mount_count - le64_to_cpu(mcd->mcd_mount_count);
-                if (mount_age < MDS_MOUNT_RECOV) {
-                        struct obd_export *exp = class_new_export(obd);
-                        struct mds_export_data *med;
-                        CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
-                               " srv lr: "LPU64" mnt: "LPU64" last mount: "
-                               LPU64"\n", mcd->mcd_uuid, cl_idx,
-                               last_transno, le64_to_cpu(msd->msd_last_transno),
-                               le64_to_cpu(mcd->mcd_mount_count), mount_count);
-                        if (exp == NULL)
-                                GOTO(err_client, rc = -ENOMEM);
-
-                        memcpy(&exp->exp_client_uuid.uuid, mcd->mcd_uuid,
-                               sizeof exp->exp_client_uuid.uuid);
-                        med = &exp->exp_mds_data;
-                        med->med_mcd = mcd;
-                        mds_client_add(obd, mds, med, cl_idx);
-                        /* create helper if export init gets more complex */
-                        INIT_LIST_HEAD(&med->med_open_head);
-                        spin_lock_init(&med->med_open_lock);
-
-                        mcd = NULL;
-                        obd->obd_recoverable_clients++;
-                        obd->obd_max_recoverable_clients++;
-                        class_export_put(exp);
-                } else {
-                        CDEBUG(D_INFO, "discarded client %d, UUID '%s', count "
-                               LPU64"\n", cl_idx, mcd->mcd_uuid,
-                               le64_to_cpu(mcd->mcd_mount_count));
-                }
-
-                CDEBUG(D_OTHER, "client at idx %d has last_transno = "LPU64"\n",
-                       cl_idx, last_transno);
-
-                if (last_transno > mds->mds_last_transno)
-                        mds->mds_last_transno = last_transno;
-        }
-
-        obd->obd_last_committed = mds->mds_last_transno;
-        if (obd->obd_recoverable_clients) {
-                CWARN("RECOVERY: %d recoverable clients, last_transno "
-                       LPU64"\n", obd->obd_recoverable_clients,
-                       mds->mds_last_transno);
-                obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
-                obd->obd_recovering = 1;
-        }
-
-        if (mcd)
-                OBD_FREE(mcd, sizeof(*mcd));
-
-        return 0;
-
-err_client:
-        class_disconnect_exports(obd, 0);
-err_msd:
-        mds_server_free_data(mds);
-        return rc;
-}
-
-int mds_fs_setup(struct obd_device *obd, struct vfsmount *mnt)
-{
-        struct mds_obd *mds = &obd->u.mds;
-        struct obd_run_ctxt saved;
-        struct dentry *dentry;
-        struct file *file;
-        int rc;
-        ENTRY;
-
-
-        /* Get rid of unneeded supplementary groups */
-        current->ngroups = 0;
-        memset(current->groups, 0, sizeof(current->groups));
-
-        mds->mds_vfsmnt = mnt;
-        mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
-
-        fsfilt_setup(obd, mds->mds_sb);
-
-        OBD_SET_CTXT_MAGIC(&obd->obd_ctxt);
-        obd->obd_ctxt.pwdmnt = mnt;
-        obd->obd_ctxt.pwd = mnt->mnt_root;
-        obd->obd_ctxt.fs = get_ds();
-        obd->obd_ctxt.cb_ops = mds_lvfs_ops;
-
-        /* setup the directory tree */
-        push_ctxt(&saved, &obd->obd_ctxt, NULL);
-        dentry = simple_mkdir(current->fs->pwd, "ROOT", 0755);
-        if (IS_ERR(dentry)) {
-                rc = PTR_ERR(dentry);
-                CERROR("cannot create ROOT directory: rc = %d\n", rc);
-                GOTO(err_pop, rc);
-        }
-
-        mds->mds_rootfid.id = dentry->d_inode->i_ino;
-        mds->mds_rootfid.generation = dentry->d_inode->i_generation;
-        mds->mds_rootfid.f_type = S_IFDIR;
-
-        dput(dentry);
-
-        dentry = lookup_one_len("__iopen__", current->fs->pwd,
-                                strlen("__iopen__"));
-        if (IS_ERR(dentry) || !dentry->d_inode) {
-                rc = (IS_ERR(dentry)) ? PTR_ERR(dentry): -ENOENT;
-                CERROR("cannot open iopen FH directory: rc = %d\n", rc);
-                GOTO(err_pop, rc);
-        }
-        mds->mds_fid_de = dentry;
-
-        dentry = simple_mkdir(current->fs->pwd, "PENDING", 0777);
-        if (IS_ERR(dentry)) {
-                rc = PTR_ERR(dentry);
-                CERROR("cannot create PENDING directory: rc = %d\n", rc);
-                GOTO(err_fid, rc);
-        }
-        mds->mds_pending_dir = dentry;
-
-        dentry = simple_mkdir(current->fs->pwd, "LOGS", 0777);
-        if (IS_ERR(dentry)) {
-                rc = PTR_ERR(dentry);
-                CERROR("cannot create LOGS directory: rc = %d\n", rc);
-                GOTO(err_pending, rc);
-        }
-        mds->mds_logs_dir = dentry;
-
-        dentry = simple_mkdir(current->fs->pwd, "OBJECTS", 0777);
-        if (IS_ERR(dentry)) {
-                rc = PTR_ERR(dentry);
-                CERROR("cannot create OBJECTS directory: rc = %d\n", rc);
-                GOTO(err_logs, rc);
-        }
-        mds->mds_objects_dir = dentry;
-
-        /* open and test the last rcvd file */
-        file = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
-        if (IS_ERR(file)) {
-                rc = PTR_ERR(file);
-                CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
-                GOTO(err_objects, rc = PTR_ERR(file));
-        }
-        mds->mds_rcvd_filp = file;
-        if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
-                CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
-                       file->f_dentry->d_inode->i_mode);
-                GOTO(err_last_rcvd, rc = -ENOENT);
-        }
-
-        rc = mds_read_last_rcvd(obd, file);
-        if (rc) {
-                CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
-                GOTO(err_last_rcvd, rc);
-        }
-
-        /* open and test the lov objd file */
-        file = filp_open(LOV_OBJID, O_RDWR | O_CREAT, 0644);
-        if (IS_ERR(file)) {
-                rc = PTR_ERR(file);
-                CERROR("cannot open/create %s file: rc = %d\n", LOV_OBJID, rc);
-                GOTO(err_client, rc = PTR_ERR(file));
-        }
-        mds->mds_lov_objid_filp = file;
-        if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
-                CERROR("%s is not a regular file!: mode = %o\n", LOV_OBJID,
-                       file->f_dentry->d_inode->i_mode);
-                GOTO(err_lov_objid, rc = -ENOENT);
-        }
-err_pop:
-        pop_ctxt(&saved, &obd->obd_ctxt, NULL);
-
-        return rc;
-
-err_lov_objid:
-        if (mds->mds_lov_objid_filp && filp_close(mds->mds_lov_objid_filp, 0))
-                CERROR("can't close %s after error\n", LOV_OBJID);
-err_client:
-        class_disconnect_exports(obd, 0);
-err_last_rcvd:
-        if (mds->mds_rcvd_filp && filp_close(mds->mds_rcvd_filp, 0))
-                CERROR("can't close %s after error\n", LAST_RCVD);
-err_objects:
-        dput(mds->mds_objects_dir);
-err_logs:
-        dput(mds->mds_logs_dir);
-err_pending:
-        dput(mds->mds_pending_dir);
-err_fid:
-        dput(mds->mds_fid_de);
-        goto err_pop;
-}
-
-
-int mds_fs_cleanup(struct obd_device *obd, int flags)
-{
-        struct mds_obd *mds = &obd->u.mds;
-        struct obd_run_ctxt saved;
-        int rc = 0;
-
-        if (flags & OBD_OPT_FAILOVER)
-                CERROR("%s: shutting down for failover; client state will"
-                       " be preserved.\n", obd->obd_name);
-
-        class_disconnect_exports(obd, flags); /* cleans up client info too */
-        mds_server_free_data(mds);
-
-        push_ctxt(&saved, &obd->obd_ctxt, NULL);
-        if (mds->mds_rcvd_filp) {
-                rc = filp_close(mds->mds_rcvd_filp, 0);
-                mds->mds_rcvd_filp = NULL;
-                if (rc)
-                        CERROR("%s file won't close, rc=%d\n", LAST_RCVD, rc);
-        }
-        if (mds->mds_lov_objid_filp) {
-                rc = filp_close(mds->mds_lov_objid_filp, 0);
-                mds->mds_lov_objid_filp = NULL;
-                if (rc)
-                        CERROR("%s file won't close, rc=%d\n", LOV_OBJID, rc);
-        }
-        if (mds->mds_objects_dir != NULL) {
-                l_dput(mds->mds_objects_dir);
-                mds->mds_objects_dir = NULL;
-        }
-        if (mds->mds_logs_dir) {
-                l_dput(mds->mds_logs_dir);
-                mds->mds_logs_dir = NULL;
-        }
-        if (mds->mds_pending_dir) {
-                l_dput(mds->mds_pending_dir);
-                mds->mds_pending_dir = NULL;
-        }
-        pop_ctxt(&saved, &obd->obd_ctxt, NULL);
-        shrink_dcache_parent(mds->mds_fid_de);
-        dput(mds->mds_fid_de);
-
-        return rc;
-}
 
 /* Creates an object with the same name as its fid.  Because this is not at all
  * performance sensitive, it is accomplished by creating a file, checking the
  * fid, and renaming it. */
 int mds_obd_create(struct obd_export *exp, struct obdo *oa,
-                      struct lov_stripe_md **ea, struct obd_trans_info *oti)
+                   struct lov_stripe_md **ea, struct obd_trans_info *oti)
 {
         struct mds_obd *mds = &exp->exp_obd->u.mds;
         struct inode *parent_inode = mds->mds_objects_dir->d_inode;
-        unsigned int tmpname = ll_insecure_random_int();
+        unsigned int tmpname = ll_rand();
         struct file *filp;
         struct dentry *new_child;
-        struct obd_run_ctxt saved;
+        struct lvfs_run_ctxt saved;
         char fidname[LL_FID_NAMELEN];
         void *handle;
+        struct lvfs_ucred ucred = { 0 };
         int rc = 0, err, namelen;
         ENTRY;
 
-        push_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
-        
-        sprintf(fidname, "OBJECTS/%u", tmpname);
-        filp = filp_open(fidname, O_CREAT | O_EXCL, 0644);
+        /* the owner of object file should always be root */
+        cap_raise(ucred.luc_cap, CAP_SYS_RESOURCE);
+
+        if (strncmp(exp->exp_obd->obd_name, MDD_OBD_NAME,
+                                   strlen(MDD_OBD_NAME))) {
+                RETURN(0);
+        }
+
+        push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, &ucred);
+
+        sprintf(fidname, "OBJECTS/%u.%u", tmpname, current->pid);
+        filp = filp_open(fidname, O_CREAT | O_EXCL, 0666);
         if (IS_ERR(filp)) {
                 rc = PTR_ERR(filp);
                 if (rc == -EEXIST) {
@@ -605,7 +105,7 @@ int mds_obd_create(struct obd_export *exp, struct obdo *oa,
         oa->o_generation = filp->f_dentry->d_inode->i_generation;
         namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
 
-        down(&parent_inode->i_sem);
+        LOCK_INODE_MUTEX_PARENT(parent_inode);
         new_child = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
 
         if (IS_ERR(new_child)) {
@@ -620,12 +120,13 @@ int mds_obd_create(struct obd_export *exp, struct obdo *oa,
 
         handle = fsfilt_start(exp->exp_obd, mds->mds_objects_dir->d_inode,
                               FSFILT_OP_RENAME, NULL);
-        if (IS_ERR(handle)) 
+        if (IS_ERR(handle))
                 GOTO(out_dput, rc = PTR_ERR(handle));
-        
+
         lock_kernel();
-        rc = vfs_rename(mds->mds_objects_dir->d_inode, filp->f_dentry,
-                        mds->mds_objects_dir->d_inode, new_child);
+        rc = ll_vfs_rename(mds->mds_objects_dir->d_inode, filp->f_dentry,
+                           filp->f_vfsmnt, mds->mds_objects_dir->d_inode,
+                           new_child, filp->f_vfsmnt);
         unlock_kernel();
         if (rc)
                 CERROR("error renaming new object "LPU64":%u: rc %d\n",
@@ -633,14 +134,15 @@ int mds_obd_create(struct obd_export *exp, struct obdo *oa,
 
         err = fsfilt_commit(exp->exp_obd, mds->mds_objects_dir->d_inode,
                             handle, 0);
-        if (!err)
-                oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
-        else if (!rc)
+        if (!err) {
+                oa->o_gr = mdt_to_obd_objgrp(mds->mds_id);
+                oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FLGROUP;
+        } else if (!rc)
                 rc = err;
 out_dput:
         dput(new_child);
 out_close:
-        up(&parent_inode->i_sem);
+        UNLOCK_INODE_MUTEX(parent_inode);
         err = filp_close(filp, 0);
         if (err) {
                 CERROR("closing tmpfile %u: rc %d\n", tmpname, rc);
@@ -648,52 +150,74 @@ out_close:
                         rc = err;
         }
 out_pop:
-        pop_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
+        pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, &ucred);
         RETURN(rc);
 }
 
 int mds_obd_destroy(struct obd_export *exp, struct obdo *oa,
-                    struct lov_stripe_md *ea, struct obd_trans_info *oti)
+                    struct lov_stripe_md *ea, struct obd_trans_info *oti,
+                    struct obd_export *md_exp, void *capa)
 {
         struct mds_obd *mds = &exp->exp_obd->u.mds;
         struct inode *parent_inode = mds->mds_objects_dir->d_inode;
         struct obd_device *obd = exp->exp_obd;
-        struct obd_run_ctxt saved;
+        struct lvfs_run_ctxt saved;
+        struct lvfs_ucred ucred = { 0 };
         char fidname[LL_FID_NAMELEN];
+        struct inode *inode = NULL;
         struct dentry *de;
         void *handle;
         int err, namelen, rc = 0;
         ENTRY;
 
-        push_ctxt(&saved, &obd->obd_ctxt, NULL);
+        cap_raise(ucred.luc_cap, CAP_SYS_RESOURCE);
+        push_ctxt(&saved, &obd->obd_lvfs_ctxt, &ucred);
 
         namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
 
-        down(&parent_inode->i_sem);
+        LOCK_INODE_MUTEX_PARENT(parent_inode);
         de = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
-        if (de == NULL || de->d_inode == NULL) {
-                CERROR("destroying non-existent object "LPU64"\n", oa->o_id);
-                GOTO(out, rc = IS_ERR(de) ? PTR_ERR(de) : -ENOENT);
+        if (IS_ERR(de)) {
+                rc = IS_ERR(de);
+                de = NULL;
+                CERROR("error looking up object "LPU64" %s: rc %d\n",
+                       oa->o_id, fidname, rc);
+                GOTO(out_dput, rc);
+        }
+        if (de->d_inode == NULL) {
+                CERROR("destroying non-existent object "LPU64" %s: rc %d\n",
+                       oa->o_id, fidname, rc);
+                GOTO(out_dput, rc = -ENOENT);
         }
 
-        handle = fsfilt_start(obd, mds->mds_objects_dir->d_inode,
-                              FSFILT_OP_UNLINK_LOG, oti);
-        if (IS_ERR(handle)) {
+        /* Stripe count is 1 here since this is some MDS specific stuff
+           that is unlinked, not spanned across multiple OSTs */
+        handle = fsfilt_start_log(obd, mds->mds_objects_dir->d_inode,
+                                  FSFILT_OP_UNLINK, oti, 1);
+
+        if (IS_ERR(handle))
                 GOTO(out_dput, rc = PTR_ERR(handle));
-        }
-        
-        rc = vfs_unlink(mds->mds_objects_dir->d_inode, de);
-        if (rc) 
+
+        /* take a reference to protect inode from truncation within
+           vfs_unlink() context. bug 10409 */
+        inode = de->d_inode;
+        atomic_inc(&inode->i_count);
+        rc = ll_vfs_unlink(mds->mds_objects_dir->d_inode, de, mds->mds_vfsmnt);
+        if (rc)
                 CERROR("error destroying object "LPU64":%u: rc %d\n",
                        oa->o_id, oa->o_generation, rc);
-        
+
         err = fsfilt_commit(obd, mds->mds_objects_dir->d_inode, handle, 0);
         if (err && !rc)
                 rc = err;
 out_dput:
-        l_dput(de);
-out:
-        up(&parent_inode->i_sem);
-        pop_ctxt(&saved, &obd->obd_ctxt, NULL);
+        if (de != NULL)
+                l_dput(de);
+        UNLOCK_INODE_MUTEX(parent_inode);
+
+        if (inode)
+                iput(inode);
+
+        pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &ucred);
         RETURN(rc);
 }