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.h>
34 #include <linux/lustre_lib.h>
35 #include <linux/lustre_idl.h>
36 #include <linux/lustre_mds.h>
37 #include <linux/obd_class.h>
39 extern struct ptlrpc_request *mds_prep_req(int size, int opcode, int namelen, char *name, int tgtlen, char *tgt);
41 static int mds_reint_setattr(struct mds_update_record *rec, struct ptlrpc_request *req)
45 de = mds_fid2dentry(req->rq_obd, rec->ur_fid1, NULL);
47 req->rq_rephdr->status = -ESTALE;
51 CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
53 /* a _really_ horrible hack to avoid removing the data stored
54 in the block pointers; this data is the object id
55 this will go into an extended attribute at some point.
57 if ( rec->ur_iattr.ia_valid & ATTR_SIZE ) {
58 /* ATTR_SIZE would invoke truncate: clear it */
59 rec->ur_iattr.ia_valid &= ~ATTR_SIZE;
60 de->d_inode->i_size = rec->ur_iattr.ia_size;
61 /* make sure _something_ gets set - so new inode
62 goes to disk (probably won't work over XFS */
63 if (!rec->ur_iattr.ia_valid & ATTR_MODE) {
64 rec->ur_iattr.ia_valid |= ATTR_MODE;
65 rec->ur_iattr.ia_mode = de->d_inode->i_mode;
68 if ( de->d_inode->i_op->setattr ) {
69 req->rq_rephdr->status =
70 de->d_inode->i_op->setattr(de, &rec->ur_iattr);
72 req->rq_rephdr->status =
73 inode_setattr(de->d_inode, &rec->ur_iattr);
82 XXX nasty hack: store the object id in the first two
85 static inline void mds_store_objid(struct inode *inode, __u64 *id)
87 memcpy(&inode->u.ext2_i.i_data, id, sizeof(*id));
91 static int mds_reint_create(struct mds_update_record *rec,
92 struct ptlrpc_request *req)
94 int type = rec->ur_mode & S_IFMT;
96 struct mds_rep *rep = req->rq_rep.mds;
97 struct dentry *dchild;
101 de = mds_fid2dentry(req->rq_obd, rec->ur_fid1, NULL);
103 req->rq_rephdr->status = -ESTALE;
107 CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
109 dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
110 rc = PTR_ERR(dchild);
111 if (IS_ERR(dchild)) {
112 CERROR("child lookup error %d\n", rc);
114 req->rq_rephdr->status = -ESTALE;
119 if (dchild->d_inode) {
120 CERROR("child exists (dir %ld, name %s)\n",
121 de->d_inode->i_ino, rec->ur_name);
123 req->rq_rephdr->status = -EEXIST;
130 rc = vfs_create(de->d_inode, dchild, rec->ur_mode);
136 rc = vfs_mkdir(de->d_inode, dchild, rec->ur_mode);
141 rc = vfs_symlink(de->d_inode, dchild, rec->ur_tgt);
149 int rdev = rec->ur_id;
150 rc = vfs_mknod(de->d_inode, dchild, rec->ur_mode, rdev);
156 req->rq_rephdr->status = rc;
159 mds_store_objid(dchild->d_inode, &rec->ur_id);
160 dchild->d_inode->i_atime = rec->ur_time;
161 dchild->d_inode->i_ctime = rec->ur_time;
162 dchild->d_inode->i_mtime = rec->ur_time;
163 dchild->d_inode->i_uid = rec->ur_uid;
164 dchild->d_inode->i_gid = rec->ur_gid;
165 rep->ino = dchild->d_inode->i_ino;
174 static int mds_reint_unlink(struct mds_update_record *rec,
175 struct ptlrpc_request *req)
178 struct dentry *dchild;
182 de = mds_fid2dentry(req->rq_obd, rec->ur_fid1, NULL);
184 req->rq_rephdr->status = -ESTALE;
188 CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
190 dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
191 rc = PTR_ERR(dchild);
192 if (IS_ERR(dchild)) {
193 CERROR("child lookup error %d\n", rc);
195 req->rq_rephdr->status = -ESTALE;
200 if (!dchild->d_inode) {
201 CERROR("child doesn't exist (dir %ld, name %s\n",
202 de->d_inode->i_ino, rec->ur_name);
204 req->rq_rephdr->status = -ESTALE;
209 switch (dchild->d_inode->i_mode & S_IFMT) {
211 rc = vfs_rmdir(de->d_inode, dchild);
215 rc = vfs_unlink(de->d_inode, dchild);
221 req->rq_rephdr->status = rc;
228 static int mds_reint_link(struct mds_update_record *rec,
229 struct ptlrpc_request *req)
231 struct dentry *de_src = NULL;
232 struct dentry *de_tgt_dir = NULL;
233 struct dentry *dchild = NULL;
238 de_src = mds_fid2dentry(req->rq_obd, rec->ur_fid1, NULL);
239 if (IS_ERR(de_src)) {
244 de_tgt_dir = mds_fid2dentry(req->rq_obd, rec->ur_fid2, NULL);
245 if (IS_ERR(de_tgt_dir)) {
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 %d\n", rc);
254 req->rq_rephdr->status = -ESTALE;
258 if (dchild->d_inode) {
259 CERROR("child exists (dir %ld, name %s\n",
260 de_tgt_dir->d_inode->i_ino, rec->ur_name);
265 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
268 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;
288 de_srcdir = mds_fid2dentry(req->rq_obd, rec->ur_fid1, NULL);
289 if (IS_ERR(de_srcdir)) {
294 de_tgtdir = mds_fid2dentry(req->rq_obd, rec->ur_fid2, NULL);
295 if (IS_ERR(de_tgtdir)) {
301 de_old = lookup_one_len(rec->ur_name, de_srcdir, rec->ur_namelen - 1);
302 if (IS_ERR(de_old)) {
303 CERROR("child lookup error %d\n", rc);
307 de_new = lookup_one_len(rec->ur_tgt, de_tgtdir, rec->ur_tgtlen - 1);
308 if (IS_ERR(de_new)) {
309 CERROR("child lookup error %d\n", rc);
313 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
316 req->rq_rephdr->status = rc;
325 typedef int (*mds_reinter)(struct mds_update_record *, struct ptlrpc_request*);
327 static mds_reinter reinters[REINT_MAX+1] = {
328 [REINT_SETATTR] mds_reint_setattr,
329 [REINT_CREATE] mds_reint_create,
330 [REINT_UNLINK] mds_reint_unlink,
331 [REINT_LINK] mds_reint_link,
332 [REINT_RENAME] mds_reint_rename
335 int mds_reint_rec(struct mds_update_record *rec, struct ptlrpc_request *req)
339 if (rec->ur_opcode < 0 || rec->ur_opcode > REINT_MAX) {
340 CERROR("opcode %d not valid\n",
342 rc = req->rq_status = -EINVAL;
346 rc = mds_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
347 &req->rq_replen, &req->rq_repbuf);
350 CERROR("mds: out of memory\n");
351 rc = req->rq_status = -ENOMEM;
354 req->rq_rephdr->seqno = req->rq_reqhdr->seqno;
356 rc = reinters[rec->ur_opcode](rec, req);