Whamcloud - gitweb
LU-1347 style: removes obsolete EXPORT_SYMTAB macros
[fs/lustre-release.git] / lustre / mds / mds_fs.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lustre/mds/mds_fs.c
35  *
36  * Lustre Metadata Server (MDS) filesystem interface code
37  *
38  * Author: Andreas Dilger <adilger@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_MDS
42
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>
55
56 #include "mds_internal.h"
57
58
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)
65 {
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();
69         struct file *filp;
70         struct dentry *new_child;
71         struct lvfs_run_ctxt saved;
72         char fidname[LL_FID_NAMELEN];
73         void *handle;
74         struct lvfs_ucred ucred = { 0 };
75         int rc = 0, err, namelen;
76         ENTRY;
77
78         /* the owner of object file should always be root */
79         cap_raise(ucred.luc_cap, CAP_SYS_RESOURCE);
80
81         if (strncmp(exp->exp_obd->obd_name, MDD_OBD_NAME,
82                                    strlen(MDD_OBD_NAME))) {
83                 RETURN(0);
84         }
85
86         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, &ucred);
87
88         sprintf(fidname, "OBJECTS/%u.%u", tmpname, current->pid);
89         filp = filp_open(fidname, O_CREAT | O_EXCL, 0666);
90         if (IS_ERR(filp)) {
91                 rc = PTR_ERR(filp);
92                 if (rc == -EEXIST) {
93                         CERROR("impossible object name collision %u\n",
94                                tmpname);
95                         LBUG();
96                 }
97                 CERROR("error creating tmp object %u: rc %d\n", tmpname, rc);
98                 GOTO(out_pop, rc);
99         }
100
101         LASSERT(mds->mds_objects_dir == filp->f_dentry->d_parent);
102
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);
109
110         LOCK_INODE_MUTEX_PARENT(parent_inode);
111         new_child = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
112
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));
116         }
117         if (new_child->d_inode != NULL) {
118                 CERROR("impossible non-negative obj dentry " LPU64":%u!\n",
119                        oa->o_id, oa->o_generation);
120                 LBUG();
121         }
122
123         handle = fsfilt_start(exp->exp_obd, mds->mds_objects_dir->d_inode,
124                               FSFILT_OP_RENAME, NULL);
125         if (IS_ERR(handle))
126                 GOTO(out_dput, rc = PTR_ERR(handle));
127
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
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_seq = mdt_to_obd_objseq(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(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,
161                     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,
208                            mds->mds_obt.obt_vfsmnt);
209         if (rc)
210                 CERROR("error destroying object "LPU64":%u: rc %d\n",
211                        oa->o_id, oa->o_generation, rc);
212 #undef o_generation
213
214         err = fsfilt_commit(obd, mds->mds_objects_dir->d_inode, handle, 0);
215         if (err && !rc)
216                 rc = err;
217 out_dput:
218         if (de != NULL)
219                 l_dput(de);
220         UNLOCK_INODE_MUTEX(parent_inode);
221
222         if (inode)
223                 iput(inode);
224
225         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &ucred);
226         RETURN(rc);
227 }