Whamcloud - gitweb
b=14230
[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  *  mds/mds_fs.c
5  *  Lustre Metadata Server (MDS) filesystem interface code
6  *
7  *  Copyright (C) 2002, 2003 Cluster File Systems, Inc.
8  *   Author: Andreas Dilger <adilger@clusterfs.com>
9  *
10  *   This file is part of the Lustre file system, http://www.lustre.org
11  *   Lustre is a trademark of Cluster File Systems, Inc.
12  *
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.
17  *
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.
22  *
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.
27  */
28
29 #define DEBUG_SUBSYSTEM S_MDS
30
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>
44
45 #include "mds_internal.h"
46
47
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)
53 {
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();
57         struct file *filp;
58         struct dentry *new_child;
59         struct lvfs_run_ctxt saved;
60         char fidname[LL_FID_NAMELEN];
61         void *handle;
62         struct lvfs_ucred ucred = { 0 };
63         int rc = 0, err, namelen;
64         ENTRY;
65
66         /* the owner of object file should always be root */
67         cap_raise(ucred.luc_cap, CAP_SYS_RESOURCE);
68
69         if (strncmp(exp->exp_obd->obd_name, MDD_OBD_NAME,
70                                    strlen(MDD_OBD_NAME))) {
71                 RETURN(0);
72         }
73         
74         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, &ucred);
75
76         sprintf(fidname, "OBJECTS/%u.%u", tmpname, current->pid);
77         filp = filp_open(fidname, O_CREAT | O_EXCL, 0666);
78         if (IS_ERR(filp)) {
79                 rc = PTR_ERR(filp);
80                 if (rc == -EEXIST) {
81                         CERROR("impossible object name collision %u\n",
82                                tmpname);
83                         LBUG();
84                 }
85                 CERROR("error creating tmp object %u: rc %d\n", tmpname, rc);
86                 GOTO(out_pop, rc);
87         }
88
89         LASSERT(mds->mds_objects_dir == filp->f_dentry->d_parent);
90
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);
94
95         LOCK_INODE_MUTEX(parent_inode);
96         new_child = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
97
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));
101         }
102         if (new_child->d_inode != NULL) {
103                 CERROR("impossible non-negative obj dentry " LPU64":%u!\n",
104                        oa->o_id, oa->o_generation);
105                 LBUG();
106         }
107
108         handle = fsfilt_start(exp->exp_obd, mds->mds_objects_dir->d_inode,
109                               FSFILT_OP_RENAME, NULL);
110         if (IS_ERR(handle))
111                 GOTO(out_dput, rc = PTR_ERR(handle));
112
113         lock_kernel();
114         rc = vfs_rename(mds->mds_objects_dir->d_inode, filp->f_dentry,
115                         mds->mds_objects_dir->d_inode, new_child);
116         unlock_kernel();
117         if (rc)
118                 CERROR("error renaming new object "LPU64":%u: rc %d\n",
119                        oa->o_id, oa->o_generation, rc);
120
121         err = fsfilt_commit(exp->exp_obd, mds->mds_objects_dir->d_inode,
122                             handle, 0);
123         if (!err) {
124                 oa->o_gr = FILTER_GROUP_MDS0 + mds->mds_id;
125                 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FLGROUP;
126         } else if (!rc)
127                 rc = err;
128 out_dput:
129         dput(new_child);
130 out_close:
131         UNLOCK_INODE_MUTEX(parent_inode);
132         err = filp_close(filp, 0);
133         if (err) {
134                 CERROR("closing tmpfile %u: rc %d\n", tmpname, rc);
135                 if (!rc)
136                         rc = err;
137         }
138 out_pop:
139         pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, &ucred);
140         RETURN(rc);
141 }
142
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)
146 {
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;
154         struct dentry *de;
155         void *handle;
156         int err, namelen, rc = 0;
157         ENTRY;
158
159         cap_raise(ucred.luc_cap, CAP_SYS_RESOURCE);
160         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &ucred);
161
162         namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
163
164         LOCK_INODE_MUTEX(parent_inode);
165         de = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
166         if (IS_ERR(de)) {
167                 rc = IS_ERR(de);
168                 de = NULL;
169                 CERROR("error looking up object "LPU64" %s: rc %d\n",
170                        oa->o_id, fidname, rc);
171                 GOTO(out_dput, rc);
172         }
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);
177         }
178
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);
183
184         if (IS_ERR(handle))
185                 GOTO(out_dput, rc = PTR_ERR(handle));
186
187         /* take a reference to protect inode from truncation within
188            vfs_unlink() context. bug 10409 */
189         inode = de->d_inode;
190         atomic_inc(&inode->i_count);
191         rc = vfs_unlink(mds->mds_objects_dir->d_inode, de);
192         if (rc)
193                 CERROR("error destroying object "LPU64":%u: rc %d\n",
194                        oa->o_id, oa->o_generation, rc);
195
196         err = fsfilt_commit(obd, mds->mds_objects_dir->d_inode, handle, 0);
197         if (err && !rc)
198                 rc = err;
199 out_dput:
200         if (de != NULL)
201                 l_dput(de);
202         UNLOCK_INODE_MUTEX(parent_inode);
203
204         if (inode)
205                 iput(inode);
206
207         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &ucred);
208         RETURN(rc);
209 }