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/quotaops.h>
26 #include <asm/unistd.h>
27 #include <asm/uaccess.h>
29 #define DEBUG_SUBSYSTEM S_MDS
31 #include <linux/obd_support.h>
32 #include <linux/obd_class.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 struct mds_client_info *mds_uuid_to_mci(struct mds_obd *mds, __u8 *uuid)
46 list_for_each(p, &mds->mds_client_info) {
47 struct mds_client_info *mci;
49 mci = list_entry(p, struct mds_client_info, mci_list);
50 CDEBUG(D_INFO, "checking client UUID '%s'\n",
51 mci->mci_mcd->mcd_uuid);
52 if (!strncmp(mci->mci_mcd->mcd_uuid, uuid,
53 sizeof(mci->mci_mcd->mcd_uuid)))
56 CDEBUG(D_INFO, "no mds client info found for UUID '%s'\n", uuid);
60 int mds_update_last_rcvd(struct mds_obd *mds, void *handle,
61 struct ptlrpc_request *req)
63 /* get from req->rq_connection-> or req->rq_client */
64 struct obd_run_ctxt saved;
65 struct mds_client_info *mci;
69 mci = mds_uuid_to_mci(mds, req->rq_connection->c_remote_uuid);
71 CERROR("unable to locate MDS client data for UUID '%s'\n",
72 ptlrpc_req_to_uuid(req));
73 /* This will be a real error once everything is working */
78 off = MDS_LR_CLIENT + mci->mci_off * MDS_LR_SIZE;
80 ++mds->mds_last_rcvd; /* lock this, or make it an LDLM function? */
81 req->rq_repmsg->transno = HTON__u64(mds->mds_last_rcvd);
82 mci->mci_mcd->mcd_last_rcvd = cpu_to_le64(mds->mds_last_rcvd);
83 mci->mci_mcd->mcd_mount_count = cpu_to_le64(mds->mds_mount_count);
84 mci->mci_mcd->mcd_last_xid = cpu_to_le32(req->rq_reqmsg->xid);
86 mds_fs_set_last_rcvd(mds, handle);
87 push_ctxt(&saved, &mds->mds_ctxt);
88 rc = lustre_fwrite(mds->mds_rcvd_filp, (char *)mci->mci_mcd,
89 sizeof(*mci->mci_mcd), &off);
91 CDEBUG(D_INODE, "wrote trans #%Ld for client '%s' at #%d: rc = %d\n",
92 mds->mds_last_rcvd, mci->mci_mcd->mcd_uuid, mci->mci_off, rc);
93 // store new value and last committed value in req struct
95 if (rc == sizeof(*mci->mci_mcd))
98 CERROR("error writing to last_rcvd file: rc = %d\n", rc);
106 static int mds_reint_setattr(struct mds_update_record *rec,
107 struct ptlrpc_request *req)
109 struct mds_obd *mds = &req->rq_obd->u.mds;
114 de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
115 if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_SETATTR)) {
116 GOTO(out_setattr, rc = -ESTALE);
119 CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
121 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE,
122 de->d_inode->i_sb->s_dev);
124 handle = mds_fs_start(mds, de->d_inode, MDS_FSOP_SETATTR);
126 GOTO(out_setattr_de, rc = PTR_ERR(handle));
127 rc = mds_fs_setattr(mds, de, handle, &rec->ur_iattr);
130 rc = mds_update_last_rcvd(mds, handle, req);
131 /* FIXME: need to return last_rcvd, last_committed */
135 /* FIXME: keep rc intact */
136 rc = mds_fs_commit(mds, de->d_inode, handle);
144 static int mds_reint_recreate(struct mds_update_record *rec,
145 struct ptlrpc_request *req)
147 struct dentry *de = NULL;
148 struct mds_obd *mds = &req->rq_obd->u.mds;
149 struct dentry *dchild = NULL;
154 de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
155 if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE)) {
157 GOTO(out_create_de, rc = -ESTALE);
160 CDEBUG(D_INODE, "parent ino %ld\n", dir->i_ino);
163 dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
164 if (IS_ERR(dchild)) {
165 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
168 GOTO(out_create_dchild, rc = -ESTALE);
171 if (dchild->d_inode) {
172 struct mds_body *body;
174 body = lustre_msg_buf(req->rq_repmsg, 0);
175 body->ino = dchild->d_inode->i_ino;
176 body->generation = dchild->d_inode->i_generation;
178 CERROR("child doesn't exist (dir %ld, name %s)\n",
179 dir->i_ino, rec->ur_name);
192 static int mds_reint_create(struct mds_update_record *rec,
193 struct ptlrpc_request *req)
195 struct dentry *de = NULL;
196 struct mds_obd *mds = &req->rq_obd->u.mds;
197 struct dentry *dchild = NULL;
200 int rc = 0, type = rec->ur_mode & S_IFMT;
203 de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
204 if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE)) {
206 GOTO(out_create_de, rc = -ESTALE);
209 CDEBUG(D_INODE, "parent ino %ld name %s mode %o\n",
210 dir->i_ino, rec->ur_name, rec->ur_mode);
213 dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
214 if (IS_ERR(dchild)) {
215 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
218 GOTO(out_create_dchild, rc = -ESTALE);
221 if (dchild->d_inode) {
222 CERROR("child exists (dir %ld, name %s, ino %ld)\n",
223 dir->i_ino, rec->ur_name, dchild->d_inode->i_ino);
225 GOTO(out_create_dchild, rc = -EEXIST);
228 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb->s_dev);
232 handle = mds_fs_start(mds, dir, MDS_FSOP_CREATE);
234 GOTO(out_create_dchild, PTR_ERR(handle));
235 rc = vfs_create(dir, dchild, rec->ur_mode);
240 handle = mds_fs_start(mds, dir, MDS_FSOP_MKDIR);
242 GOTO(out_create_dchild, PTR_ERR(handle));
243 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
248 handle = mds_fs_start(mds, dir, MDS_FSOP_SYMLINK);
250 GOTO(out_create_dchild, PTR_ERR(handle));
251 rc = vfs_symlink(dir, dchild, rec->ur_tgt);
259 int rdev = rec->ur_id;
260 handle = mds_fs_start(mds, dir, MDS_FSOP_MKNOD);
262 GOTO(out_create_dchild, PTR_ERR(handle));
263 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
268 CERROR("bad file type %d for create of %s\n",type,rec->ur_name);
269 GOTO(out_create_dchild, rc = -EINVAL);
273 CERROR("error during create: %d\n", rc);
274 if (rc != -ENOSPC) LBUG();
275 GOTO(out_create_commit, rc);
278 struct inode *inode = dchild->d_inode;
279 struct mds_body *body;
281 CDEBUG(D_INODE, "created ino %ld\n", dchild->d_inode->i_ino);
282 if (type == S_IFREG) {
283 rc = mds_fs_set_objid(mds, inode, handle, rec->ur_id);
285 CERROR("error %d setting objid for %ld\n",
289 iattr.ia_atime = rec->ur_time;
290 iattr.ia_ctime = rec->ur_time;
291 iattr.ia_mtime = rec->ur_time;
292 iattr.ia_uid = rec->ur_uid;
293 iattr.ia_gid = rec->ur_gid;
294 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
295 ATTR_MTIME | ATTR_CTIME;
297 rc = mds_fs_setattr(mds, dchild, handle, &iattr);
298 /* XXX should we abort here in case of error? */
301 rc = mds_update_last_rcvd(mds, handle, req);
303 body = lustre_msg_buf(req->rq_repmsg, 0);
304 body->ino = inode->i_ino;
305 body->generation = inode->i_generation;
309 /* FIXME: keep rc intact */
310 rc = mds_fs_commit(mds, dir, handle);
320 static int mds_reint_unlink(struct mds_update_record *rec,
321 struct ptlrpc_request *req)
323 struct dentry *de = NULL;
324 struct dentry *dchild = NULL;
325 struct mds_obd *mds = &req->rq_obd->u.mds;
326 struct inode *dir, *inode;
331 de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
332 if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK)) {
334 GOTO(out_unlink, rc = -ESTALE);
337 CDEBUG(D_INODE, "parent ino %ld\n", dir->i_ino);
340 dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
341 if (IS_ERR(dchild)) {
342 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
344 GOTO(out_unlink_de, rc = -ESTALE);
347 inode = dchild->d_inode;
349 CERROR("child doesn't exist (dir %ld, name %s\n",
350 dir->i_ino, rec->ur_name);
352 GOTO(out_unlink_dchild, rc = -ESTALE);
355 if (inode->i_ino != rec->ur_fid2->id) {
356 CERROR("inode and FID ID do not match (%ld != %Ld)\n",
357 inode->i_ino, rec->ur_fid2->id);
359 GOTO(out_unlink_dchild, rc = -ESTALE);
361 if (inode->i_generation != rec->ur_fid2->generation) {
362 CERROR("inode and FID GENERATION do not match (%d != %d)\n",
363 inode->i_generation, rec->ur_fid2->generation);
365 GOTO(out_unlink_dchild, rc = -ESTALE);
368 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dir->i_sb->s_dev);
370 switch (dchild->d_inode->i_mode & S_IFMT) {
372 handle = mds_fs_start(mds, dir, MDS_FSOP_RMDIR);
374 GOTO(out_unlink_dchild, rc = PTR_ERR(handle));
375 rc = vfs_rmdir(dir, dchild);
378 handle = mds_fs_start(mds, dir, MDS_FSOP_UNLINK);
380 GOTO(out_unlink_dchild, rc = PTR_ERR(handle));
381 rc = vfs_unlink(dir, dchild);
386 rc = mds_update_last_rcvd(mds, handle, req);
387 /* FIXME: need to return last_rcvd, last_committed */
388 /* FIXME: keep rc intact */
389 rc = mds_fs_commit(mds, dir, handle);
402 static int mds_reint_link(struct mds_update_record *rec,
403 struct ptlrpc_request *req)
405 struct dentry *de_src = NULL;
406 struct dentry *de_tgt_dir = NULL;
407 struct dentry *dchild = NULL;
408 struct mds_obd *mds = &req->rq_obd->u.mds;
413 de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
414 if (IS_ERR(de_src) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK)) {
415 GOTO(out_link, rc = -ESTALE);
418 de_tgt_dir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
419 if (IS_ERR(de_tgt_dir)) {
420 GOTO(out_link_de_src, rc = -ESTALE);
423 down(&de_tgt_dir->d_inode->i_sem);
424 dchild = lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen - 1);
425 if (IS_ERR(dchild)) {
426 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
427 GOTO(out_link_de_tgt_dir, rc = -ESTALE);
430 if (dchild->d_inode) {
431 CERROR("child exists (dir %ld, name %s\n",
432 de_tgt_dir->d_inode->i_ino, rec->ur_name);
433 GOTO(out_link_dchild, rc = -EEXIST);
436 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE,
437 dchild->d_inode->i_sb->s_dev);
439 handle = mds_fs_start(mds, de_tgt_dir->d_inode, MDS_FSOP_LINK);
441 GOTO(out_link_dchild, rc = PTR_ERR(handle));
443 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
446 rc = mds_update_last_rcvd(mds, handle, req);
448 /* FIXME: need to return last_rcvd, last_committed */
449 /* FIXME: keep rc intact */
450 rc = mds_fs_commit(mds, de_tgt_dir->d_inode, handle);
456 up(&de_tgt_dir->d_inode->i_sem);
465 static int mds_reint_rename(struct mds_update_record *rec,
466 struct ptlrpc_request *req)
468 struct dentry *de_srcdir = NULL;
469 struct dentry *de_tgtdir = NULL;
470 struct dentry *de_old = NULL;
471 struct dentry *de_new = NULL;
472 struct mds_obd *mds = &req->rq_obd->u.mds;
477 de_srcdir = mds_fid2dentry(mds, rec->ur_fid1, NULL);
478 if (IS_ERR(de_srcdir)) {
479 GOTO(out_rename, rc = -ESTALE);
482 de_tgtdir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
483 if (IS_ERR(de_tgtdir)) {
484 GOTO(out_rename_srcdir, rc = -ESTALE);
487 de_old = lookup_one_len(rec->ur_name, de_srcdir, rec->ur_namelen - 1);
488 if (IS_ERR(de_old)) {
489 CERROR("old child lookup error %ld\n", PTR_ERR(de_old));
490 GOTO(out_rename_tgtdir, rc = -ESTALE);
493 de_new = lookup_one_len(rec->ur_tgt, de_tgtdir, rec->ur_tgtlen - 1);
494 if (IS_ERR(de_new)) {
495 CERROR("new child lookup error %ld\n", PTR_ERR(de_new));
496 GOTO(out_rename_deold, rc = -ESTALE);
499 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
500 de_srcdir->d_inode->i_sb->s_dev);
502 handle = mds_fs_start(mds, de_tgtdir->d_inode, MDS_FSOP_RENAME);
504 GOTO(out_rename_denew, rc = PTR_ERR(handle));
505 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
508 rc = mds_update_last_rcvd(mds, handle, req);
510 /* FIXME: need to return last_rcvd, last_committed */
511 /* FIXME: keep rc intact */
512 rc = mds_fs_commit(mds, de_tgtdir->d_inode, handle);
528 typedef int (*mds_reinter)(struct mds_update_record *, struct ptlrpc_request*);
530 static mds_reinter reinters[REINT_MAX+1] = {
531 [REINT_SETATTR] mds_reint_setattr,
532 [REINT_CREATE] mds_reint_create,
533 [REINT_UNLINK] mds_reint_unlink,
534 [REINT_LINK] mds_reint_link,
535 [REINT_RENAME] mds_reint_rename,
536 [REINT_RECREATE] mds_reint_recreate,
539 int mds_reint_rec(struct mds_update_record *rec, struct ptlrpc_request *req)
541 int rc, size = sizeof(struct mds_body);
543 if (rec->ur_opcode < 1 || rec->ur_opcode > REINT_MAX) {
544 CERROR("opcode %d not valid\n", rec->ur_opcode);
545 rc = req->rq_status = -EINVAL;
549 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
551 CERROR("mds: out of memory\n");
552 rc = req->rq_status = -ENOMEM;
556 rc = reinters[rec->ur_opcode](rec, req);