4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
36 * Lustre Metadata Server (MDS) filesystem interface code
38 * Author: Andreas Dilger <adilger@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_MDS
43 #include <linux/module.h>
44 #include <linux/kmod.h>
45 #include <linux/version.h>
46 #include <linux/sched.h>
47 #include <linux/mount.h>
48 #include <lustre_mds.h>
49 #include <obd_class.h>
50 #include <obd_support.h>
51 #include <lustre_lib.h>
52 #include <lustre_fsfilt.h>
53 #include <lustre_disk.h>
54 #include <libcfs/list.h>
56 #include "mds_internal.h"
59 /* Creates an object with the same name as its fid. Because this is not at all
60 * performance sensitive, it is accomplished by creating a file, checking the
61 * fid, and renaming it. */
62 int mds_obd_create(const struct lu_env *env, struct obd_export *exp,
63 struct obdo *oa, struct lov_stripe_md **ea,
64 struct obd_trans_info *oti)
66 struct mds_obd *mds = &exp->exp_obd->u.mds;
67 struct inode *parent_inode = mds->mds_objects_dir->d_inode;
68 unsigned int tmpname = cfs_rand();
70 struct dentry *new_child;
71 struct lvfs_run_ctxt saved;
72 char fidname[LL_FID_NAMELEN];
74 struct lvfs_ucred ucred = { 0 };
75 int rc = 0, err, namelen;
78 /* the owner of object file should always be root */
79 cap_raise(ucred.luc_cap, CAP_SYS_RESOURCE);
81 if (strncmp(exp->exp_obd->obd_name, MDD_OBD_NAME,
82 strlen(MDD_OBD_NAME))) {
86 push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, &ucred);
88 sprintf(fidname, "OBJECTS/%u.%u", tmpname, current->pid);
89 filp = filp_open(fidname, O_CREAT | O_EXCL, 0666);
93 CERROR("impossible object name collision %u\n",
97 CERROR("error creating tmp object %u: rc %d\n", tmpname, rc);
101 LASSERT(mds->mds_objects_dir == filp->f_dentry->d_parent);
103 /* FIXME: need to see how this should change to properly store FID
104 * into obdo (o_id == OID, o_seq = SEQ) (maybe as IGIF?). */
105 #define o_generation o_parent_oid
106 oa->o_id = filp->f_dentry->d_inode->i_ino;
107 oa->o_generation = filp->f_dentry->d_inode->i_generation;
108 namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
110 LOCK_INODE_MUTEX_PARENT(parent_inode);
111 new_child = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
113 if (IS_ERR(new_child)) {
114 CERROR("getting neg dentry for obj rename: %d\n", rc);
115 GOTO(out_close, rc = PTR_ERR(new_child));
117 if (new_child->d_inode != NULL) {
118 CERROR("impossible non-negative obj dentry " LPU64":%u!\n",
119 oa->o_id, oa->o_generation);
123 handle = fsfilt_start(exp->exp_obd, mds->mds_objects_dir->d_inode,
124 FSFILT_OP_RENAME, NULL);
126 GOTO(out_dput, rc = PTR_ERR(handle));
128 rc = ll_vfs_rename(mds->mds_objects_dir->d_inode, filp->f_dentry,
129 filp->f_vfsmnt, mds->mds_objects_dir->d_inode,
130 new_child, filp->f_vfsmnt);
133 CERROR("error renaming new object "LPU64":%u: rc %d\n",
134 oa->o_id, oa->o_generation, rc);
136 err = fsfilt_commit(exp->exp_obd, mds->mds_objects_dir->d_inode,
139 oa->o_seq = mdt_to_obd_objseq(mds->mds_id);
140 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FLGROUP;
146 UNLOCK_INODE_MUTEX(parent_inode);
147 err = filp_close(filp, 0);
149 CERROR("closing tmpfile %u: rc %d\n", tmpname, rc);
154 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, &ucred);
158 int mds_obd_destroy(const struct lu_env *env, struct obd_export *exp,
159 struct obdo *oa, struct lov_stripe_md *ea,
160 struct obd_trans_info *oti, struct obd_export *md_exp,
163 struct mds_obd *mds = &exp->exp_obd->u.mds;
164 struct inode *parent_inode = mds->mds_objects_dir->d_inode;
165 struct obd_device *obd = exp->exp_obd;
166 struct lvfs_run_ctxt saved;
167 struct lvfs_ucred ucred = { 0 };
168 char fidname[LL_FID_NAMELEN];
169 struct inode *inode = NULL;
172 int err, namelen, rc = 0;
175 cap_raise(ucred.luc_cap, CAP_SYS_RESOURCE);
176 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &ucred);
178 namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
180 LOCK_INODE_MUTEX_PARENT(parent_inode);
181 de = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
185 CERROR("error looking up object "LPU64" %s: rc %d\n",
186 oa->o_id, fidname, rc);
189 if (de->d_inode == NULL) {
190 CERROR("destroying non-existent object "LPU64" %s: rc %d\n",
191 oa->o_id, fidname, rc);
192 GOTO(out_dput, rc = -ENOENT);
195 /* Stripe count is 1 here since this is some MDS specific stuff
196 that is unlinked, not spanned across multiple OSTs */
197 handle = fsfilt_start_log(obd, mds->mds_objects_dir->d_inode,
198 FSFILT_OP_UNLINK, oti, 1);
201 GOTO(out_dput, rc = PTR_ERR(handle));
203 /* take a reference to protect inode from truncation within
204 vfs_unlink() context. bug 10409 */
206 atomic_inc(&inode->i_count);
207 rc = ll_vfs_unlink(mds->mds_objects_dir->d_inode, de,
208 mds->mds_obt.obt_vfsmnt);
210 CERROR("error destroying object "LPU64":%u: rc %d\n",
211 oa->o_id, oa->o_generation, rc);
214 err = fsfilt_commit(obd, mds->mds_objects_dir->d_inode, handle, 0);
220 UNLOCK_INODE_MUTEX(parent_inode);
225 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &ucred);