/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=8:tabstop=8:
*
- * linux/mds/mds_fs.c
+ * GPL HEADER START
*
- * Lustre Metadata Server (MDS) filesystem interface code
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
- * Copyright (C) 2002 Cluster File Systems, Inc.
+ * 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.
*
- * This code is issued under the GNU General Public License.
- * See the file COPYING in this distribution
+ * 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).
*
- * by Andreas Dilger <adilger@clusterfs.com>
+ * 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
*
+ * 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>
*/
-#define EXPORT_SYMTAB
#define DEBUG_SUBSYSTEM S_MDS
#include <linux/module.h>
#include <linux/kmod.h>
-#include <linux/lustre_mds.h>
-
-LIST_HEAD(mds_fs_types);
-
-struct mds_fs_type {
- struct list_head mft_list;
- struct mds_fs_operations *mft_ops;
- char *mft_name;
-};
-
-/* This will be a hash table at some point. */
-static int mds_init_client_data(struct mds_obd *mds)
-{
- INIT_LIST_HEAD(&mds->mds_client_info);
- return 0;
-}
-
-#define MDS_MAX_CLIENTS 1024
-#define MDS_MAX_CLIENT_WORDS (MDS_MAX_CLIENTS / sizeof(unsigned long))
-
-static unsigned long last_rcvd_slots[MDS_MAX_CLIENT_WORDS];
-
-/* Add client data to the MDS. The in-memory storage will be a hash at some
- * point. 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 mds_obd *mds, struct mds_client_data *mcd, int cl_off)
+#include <linux/version.h>
+#include <linux/sched.h>
+#include <linux/mount.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"
+
+
+/* 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 mds_client_info *mci;
-
- OBD_ALLOC(mci, sizeof(*mci));
- if (!mci) {
- CERROR("no memory for MDS client info\n");
- RETURN(-ENOMEM);
- }
- INIT_LIST_HEAD(&mci->mci_open_head);
-
- CDEBUG(D_INFO, "client at offset %d with UUID '%s' added\n",
- cl_off, mcd->mcd_uuid);
-
- if (cl_off == -1) {
- unsigned long *word;
- int bit;
-
- repeat:
- word = last_rcvd_slots;
- while(*word == ~0UL)
- ++word;
- if (word - last_rcvd_slots >= MDS_MAX_CLIENT_WORDS) {
- CERROR("no room in client MDS bitmap - fix code\n");
- return -ENOMEM;
- }
- bit = ffz(*word);
- if (test_and_set_bit(bit, word)) {
- CERROR("found bit %d set for word %d - fix code\n",
- bit, word - last_rcvd_slots);
- goto repeat;
- }
- cl_off = word - last_rcvd_slots + bit;
- } else {
- if (test_and_set_bit(cl_off, last_rcvd_slots)) {
- CERROR("bit %d already set in bitmap - bad bad\n",
- cl_off);
- LBUG();
- }
- }
-
- mci->mci_mcd = mcd;
- mci->mci_off = cl_off;
-
- /* For now we just put the clients in a list, not a hashed list */
- list_add_tail(&mci->mci_list, &mds->mds_client_info);
-
- mds->mds_client_count++;
-
- return 0;
-}
-
-void mds_client_del(struct mds_obd *mds, struct mds_client_info *mci)
-{
- unsigned long *word;
- int bit;
-
- word = last_rcvd_slots + mci->mci_off / sizeof(unsigned long);
- bit = mci->mci_off % sizeof(unsigned long);
-
- if (!test_and_clear_bit(bit, word)) {
- CERROR("bit %d already clear in word %d - bad bad\n",
- bit, word - last_rcvd_slots);
- LBUG();
- }
-
- --mds->mds_client_count;
- list_del(&mci->mci_list);
- OBD_FREE(mci->mci_mcd, sizeof(*mci->mci_mcd));
- OBD_FREE(mci, sizeof (*mci));
-}
-
-static int mds_client_free_all(struct mds_obd *mds)
-{
- struct list_head *p, *n;
-
- list_for_each_safe(p, n, &mds->mds_client_info) {
- struct mds_client_info *mci;
-
- mci = list_entry(p, struct mds_client_info, mci_list);
- mds_client_del(mds, mci);
- }
-
- return 0;
-}
-
-static int mds_server_free_data(struct mds_obd *mds)
-{
- OBD_FREE(mds->mds_server_data, sizeof(*mds->mds_server_data));
- mds->mds_server_data = NULL;
-
- return 0;
-}
-
-#define LAST_RCVD "last_rcvd"
-
-static int mds_read_last_rcvd(struct mds_obd *mds, struct file *f)
-{
- struct mds_server_data *msd;
- struct mds_client_data *mcd = NULL;
- loff_t fsize = f->f_dentry->d_inode->i_size;
- loff_t off = 0;
- int cl_off;
- __u64 last_rcvd = 0;
- __u64 last_mount;
- int rc = 0;
-
- OBD_ALLOC(msd, sizeof(*msd));
- if (!msd)
- RETURN(-ENOMEM);
- rc = lustre_fread(f, (char *)msd, sizeof(*msd), &off);
-
- mds->mds_server_data = msd;
- if (rc == 0) {
- CERROR("empty MDS %s, new MDS?\n", LAST_RCVD);
+ struct mds_obd *mds = &exp->exp_obd->u.mds;
+ struct inode *parent_inode = mds->mds_objects_dir->d_inode;
+ unsigned int tmpname = ll_rand();
+ struct file *filp;
+ struct dentry *new_child;
+ struct lvfs_run_ctxt saved;
+ char fidname[LL_FID_NAMELEN];
+ void *handle;
+ struct lvfs_ucred ucred = { 0 };
+ int rc = 0, err, namelen;
+ ENTRY;
+
+ /* 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);
- } else if (rc != sizeof(*msd)) {
- CERROR("error reading MDS %s: rc = %d\n", LAST_RCVD, rc);
- if (rc > 0) {
- rc = -EIO;
- }
- GOTO(err_msd, rc);
}
- /*
- * When we do a clean MDS shutdown, we save the last_rcvd into
- * the header. If we find clients with higher last_rcvd values
- * then those clients may need recovery done.
- */
- last_rcvd = le64_to_cpu(msd->msd_last_rcvd);
- mds->mds_last_rcvd = last_rcvd;
- CDEBUG(D_INODE, "got %Lu for server last_rcvd value\n",
- (unsigned long long)last_rcvd);
-
- last_mount = le64_to_cpu(msd->msd_mount_count);
- mds->mds_mount_count = last_mount;
- CDEBUG(D_INODE, "got %Lu for server last_mount value\n",
- (unsigned long long)last_mount);
+ push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, &ucred);
- for (off = MDS_LR_CLIENT, cl_off = 0, rc = sizeof(*mcd);
- off <= fsize - sizeof(*mcd) && rc == sizeof(*mcd);
- off = MDS_LR_CLIENT + ++cl_off * MDS_LR_SIZE) {
- if (!mcd)
- OBD_ALLOC(mcd, sizeof(*mcd));
- if (!mcd)
- GOTO(err_msd, rc = -ENOMEM);
-
- rc = lustre_fread(f, (char *)mcd, sizeof(*mcd), &off);
- if (rc != sizeof(*mcd)) {
- CERROR("error reading MDS %s offset %d: rc = %d\n",
- LAST_RCVD, cl_off, rc);
- if (rc > 0)
- rc = -EIO;
- break;
- }
-
- last_rcvd = le64_to_cpu(mcd->mcd_last_rcvd);
- last_mount = le64_to_cpu(mcd->mcd_mount_count);
-
- if (last_rcvd &&
- last_mount - mcd->mcd_mount_count < MDS_MOUNT_RECOV) {
- rc = mds_client_add(mds, mcd, cl_off);
- if (rc) {
- rc = 0;
- break;
- }
- mcd = NULL;
- } else {
- CDEBUG(D_INFO,
- "client at offset %d with UUID '%s' ignored\n",
- cl_off, mcd->mcd_uuid);
- }
-
- if (last_rcvd > mds->mds_last_rcvd) {
- CDEBUG(D_OTHER,
- "client at offset %d has last_rcvd = %Lu\n",
- cl_off, (unsigned long long)last_rcvd);
- mds->mds_last_rcvd = last_rcvd;
+ 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) {
+ CERROR("impossible object name collision %u\n",
+ tmpname);
+ LBUG();
}
+ CERROR("error creating tmp object %u: rc %d\n", tmpname, rc);
+ GOTO(out_pop, rc);
}
- CDEBUG(D_INODE, "got %Lu for highest last_rcvd value, %d clients\n",
- (unsigned long long)mds->mds_last_rcvd, mds->mds_client_count);
- /* After recovery, there can be no local uncommitted transactions */
- mds->mds_last_committed = mds->mds_last_rcvd;
+ LASSERT(mds->mds_objects_dir == filp->f_dentry->d_parent);
- return 0;
-
-err_msd:
- mds_server_free_data(mds);
- return rc;
-}
+ oa->o_id = filp->f_dentry->d_inode->i_ino;
+ oa->o_generation = filp->f_dentry->d_inode->i_generation;
+ namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
-static int mds_fs_prep(struct mds_obd *mds)
-{
- struct obd_run_ctxt saved;
- struct dentry *dentry;
- struct file *f;
- int rc;
+ LOCK_INODE_MUTEX_PARENT(parent_inode);
+ new_child = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
- push_ctxt(&saved, &mds->mds_ctxt);
- 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);
+ if (IS_ERR(new_child)) {
+ CERROR("getting neg dentry for obj rename: %d\n", rc);
+ GOTO(out_close, rc = PTR_ERR(new_child));
}
- /* XXX probably want to hold on to this later... */
- dput(dentry);
- f = filp_open("ROOT", O_RDONLY, 0);
- if (IS_ERR(f)) {
- rc = PTR_ERR(f);
- CERROR("cannot open ROOT: rc = %d\n", rc);
+ if (new_child->d_inode != NULL) {
+ CERROR("impossible non-negative obj dentry " LPU64":%u!\n",
+ oa->o_id, oa->o_generation);
LBUG();
- GOTO(err_pop, rc);
}
- mds->mds_rootfid.id = f->f_dentry->d_inode->i_ino;
- mds->mds_rootfid.generation = f->f_dentry->d_inode->i_generation;
- mds->mds_rootfid.f_type = S_IFDIR;
-
- rc = filp_close(f, 0);
- if (rc) {
- CERROR("cannot close ROOT: rc = %d\n", rc);
- LBUG();
- }
+ handle = fsfilt_start(exp->exp_obd, mds->mds_objects_dir->d_inode,
+ FSFILT_OP_RENAME, NULL);
+ if (IS_ERR(handle))
+ GOTO(out_dput, rc = PTR_ERR(handle));
- dentry = simple_mkdir(current->fs->pwd, "FH", 0700);
- if (IS_ERR(dentry)) {
- rc = PTR_ERR(dentry);
- CERROR("cannot create FH directory: rc = %d\n", rc);
- GOTO(err_pop, rc);
- }
- /* XXX probably want to hold on to this later... */
- dput(dentry);
-
- rc = mds_init_client_data(mds);
+ lock_kernel();
+ 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)
- GOTO(err_pop, rc);
-
- f = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
- if (IS_ERR(f)) {
- rc = PTR_ERR(f);
- CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
- GOTO(err_pop, rc = PTR_ERR(f));
- }
- if (!S_ISREG(f->f_dentry->d_inode->i_mode)) {
- CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
- f->f_dentry->d_inode->i_mode);
- GOTO(err_pop, rc = -ENOENT);
- }
-
- rc = mds_fs_journal_data(mds, f);
- if (rc) {
- CERROR("cannot journal data on %s: rc = %d\n", LAST_RCVD, rc);
- GOTO(err_filp, rc);
- }
-
- rc = mds_read_last_rcvd(mds, f);
- if (rc) {
- CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
- GOTO(err_client, rc);
- }
- mds->mds_rcvd_filp = f;
- pop_ctxt(&saved);
-
- RETURN(0);
-
-err_client:
- mds_client_free_all(mds);
-err_filp:
- if (filp_close(f, 0))
- CERROR("can't close %s after error\n", LAST_RCVD);
-err_pop:
- pop_ctxt(&saved);
-
- return rc;
-}
-
-static struct mds_fs_operations *mds_search_fs_type(const char *name)
-{
- struct list_head *p;
- struct mds_fs_type *type;
-
- /* lock mds_fs_types list */
- list_for_each(p, &mds_fs_types) {
- type = list_entry(p, struct mds_fs_type, mft_list);
- if (!strcmp(type->mft_name, name)) {
- /* unlock mds_fs_types list */
- return type->mft_ops;
- }
- }
- /* unlock mds_fs_types list */
- return NULL;
-}
-
-int mds_register_fs_type(struct mds_fs_operations *ops, const char *name)
-{
- struct mds_fs_operations *found;
- struct mds_fs_type *type;
-
- if ((found = mds_search_fs_type(name))) {
- if (found != ops) {
- CERROR("different operations for type %s\n", name);
- RETURN(-EEXIST);
- }
- return 0;
- }
- OBD_ALLOC(type, sizeof(*type));
- if (!type)
- RETURN(-ENOMEM);
-
- INIT_LIST_HEAD(&type->mft_list);
- type->mft_ops = ops;
- type->mft_name = strdup(name);
- if (!type->mft_name) {
- OBD_FREE(type, sizeof(*type));
- RETURN(-ENOMEM);
- }
- MOD_INC_USE_COUNT;
- list_add(&type->mft_list, &mds_fs_types);
-
- return 0;
+ CERROR("error renaming new object "LPU64":%u: rc %d\n",
+ oa->o_id, oa->o_generation, rc);
+
+ err = fsfilt_commit(exp->exp_obd, mds->mds_objects_dir->d_inode,
+ handle, 0);
+ 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:
+ UNLOCK_INODE_MUTEX(parent_inode);
+ err = filp_close(filp, 0);
+ if (err) {
+ CERROR("closing tmpfile %u: rc %d\n", tmpname, rc);
+ if (!rc)
+ rc = err;
+ }
+out_pop:
+ pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, &ucred);
+ RETURN(rc);
}
-void mds_unregister_fs_type(const char *name)
+int mds_obd_destroy(struct obd_export *exp, struct obdo *oa,
+ struct lov_stripe_md *ea, struct obd_trans_info *oti,
+ struct obd_export *md_exp, void *capa)
{
- struct list_head *p;
-
- /* lock mds_fs_types list */
- list_for_each(p, &mds_fs_types) {
- struct mds_fs_type *type;
-
- type = list_entry(p, struct mds_fs_type, mft_list);
- if (!strcmp(type->mft_name, name)) {
- list_del(p);
- kfree(type->mft_name);
- OBD_FREE(type, sizeof(*type));
- MOD_DEC_USE_COUNT;
- break;
- }
- }
- /* unlock mds_fs_types list */
-}
-
-int mds_fs_setup(struct mds_obd *mds, struct vfsmount *mnt)
-{
- struct mds_fs_operations *fs_ops;
- int rc;
-
- if (!(fs_ops = mds_search_fs_type(mds->mds_fstype))) {
- char name[32];
-
- snprintf(name, sizeof(name) - 1, "mds_%s", mds->mds_fstype);
- name[sizeof(name) - 1] = '\0';
-
- if ((rc = request_module(name))) {
- fs_ops = mds_search_fs_type(mds->mds_fstype);
- CDEBUG(D_INFO, "Loaded module '%s'\n", name);
- if (!fs_ops)
- rc = -ENOENT;
- }
-
- if (rc) {
- CERROR("Can't find MDS fs interface '%s'\n", name);
- RETURN(rc);
- }
- }
-
- mds->mds_fsops = fs_ops;
- mds->mds_vfsmnt = mnt;
- mds->mds_ctxt.pwdmnt = mnt;
- mds->mds_ctxt.pwd = mnt->mnt_root;
- mds->mds_ctxt.fs = KERNEL_DS;
-
- /*
- * Replace the client filesystem delete_inode method with our own,
- * so that we can clear the object ID before the inode is deleted.
- * The fs_delete_inode method will call cl_delete_inode for us.
- * We need to do this for the MDS superblock only, hence we install
- * a modified copy of the original superblock method table.
- *
- * We still assume that there is only a single MDS client filesystem
- * type, as we don't have access to the mds struct in delete_inode
- * and store the client delete_inode method in a global table. This
- * will only become a problem if/when multiple MDSs are running on a
- * single host with different underlying filesystems.
- */
- OBD_ALLOC(mds->mds_sop, sizeof(*mds->mds_sop));
- if (!mds->mds_sop)
- RETURN(-ENOMEM);
-
- memcpy(mds->mds_sop, mds->mds_sb->s_op, sizeof(*mds->mds_sop));
- mds->mds_fsops->cl_delete_inode = mds->mds_sop->delete_inode;
- mds->mds_sop->delete_inode = mds->mds_fsops->fs_delete_inode;
- mds->mds_sb->s_op = mds->mds_sop;
-
- rc = mds_fs_prep(mds);
-
+ 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 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;
+
+ 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);
+
+ LOCK_INODE_MUTEX_PARENT(parent_inode);
+ de = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
+ 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);
+ }
+
+ /* 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));
+
+ /* 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)
- OBD_FREE(mds->mds_sop, sizeof(*mds->mds_sop));
-
- return 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:
+ 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);
}
-
-void mds_fs_cleanup(struct mds_obd *mds)
-{
- mds_client_free_all(mds);
- mds_server_free_data(mds);
-
- OBD_FREE(mds->mds_sop, sizeof(*mds->mds_sop));
-}
-
-EXPORT_SYMBOL(mds_register_fs_type);
-EXPORT_SYMBOL(mds_unregister_fs_type);