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)
46 de = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
48 req->rq_rephdr->status = -ESTALE;
52 CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
54 /* a _really_ horrible hack to avoid removing the data stored
55 in the block pointers; this data is the object id
56 this will go into an extended attribute at some point.
58 if ( rec->ur_iattr.ia_valid & ATTR_SIZE ) {
59 /* ATTR_SIZE would invoke truncate: clear it */
60 rec->ur_iattr.ia_valid &= ~ATTR_SIZE;
61 de->d_inode->i_size = rec->ur_iattr.ia_size;
62 /* make sure _something_ gets set - so new inode
63 goes to disk (probably won't work over XFS */
64 if (!rec->ur_iattr.ia_valid & ATTR_MODE) {
65 rec->ur_iattr.ia_valid |= ATTR_MODE;
66 rec->ur_iattr.ia_mode = de->d_inode->i_mode;
69 if ( de->d_inode->i_op->setattr ) {
70 req->rq_rephdr->status =
71 de->d_inode->i_op->setattr(de, &rec->ur_iattr);
73 req->rq_rephdr->status =
74 inode_setattr(de->d_inode, &rec->ur_iattr);
83 XXX nasty hack: store the object id in the first two
86 static inline void mds_store_objid(struct inode *inode, __u64 *id)
88 memcpy(&inode->u.ext2_i.i_data, id, sizeof(*id));
92 static int mds_reint_create(struct mds_update_record *rec,
93 struct ptlrpc_request *req)
95 int type = rec->ur_mode & S_IFMT;
97 struct mds_rep *rep = req->rq_rep.mds;
98 struct dentry *dchild;
102 de = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
104 req->rq_rephdr->status = -ESTALE;
108 CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
110 dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
111 rc = PTR_ERR(dchild);
112 if (IS_ERR(dchild)) {
113 CERROR("child lookup error %d\n", rc);
115 req->rq_rephdr->status = -ESTALE;
120 if (dchild->d_inode) {
121 CERROR("child exists (dir %ld, name %s)\n",
122 de->d_inode->i_ino, rec->ur_name);
124 req->rq_rephdr->status = -EEXIST;
131 rc = vfs_create(de->d_inode, dchild, rec->ur_mode);
137 rc = vfs_mkdir(de->d_inode, dchild, rec->ur_mode);
142 rc = vfs_symlink(de->d_inode, dchild, rec->ur_tgt);
150 int rdev = rec->ur_id;
151 rc = vfs_mknod(de->d_inode, dchild, rec->ur_mode, rdev);
157 req->rq_rephdr->status = rc;
160 mds_store_objid(dchild->d_inode, &rec->ur_id);
161 dchild->d_inode->i_atime = rec->ur_time;
162 dchild->d_inode->i_ctime = rec->ur_time;
163 dchild->d_inode->i_mtime = rec->ur_time;
164 dchild->d_inode->i_uid = rec->ur_uid;
165 dchild->d_inode->i_gid = rec->ur_gid;
166 rep->ino = dchild->d_inode->i_ino;
175 static int mds_reint_unlink(struct mds_update_record *rec,
176 struct ptlrpc_request *req)
179 struct dentry *dchild;
183 de = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
185 req->rq_rephdr->status = -ESTALE;
189 CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
191 dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
192 rc = PTR_ERR(dchild);
193 if (IS_ERR(dchild)) {
194 CERROR("child lookup error %d\n", rc);
196 req->rq_rephdr->status = -ESTALE;
201 if (!dchild->d_inode) {
202 CERROR("child doesn't exist (dir %ld, name %s\n",
203 de->d_inode->i_ino, rec->ur_name);
205 req->rq_rephdr->status = -ESTALE;
210 switch (dchild->d_inode->i_mode & S_IFMT) {
212 rc = vfs_rmdir(de->d_inode, dchild);
216 rc = vfs_unlink(de->d_inode, dchild);
222 req->rq_rephdr->status = rc;
229 static int mds_reint_link(struct mds_update_record *rec,
230 struct ptlrpc_request *req)
232 struct dentry *de_src = NULL;
233 struct dentry *de_tgt_dir = NULL;
234 struct dentry *dchild = NULL;
239 de_src = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
240 if (IS_ERR(de_src)) {
245 de_tgt_dir = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid2, NULL);
246 if (IS_ERR(de_tgt_dir)) {
252 dchild = lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen - 1);
253 if (IS_ERR(dchild)) {
254 CERROR("child lookup error %d\n", rc);
255 req->rq_rephdr->status = -ESTALE;
259 if (dchild->d_inode) {
260 CERROR("child exists (dir %ld, name %s\n",
261 de_tgt_dir->d_inode->i_ino, rec->ur_name);
266 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
269 req->rq_rephdr->status = rc;
278 static int mds_reint_rename(struct mds_update_record *rec,
279 struct ptlrpc_request *req)
281 struct dentry *de_srcdir = NULL;
282 struct dentry *de_tgtdir = NULL;
283 struct dentry *de_old = NULL;
284 struct dentry *de_new = NULL;
289 de_srcdir = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
290 if (IS_ERR(de_srcdir)) {
295 de_tgtdir = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid2, NULL);
296 if (IS_ERR(de_tgtdir)) {
302 de_old = lookup_one_len(rec->ur_name, de_srcdir, rec->ur_namelen - 1);
303 if (IS_ERR(de_old)) {
304 CERROR("child lookup error %d\n", rc);
308 de_new = lookup_one_len(rec->ur_tgt, de_tgtdir, rec->ur_tgtlen - 1);
309 if (IS_ERR(de_new)) {
310 CERROR("child lookup error %d\n", rc);
314 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
317 req->rq_rephdr->status = rc;
326 typedef int (*mds_reinter)(struct mds_update_record *, struct ptlrpc_request*);
328 static mds_reinter reinters[REINT_MAX+1] = {
329 [REINT_SETATTR] mds_reint_setattr,
330 [REINT_CREATE] mds_reint_create,
331 [REINT_UNLINK] mds_reint_unlink,
332 [REINT_LINK] mds_reint_link,
333 [REINT_RENAME] mds_reint_rename
336 int mds_reint_rec(struct mds_update_record *rec, struct ptlrpc_request *req)
340 if (rec->ur_opcode < 0 || rec->ur_opcode > REINT_MAX) {
341 CERROR("opcode %d not valid\n",
343 rc = req->rq_status = -EINVAL;
347 rc = mds_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
348 &req->rq_replen, &req->rq_repbuf);
351 CERROR("mds: out of memory\n");
352 rc = req->rq_status = -ENOMEM;
355 req->rq_rephdr->xid = req->rq_reqhdr->xid;
357 rc = reinters[rec->ur_opcode](rec, req);