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);
94 XXX nasty hack: store the object id in the first two
97 static inline void mds_store_objid(struct inode *inode, __u64 *id)
99 /* FIXME: it is only by luck that this works on ext3 */
100 memcpy(&inode->u.ext2_i.i_data, id, sizeof(*id));
104 static int mds_reint_create(struct mds_update_record *rec,
105 struct ptlrpc_request *req)
107 int type = rec->ur_mode & S_IFMT;
108 struct dentry *de = NULL;
109 struct mds_rep *rep = req->rq_rep.mds;
110 struct dentry *dchild = NULL;
114 de = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
115 if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE)) {
117 GOTO(out_reint_create, (rc = -ESTALE));
119 CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
121 dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
122 if (IS_ERR(dchild)) {
123 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
125 GOTO(out_reint_create, (rc = -ESTALE));
128 if (dchild->d_inode) {
129 CERROR("child exists (dir %ld, name %s)\n",
130 de->d_inode->i_ino, rec->ur_name);
132 GOTO(out_reint_create, (rc = -EEXIST));
135 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE);
139 rc = vfs_create(de->d_inode, dchild, rec->ur_mode);
144 rc = vfs_mkdir(de->d_inode, dchild, rec->ur_mode);
149 rc = vfs_symlink(de->d_inode, dchild, rec->ur_tgt);
157 int rdev = rec->ur_id;
158 rc = vfs_mknod(de->d_inode, dchild, rec->ur_mode, rdev);
166 mds_store_objid(dchild->d_inode, &rec->ur_id);
167 dchild->d_inode->i_atime = rec->ur_time;
168 dchild->d_inode->i_ctime = rec->ur_time;
169 dchild->d_inode->i_mtime = rec->ur_time;
170 dchild->d_inode->i_uid = rec->ur_uid;
171 dchild->d_inode->i_gid = rec->ur_gid;
172 rep->ino = dchild->d_inode->i_ino;
173 rep->generation = dchild->d_inode->i_generation;
175 CERROR("error during create: %d\n", rc);
180 req->rq_rephdr->status = rc;
186 static int mds_reint_unlink(struct mds_update_record *rec,
187 struct ptlrpc_request *req)
189 struct dentry *de = NULL;
190 struct dentry *dchild = NULL;
194 de = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
195 if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK)) {
197 GOTO(out_unlink, (rc = -ESTALE));
199 CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
201 dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
202 if (IS_ERR(dchild)) {
203 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
205 GOTO(out_unlink, (rc = -ESTALE));
208 if (!dchild->d_inode) {
209 CERROR("child doesn't exist (dir %ld, name %s\n",
210 de->d_inode->i_ino, rec->ur_name);
212 GOTO(out_unlink, (rc = -ESTALE));
215 if (dchild->d_inode->i_ino != rec->ur_fid2->id)
217 if (dchild->d_inode->i_generation != rec->ur_fid2->generation)
220 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE);
222 switch (dchild->d_inode->i_mode & S_IFMT) {
224 rc = vfs_rmdir(de->d_inode, dchild);
228 rc = vfs_unlink(de->d_inode, dchild);
234 req->rq_rephdr->status = rc;
240 static int mds_reint_link(struct mds_update_record *rec,
241 struct ptlrpc_request *req)
243 struct dentry *de_src = NULL;
244 struct dentry *de_tgt_dir = NULL;
245 struct dentry *dchild = NULL;
249 de_src = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
250 if (IS_ERR(de_src) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK)) {
251 GOTO(out_link, (rc = -ESTALE));
254 de_tgt_dir = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid2, NULL);
255 if (IS_ERR(de_tgt_dir)) {
256 GOTO(out_link, (rc = -ESTALE));
259 dchild = lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen - 1);
260 if (IS_ERR(dchild)) {
261 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
262 GOTO(out_link, (rc = -ESTALE));
265 if (dchild->d_inode) {
266 CERROR("child exists (dir %ld, name %s\n",
267 de_tgt_dir->d_inode->i_ino, rec->ur_name);
268 GOTO(out_link, (rc = -EEXIST));
271 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE);
273 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
277 req->rq_rephdr->status = rc;
285 static int mds_reint_rename(struct mds_update_record *rec,
286 struct ptlrpc_request *req)
288 struct dentry *de_srcdir = NULL;
289 struct dentry *de_tgtdir = NULL;
290 struct dentry *de_old = NULL;
291 struct dentry *de_new = NULL;
295 de_srcdir = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
296 if (IS_ERR(de_srcdir)) {
297 GOTO(out_rename, (rc = -ESTALE));
300 de_tgtdir = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid2, NULL);
301 if (IS_ERR(de_tgtdir)) {
302 GOTO(out_rename, (rc = -ESTALE));
305 de_old = lookup_one_len(rec->ur_name, de_srcdir, rec->ur_namelen - 1);
306 if (IS_ERR(de_old)) {
307 CERROR("child lookup error %ld\n", PTR_ERR(de_old));
308 GOTO(out_rename, (rc = -ESTALE));
311 de_new = lookup_one_len(rec->ur_tgt, de_tgtdir, rec->ur_tgtlen - 1);
312 if (IS_ERR(de_new)) {
313 CERROR("child lookup error %ld\n", PTR_ERR(de_new));
314 GOTO(out_rename, (rc = -ESTALE));
317 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE);
319 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
323 req->rq_rephdr->status = rc;
331 typedef int (*mds_reinter)(struct mds_update_record *, struct ptlrpc_request*);
333 static mds_reinter reinters[REINT_MAX+1] = {
334 [REINT_SETATTR] mds_reint_setattr,
335 [REINT_CREATE] mds_reint_create,
336 [REINT_UNLINK] mds_reint_unlink,
337 [REINT_LINK] mds_reint_link,
338 [REINT_RENAME] mds_reint_rename
341 int mds_reint_rec(struct mds_update_record *rec, struct ptlrpc_request *req)
345 if (rec->ur_opcode < 0 || rec->ur_opcode > REINT_MAX) {
346 CERROR("opcode %d not valid\n", rec->ur_opcode);
347 rc = req->rq_status = -EINVAL;
351 rc = mds_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
352 &req->rq_replen, &req->rq_repbuf);
354 CERROR("mds: out of memory\n");
355 rc = req->rq_status = -ENOMEM;
358 req->rq_rephdr->xid = req->rq_reqhdr->xid;
360 rc = reinters[rec->ur_opcode](rec, req);