1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * Lustre Metadata Server (MDS) filesystem interface code
7 * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
8 * Author: Andreas Dilger <adilger@clusterfs.com>
10 * This file is part of the Lustre file system, http://www.lustre.org
11 * Lustre is a trademark of Cluster File Systems, Inc.
13 * You may have signed or agreed to another license before downloading
14 * this software. If so, you are bound by the terms and conditions
15 * of that agreement, and the following does not apply to you. See the
16 * LICENSE file included with this distribution for more information.
18 * If you did not agree to a different license, then this copy of Lustre
19 * is open source software; you can redistribute it and/or modify it
20 * under the terms of version 2 of the GNU General Public License as
21 * published by the Free Software Foundation.
23 * In either case, Lustre is distributed in the hope that it will be
24 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
25 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * license text for more details.
29 #define DEBUG_SUBSYSTEM S_MDS
31 #include <linux/module.h>
32 #include <linux/kmod.h>
33 #include <linux/version.h>
34 #include <linux/sched.h>
35 #include <lustre_quota.h>
36 #include <linux/mount.h>
37 #include <lustre_mds.h>
38 #include <obd_class.h>
39 #include <obd_support.h>
40 #include <lustre_lib.h>
41 #include <lustre_fsfilt.h>
42 #include <lustre_disk.h>
43 #include <libcfs/list.h>
45 #include "mds_internal.h"
48 /* Creates an object with the same name as its fid. Because this is not at all
49 * performance sensitive, it is accomplished by creating a file, checking the
50 * fid, and renaming it. */
51 int mds_obd_create(struct obd_export *exp, struct obdo *oa,
52 struct lov_stripe_md **ea, struct obd_trans_info *oti)
54 struct mds_obd *mds = &exp->exp_obd->u.mds;
55 struct inode *parent_inode = mds->mds_objects_dir->d_inode;
56 unsigned int tmpname = ll_rand();
58 struct dentry *new_child;
59 struct lvfs_run_ctxt saved;
60 char fidname[LL_FID_NAMELEN];
62 struct lvfs_ucred ucred = { 0 };
63 int rc = 0, err, namelen;
66 /* the owner of object file should always be root */
67 cap_raise(ucred.luc_cap, CAP_SYS_RESOURCE);
69 if (strncmp(exp->exp_obd->obd_name, MDD_OBD_NAME,
70 strlen(MDD_OBD_NAME))) {
74 push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, &ucred);
76 sprintf(fidname, "OBJECTS/%u.%u", tmpname, current->pid);
77 filp = filp_open(fidname, O_CREAT | O_EXCL, 0666);
81 CERROR("impossible object name collision %u\n",
85 CERROR("error creating tmp object %u: rc %d\n", tmpname, rc);
89 LASSERT(mds->mds_objects_dir == filp->f_dentry->d_parent);
91 oa->o_id = filp->f_dentry->d_inode->i_ino;
92 oa->o_generation = filp->f_dentry->d_inode->i_generation;
93 namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
95 LOCK_INODE_MUTEX(parent_inode);
96 new_child = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
98 if (IS_ERR(new_child)) {
99 CERROR("getting neg dentry for obj rename: %d\n", rc);
100 GOTO(out_close, rc = PTR_ERR(new_child));
102 if (new_child->d_inode != NULL) {
103 CERROR("impossible non-negative obj dentry " LPU64":%u!\n",
104 oa->o_id, oa->o_generation);
108 handle = fsfilt_start(exp->exp_obd, mds->mds_objects_dir->d_inode,
109 FSFILT_OP_RENAME, NULL);
111 GOTO(out_dput, rc = PTR_ERR(handle));
114 rc = vfs_rename(mds->mds_objects_dir->d_inode, filp->f_dentry,
115 mds->mds_objects_dir->d_inode, new_child);
118 CERROR("error renaming new object "LPU64":%u: rc %d\n",
119 oa->o_id, oa->o_generation, rc);
121 err = fsfilt_commit(exp->exp_obd, mds->mds_objects_dir->d_inode,
124 oa->o_gr = FILTER_GROUP_MDS0 + mds->mds_id;
125 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FLGROUP;
131 UNLOCK_INODE_MUTEX(parent_inode);
132 err = filp_close(filp, 0);
134 CERROR("closing tmpfile %u: rc %d\n", tmpname, rc);
139 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, &ucred);
143 int mds_obd_destroy(struct obd_export *exp, struct obdo *oa,
144 struct lov_stripe_md *ea, struct obd_trans_info *oti,
145 struct obd_export *md_exp)
147 struct mds_obd *mds = &exp->exp_obd->u.mds;
148 struct inode *parent_inode = mds->mds_objects_dir->d_inode;
149 struct obd_device *obd = exp->exp_obd;
150 struct lvfs_run_ctxt saved;
151 struct lvfs_ucred ucred = { 0 };
152 char fidname[LL_FID_NAMELEN];
153 struct inode *inode = NULL;
156 int err, namelen, rc = 0;
159 cap_raise(ucred.luc_cap, CAP_SYS_RESOURCE);
160 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &ucred);
162 namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
164 LOCK_INODE_MUTEX(parent_inode);
165 de = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
169 CERROR("error looking up object "LPU64" %s: rc %d\n",
170 oa->o_id, fidname, rc);
173 if (de->d_inode == NULL) {
174 CERROR("destroying non-existent object "LPU64" %s: rc %d\n",
175 oa->o_id, fidname, rc);
176 GOTO(out_dput, rc = -ENOENT);
179 /* Stripe count is 1 here since this is some MDS specific stuff
180 that is unlinked, not spanned across multiple OSTs */
181 handle = fsfilt_start_log(obd, mds->mds_objects_dir->d_inode,
182 FSFILT_OP_UNLINK, oti, 1);
185 GOTO(out_dput, rc = PTR_ERR(handle));
187 /* take a reference to protect inode from truncation within
188 vfs_unlink() context. bug 10409 */
190 atomic_inc(&inode->i_count);
191 rc = vfs_unlink(mds->mds_objects_dir->d_inode, de);
193 CERROR("error destroying object "LPU64":%u: rc %d\n",
194 oa->o_id, oa->o_generation, rc);
196 err = fsfilt_commit(obd, mds->mds_objects_dir->d_inode, handle, 0);
202 UNLOCK_INODE_MUTEX(parent_inode);
207 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &ucred);