Whamcloud - gitweb
Introduce and use new LOCK_INODE_MUTEX_PARENT() macro to be used in the
[fs/lustre-release.git] / lustre / mds / mds_fs.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/mds/mds_fs.c
37  *
38  * Lustre Metadata Server (MDS) filesystem interface code
39  *
40  * Author: Andreas Dilger <adilger@clusterfs.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_MDS
44
45 #include <linux/module.h>
46 #include <linux/kmod.h>
47 #include <linux/version.h>
48 #include <linux/sched.h>
49 #include <lustre_quota.h>
50 #include <linux/mount.h>
51 #include <lustre_mds.h>
52 #include <obd_class.h>
53 #include <obd_support.h>
54 #include <lustre_lib.h>
55 #include <lustre_fsfilt.h>
56 #include <lustre_disk.h>
57 #include <libcfs/list.h>
58
59 #include "mds_internal.h"
60
61
62 /* Creates an object with the same name as its fid.  Because this is not at all
63  * performance sensitive, it is accomplished by creating a file, checking the
64  * fid, and renaming it. */
65 int mds_obd_create(struct obd_export *exp, struct obdo *oa,
66                    struct lov_stripe_md **ea, struct obd_trans_info *oti)
67 {
68         struct mds_obd *mds = &exp->exp_obd->u.mds;
69         struct inode *parent_inode = mds->mds_objects_dir->d_inode;
70         unsigned int tmpname = ll_rand();
71         struct file *filp;
72         struct dentry *new_child;
73         struct lvfs_run_ctxt saved;
74         char fidname[LL_FID_NAMELEN];
75         void *handle;
76         struct lvfs_ucred ucred = { 0 };
77         int rc = 0, err, namelen;
78         ENTRY;
79
80         /* the owner of object file should always be root */
81         cap_raise(ucred.luc_cap, CAP_SYS_RESOURCE);
82
83         if (strncmp(exp->exp_obd->obd_name, MDD_OBD_NAME,
84                                    strlen(MDD_OBD_NAME))) {
85                 RETURN(0);
86         }
87         
88         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, &ucred);
89
90         sprintf(fidname, "OBJECTS/%u.%u", tmpname, current->pid);
91         filp = filp_open(fidname, O_CREAT | O_EXCL, 0666);
92         if (IS_ERR(filp)) {
93                 rc = PTR_ERR(filp);
94                 if (rc == -EEXIST) {
95                         CERROR("impossible object name collision %u\n",
96                                tmpname);
97                         LBUG();
98                 }
99                 CERROR("error creating tmp object %u: rc %d\n", tmpname, rc);
100                 GOTO(out_pop, rc);
101         }
102
103         LASSERT(mds->mds_objects_dir == filp->f_dentry->d_parent);
104
105         oa->o_id = filp->f_dentry->d_inode->i_ino;
106         oa->o_generation = filp->f_dentry->d_inode->i_generation;
107         namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
108
109         LOCK_INODE_MUTEX_PARENT(parent_inode);
110         new_child = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
111
112         if (IS_ERR(new_child)) {
113                 CERROR("getting neg dentry for obj rename: %d\n", rc);
114                 GOTO(out_close, rc = PTR_ERR(new_child));
115         }
116         if (new_child->d_inode != NULL) {
117                 CERROR("impossible non-negative obj dentry " LPU64":%u!\n",
118                        oa->o_id, oa->o_generation);
119                 LBUG();
120         }
121
122         handle = fsfilt_start(exp->exp_obd, mds->mds_objects_dir->d_inode,
123                               FSFILT_OP_RENAME, NULL);
124         if (IS_ERR(handle))
125                 GOTO(out_dput, rc = PTR_ERR(handle));
126
127         lock_kernel();
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);
131         unlock_kernel();
132         if (rc)
133                 CERROR("error renaming new object "LPU64":%u: rc %d\n",
134                        oa->o_id, oa->o_generation, rc);
135
136         err = fsfilt_commit(exp->exp_obd, mds->mds_objects_dir->d_inode,
137                             handle, 0);
138         if (!err) {
139                 oa->o_gr = FILTER_GROUP_MDS0 + mds->mds_id;
140                 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FLGROUP;
141         } else if (!rc)
142                 rc = err;
143 out_dput:
144         dput(new_child);
145 out_close:
146         UNLOCK_INODE_MUTEX(parent_inode);
147         err = filp_close(filp, 0);
148         if (err) {
149                 CERROR("closing tmpfile %u: rc %d\n", tmpname, rc);
150                 if (!rc)
151                         rc = err;
152         }
153 out_pop:
154         pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, &ucred);
155         RETURN(rc);
156 }
157
158 int mds_obd_destroy(struct obd_export *exp, struct obdo *oa,
159                     struct lov_stripe_md *ea, struct obd_trans_info *oti,
160                     struct obd_export *md_exp)
161 {
162         struct mds_obd *mds = &exp->exp_obd->u.mds;
163         struct inode *parent_inode = mds->mds_objects_dir->d_inode;
164         struct obd_device *obd = exp->exp_obd;
165         struct lvfs_run_ctxt saved;
166         struct lvfs_ucred ucred = { 0 };
167         char fidname[LL_FID_NAMELEN];
168         struct inode *inode = NULL;
169         struct dentry *de;
170         void *handle;
171         int err, namelen, rc = 0;
172         ENTRY;
173
174         cap_raise(ucred.luc_cap, CAP_SYS_RESOURCE);
175         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &ucred);
176
177         namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
178
179         LOCK_INODE_MUTEX_PARENT(parent_inode);
180         de = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
181         if (IS_ERR(de)) {
182                 rc = IS_ERR(de);
183                 de = NULL;
184                 CERROR("error looking up object "LPU64" %s: rc %d\n",
185                        oa->o_id, fidname, rc);
186                 GOTO(out_dput, rc);
187         }
188         if (de->d_inode == NULL) {
189                 CERROR("destroying non-existent object "LPU64" %s: rc %d\n",
190                        oa->o_id, fidname, rc);
191                 GOTO(out_dput, rc = -ENOENT);
192         }
193
194         /* Stripe count is 1 here since this is some MDS specific stuff
195            that is unlinked, not spanned across multiple OSTs */
196         handle = fsfilt_start_log(obd, mds->mds_objects_dir->d_inode,
197                                   FSFILT_OP_UNLINK, oti, 1);
198
199         if (IS_ERR(handle))
200                 GOTO(out_dput, rc = PTR_ERR(handle));
201
202         /* take a reference to protect inode from truncation within
203            vfs_unlink() context. bug 10409 */
204         inode = de->d_inode;
205         atomic_inc(&inode->i_count);
206         rc = ll_vfs_unlink(mds->mds_objects_dir->d_inode, de, mds->mds_vfsmnt);
207         if (rc)
208                 CERROR("error destroying object "LPU64":%u: rc %d\n",
209                        oa->o_id, oa->o_generation, rc);
210
211         err = fsfilt_commit(obd, mds->mds_objects_dir->d_inode, handle, 0);
212         if (err && !rc)
213                 rc = err;
214 out_dput:
215         if (de != NULL)
216                 l_dput(de);
217         UNLOCK_INODE_MUTEX(parent_inode);
218
219         if (inode)
220                 iput(inode);
221
222         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &ucred);
223         RETURN(rc);
224 }