Whamcloud - gitweb
eba74270cf2d62b0e48ce0d1c03d044b359d715a
[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 (c) 2002, 2010, Oracle and/or its affiliates. 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 <linux/mount.h>
50 #include <lustre_mds.h>
51 #include <obd_class.h>
52 #include <obd_support.h>
53 #include <lustre_lib.h>
54 #include <lustre_fsfilt.h>
55 #include <lustre_disk.h>
56 #include <libcfs/list.h>
57
58 #include "mds_internal.h"
59
60
61 /* Creates an object with the same name as its fid.  Because this is not at all
62  * performance sensitive, it is accomplished by creating a file, checking the
63  * fid, and renaming it. */
64 int mds_obd_create(struct obd_export *exp, struct obdo *oa,
65                    struct lov_stripe_md **ea, struct obd_trans_info *oti)
66 {
67         struct mds_obd *mds = &exp->exp_obd->u.mds;
68         struct inode *parent_inode = mds->mds_objects_dir->d_inode;
69         unsigned int tmpname = ll_rand();
70         struct file *filp;
71         struct dentry *new_child;
72         struct lvfs_run_ctxt saved;
73         char fidname[LL_FID_NAMELEN];
74         void *handle;
75         struct lvfs_ucred ucred = { 0 };
76         int rc = 0, err, namelen;
77         ENTRY;
78
79         /* the owner of object file should always be root */
80         cap_raise(ucred.luc_cap, CAP_SYS_RESOURCE);
81
82         if (strncmp(exp->exp_obd->obd_name, MDD_OBD_NAME,
83                                    strlen(MDD_OBD_NAME))) {
84                 RETURN(0);
85         }
86
87         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, &ucred);
88
89         sprintf(fidname, "OBJECTS/%u.%u", tmpname, current->pid);
90         filp = filp_open(fidname, O_CREAT | O_EXCL, 0666);
91         if (IS_ERR(filp)) {
92                 rc = PTR_ERR(filp);
93                 if (rc == -EEXIST) {
94                         CERROR("impossible object name collision %u\n",
95                                tmpname);
96                         LBUG();
97                 }
98                 CERROR("error creating tmp object %u: rc %d\n", tmpname, rc);
99                 GOTO(out_pop, rc);
100         }
101
102         LASSERT(mds->mds_objects_dir == filp->f_dentry->d_parent);
103
104         /* FIXME: need to see how this should change to properly store FID
105          *        into obdo (o_id == OID, o_seq = SEQ) (maybe as IGIF?). */
106 #define o_generation o_parent_oid
107         oa->o_id = filp->f_dentry->d_inode->i_ino;
108         oa->o_generation = filp->f_dentry->d_inode->i_generation;
109         namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
110
111         LOCK_INODE_MUTEX_PARENT(parent_inode);
112         new_child = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
113
114         if (IS_ERR(new_child)) {
115                 CERROR("getting neg dentry for obj rename: %d\n", rc);
116                 GOTO(out_close, rc = PTR_ERR(new_child));
117         }
118         if (new_child->d_inode != NULL) {
119                 CERROR("impossible non-negative obj dentry " LPU64":%u!\n",
120                        oa->o_id, oa->o_generation);
121                 LBUG();
122         }
123
124         handle = fsfilt_start(exp->exp_obd, mds->mds_objects_dir->d_inode,
125                               FSFILT_OP_RENAME, NULL);
126         if (IS_ERR(handle))
127                 GOTO(out_dput, rc = PTR_ERR(handle));
128
129         rc = ll_vfs_rename(mds->mds_objects_dir->d_inode, filp->f_dentry,
130                            filp->f_vfsmnt, mds->mds_objects_dir->d_inode,
131                            new_child, filp->f_vfsmnt);
132
133         if (rc)
134                 CERROR("error renaming new object "LPU64":%u: rc %d\n",
135                        oa->o_id, oa->o_generation, rc);
136
137         err = fsfilt_commit(exp->exp_obd, mds->mds_objects_dir->d_inode,
138                             handle, 0);
139         if (!err) {
140                 oa->o_seq = mdt_to_obd_objseq(mds->mds_id);
141                 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FLGROUP;
142         } else if (!rc)
143                 rc = err;
144 out_dput:
145         dput(new_child);
146 out_close:
147         UNLOCK_INODE_MUTEX(parent_inode);
148         err = filp_close(filp, 0);
149         if (err) {
150                 CERROR("closing tmpfile %u: rc %d\n", tmpname, rc);
151                 if (!rc)
152                         rc = err;
153         }
154 out_pop:
155         pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, &ucred);
156         RETURN(rc);
157 }
158
159 int mds_obd_destroy(struct obd_export *exp, struct obdo *oa,
160                     struct lov_stripe_md *ea, struct obd_trans_info *oti,
161                     struct obd_export *md_exp, void *capa)
162 {
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;
170         struct dentry *de;
171         void *handle;
172         int err, namelen, rc = 0;
173         ENTRY;
174
175         cap_raise(ucred.luc_cap, CAP_SYS_RESOURCE);
176         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &ucred);
177
178         namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
179
180         LOCK_INODE_MUTEX_PARENT(parent_inode);
181         de = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
182         if (IS_ERR(de)) {
183                 rc = IS_ERR(de);
184                 de = NULL;
185                 CERROR("error looking up object "LPU64" %s: rc %d\n",
186                        oa->o_id, fidname, rc);
187                 GOTO(out_dput, rc);
188         }
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);
193         }
194
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);
199
200         if (IS_ERR(handle))
201                 GOTO(out_dput, rc = PTR_ERR(handle));
202
203         /* take a reference to protect inode from truncation within
204            vfs_unlink() context. bug 10409 */
205         inode = de->d_inode;
206         atomic_inc(&inode->i_count);
207         rc = ll_vfs_unlink(mds->mds_objects_dir->d_inode, de, mds->mds_vfsmnt);
208         if (rc)
209                 CERROR("error destroying object "LPU64":%u: rc %d\n",
210                        oa->o_id, oa->o_generation, rc);
211 #undef o_generation
212
213         err = fsfilt_commit(obd, mds->mds_objects_dir->d_inode, handle, 0);
214         if (err && !rc)
215                 rc = err;
216 out_dput:
217         if (de != NULL)
218                 l_dput(de);
219         UNLOCK_INODE_MUTEX(parent_inode);
220
221         if (inode)
222                 iput(inode);
223
224         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &ucred);
225         RETURN(rc);
226 }