1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * linux/mds/mds_reint.c
6 * Lustre Metadata Server (mds) reintegration routines
8 * Copyright (C) 2002 Cluster File Systems, Inc.
9 * author: Peter Braam <braam@clusterfs.com>
11 * This code is issued under the GNU General Public License.
12 * See the file COPYING in this distribution
16 // XXX - add transaction sequence numbers
20 #include <linux/version.h>
21 #include <linux/module.h>
23 #include <linux/stat.h>
24 #include <linux/locks.h>
25 #include <linux/ext2_fs.h>
26 #include <linux/quotaops.h>
27 #include <asm/unistd.h>
28 #include <asm/uaccess.h>
30 #define DEBUG_SUBSYSTEM S_MDS
32 #include <linux/obd_support.h>
33 #include <linux/obd_class.h>
34 #include <linux/obd.h>
35 #include <linux/lustre_lib.h>
36 #include <linux/lustre_idl.h>
37 #include <linux/lustre_mds.h>
38 #include <linux/obd_class.h>
40 extern struct ptlrpc_request *mds_prep_req(int size, int opcode, int namelen, char *name, int tgtlen, char *tgt);
42 static int mds_reint_setattr(struct mds_update_record *rec, struct ptlrpc_request *req)
47 de = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
48 if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_SETATTR)) {
49 req->rq_rephdr->status = -ESTALE;
54 CDEBUG(D_INODE, "ino %ld\n", inode->i_ino);
56 /* a _really_ horrible hack to avoid removing the data stored
57 in the block pointers; this data is the object id
58 this will go into an extended attribute at some point.
60 if ( rec->ur_iattr.ia_valid & ATTR_SIZE ) {
61 /* ATTR_SIZE would invoke truncate: clear it */
62 rec->ur_iattr.ia_valid &= ~ATTR_SIZE;
63 inode->i_size = rec->ur_iattr.ia_size;
65 /* an _even_more_ horrible hack to make this hack work with
66 * ext3. This is because ext3 keeps a separate inode size
67 * until the inode is committed to ensure consistency. This
68 * will also go away with the move to EAs.
70 if (!strcmp(inode->i_sb->s_type->name, "ext3"))
71 inode->u.ext3_i.i_disksize = inode->i_size;
73 /* make sure _something_ gets set - so new inode
74 goes to disk (probably won't work over XFS */
75 if (!rec->ur_iattr.ia_valid & ATTR_MODE) {
76 rec->ur_iattr.ia_valid |= ATTR_MODE;
77 rec->ur_iattr.ia_mode = inode->i_mode;
80 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE);
81 if ( inode->i_op->setattr ) {
82 req->rq_rephdr->status =
83 inode->i_op->setattr(de, &rec->ur_iattr);
85 req->rq_rephdr->status =
86 inode_setattr(inode, &rec->ur_iattr);
95 XXX nasty hack: store the object id in the first two
98 static inline void mds_store_objid(struct inode *inode, __u64 *id)
100 /* FIXME: it is only by luck that this works on ext3 */
101 memcpy(&inode->u.ext2_i.i_data, id, sizeof(*id));
105 static int mds_reint_create(struct mds_update_record *rec,
106 struct ptlrpc_request *req)
108 int type = rec->ur_mode & S_IFMT;
109 struct dentry *de = NULL;
110 struct mds_rep *rep = req->rq_rep.mds;
111 struct dentry *dchild = NULL;
115 de = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
116 if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE)) {
118 GOTO(out_reint_create, (rc = -ESTALE));
120 CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
122 dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
123 if (IS_ERR(dchild)) {
124 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
126 GOTO(out_reint_create, (rc = -ESTALE));
129 if (dchild->d_inode) {
130 CERROR("child exists (dir %ld, name %s)\n",
131 de->d_inode->i_ino, rec->ur_name);
133 GOTO(out_reint_create, (rc = -EEXIST));
136 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE);
140 rc = vfs_create(de->d_inode, dchild, rec->ur_mode);
145 rc = vfs_mkdir(de->d_inode, dchild, rec->ur_mode);
150 rc = vfs_symlink(de->d_inode, dchild, rec->ur_tgt);
158 int rdev = rec->ur_id;
159 rc = vfs_mknod(de->d_inode, dchild, rec->ur_mode, rdev);
167 mds_store_objid(dchild->d_inode, &rec->ur_id);
168 dchild->d_inode->i_atime = rec->ur_time;
169 dchild->d_inode->i_ctime = rec->ur_time;
170 dchild->d_inode->i_mtime = rec->ur_time;
171 dchild->d_inode->i_uid = rec->ur_uid;
172 dchild->d_inode->i_gid = rec->ur_gid;
173 rep->ino = dchild->d_inode->i_ino;
177 req->rq_rephdr->status = rc;
183 static int mds_reint_unlink(struct mds_update_record *rec,
184 struct ptlrpc_request *req)
186 struct dentry *de = NULL;
187 struct dentry *dchild = NULL;
191 de = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
192 if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK)) {
194 GOTO(out_unlink, (rc = -ESTALE));
196 CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
198 dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
199 if (IS_ERR(dchild)) {
200 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
202 GOTO(out_unlink, (rc = -ESTALE));
205 if (!dchild->d_inode) {
206 CERROR("child doesn't exist (dir %ld, name %s\n",
207 de->d_inode->i_ino, rec->ur_name);
209 GOTO(out_unlink, (rc = -ESTALE));
212 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE);
214 switch (dchild->d_inode->i_mode & S_IFMT) {
216 rc = vfs_rmdir(de->d_inode, dchild);
220 rc = vfs_unlink(de->d_inode, dchild);
226 req->rq_rephdr->status = rc;
232 static int mds_reint_link(struct mds_update_record *rec,
233 struct ptlrpc_request *req)
235 struct dentry *de_src = NULL;
236 struct dentry *de_tgt_dir = NULL;
237 struct dentry *dchild = NULL;
241 de_src = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
242 if (IS_ERR(de_src) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK)) {
243 GOTO(out_link, (rc = -ESTALE));
246 de_tgt_dir = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid2, NULL);
247 if (IS_ERR(de_tgt_dir)) {
248 GOTO(out_link, (rc = -ESTALE));
251 dchild = lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen - 1);
252 if (IS_ERR(dchild)) {
253 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
254 GOTO(out_link, (rc = -ESTALE));
257 if (dchild->d_inode) {
258 CERROR("child exists (dir %ld, name %s\n",
259 de_tgt_dir->d_inode->i_ino, rec->ur_name);
260 GOTO(out_link, (rc = -EEXIST));
263 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE);
265 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
269 req->rq_rephdr->status = rc;
277 static int mds_reint_rename(struct mds_update_record *rec,
278 struct ptlrpc_request *req)
280 struct dentry *de_srcdir = NULL;
281 struct dentry *de_tgtdir = NULL;
282 struct dentry *de_old = NULL;
283 struct dentry *de_new = NULL;
287 de_srcdir = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
288 if (IS_ERR(de_srcdir)) {
289 GOTO(out_rename, (rc = -ESTALE));
292 de_tgtdir = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid2, NULL);
293 if (IS_ERR(de_tgtdir)) {
294 GOTO(out_rename, (rc = -ESTALE));
297 de_old = lookup_one_len(rec->ur_name, de_srcdir, rec->ur_namelen - 1);
298 if (IS_ERR(de_old)) {
299 CERROR("child lookup error %ld\n", PTR_ERR(de_old));
300 GOTO(out_rename, (rc = -ESTALE));
303 de_new = lookup_one_len(rec->ur_tgt, de_tgtdir, rec->ur_tgtlen - 1);
304 if (IS_ERR(de_new)) {
305 CERROR("child lookup error %ld\n", PTR_ERR(de_new));
306 GOTO(out_rename, (rc = -ESTALE));
309 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE);
311 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
315 req->rq_rephdr->status = rc;
323 typedef int (*mds_reinter)(struct mds_update_record *, struct ptlrpc_request*);
325 static mds_reinter reinters[REINT_MAX+1] = {
326 [REINT_SETATTR] mds_reint_setattr,
327 [REINT_CREATE] mds_reint_create,
328 [REINT_UNLINK] mds_reint_unlink,
329 [REINT_LINK] mds_reint_link,
330 [REINT_RENAME] mds_reint_rename
333 int mds_reint_rec(struct mds_update_record *rec, struct ptlrpc_request *req)
337 if (rec->ur_opcode < 0 || rec->ur_opcode > REINT_MAX) {
338 CERROR("opcode %d not valid\n", rec->ur_opcode);
339 rc = req->rq_status = -EINVAL;
343 rc = mds_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
344 &req->rq_replen, &req->rq_repbuf);
346 CERROR("mds: out of memory\n");
347 rc = req->rq_status = -ENOMEM;
350 req->rq_rephdr->xid = req->rq_reqhdr->xid;
352 rc = reinters[rec->ur_opcode](rec, req);