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
19 #define DEBUG_SUBSYSTEM S_MDS
21 #include <linux/obd_support.h>
22 #include <linux/obd_class.h>
23 #include <linux/obd.h>
24 #include <linux/lustre_lib.h>
25 #include <linux/lustre_idl.h>
26 #include <linux/lustre_mds.h>
27 #include <linux/lustre_dlm.h>
28 #include <linux/obd_class.h>
30 struct mds_client_info *mds_uuid_to_mci(struct mds_obd *mds, __u8 *uuid)
37 list_for_each(p, &mds->mds_client_info) {
38 struct mds_client_info *mci;
40 mci = list_entry(p, struct mds_client_info, mci_list);
41 CDEBUG(D_INFO, "checking client UUID '%s'\n",
42 mci->mci_mcd->mcd_uuid);
43 if (!strncmp(mci->mci_mcd->mcd_uuid, uuid,
44 sizeof(mci->mci_mcd->mcd_uuid)))
47 CDEBUG(D_INFO, "no mds client info found for UUID '%s'\n", uuid);
51 /* Assumes caller has already pushed us into the kernel context. */
52 int mds_update_last_rcvd(struct mds_obd *mds, void *handle,
53 struct ptlrpc_request *req)
55 /* get from req->rq_connection-> or req->rq_client */
56 struct mds_client_info *mci;
60 mci = mds_uuid_to_mci(mds, req->rq_connection->c_remote_uuid);
62 CERROR("unable to locate MDS client data for UUID '%s'\n",
63 ptlrpc_req_to_uuid(req));
64 /* This will be a real error once everything is working */
69 off = MDS_LR_CLIENT + mci->mci_off * MDS_LR_SIZE;
71 ++mds->mds_last_rcvd; /* lock this, or make it an LDLM function? */
72 req->rq_repmsg->transno = HTON__u64(mds->mds_last_rcvd);
73 mci->mci_mcd->mcd_last_rcvd = cpu_to_le64(mds->mds_last_rcvd);
74 mci->mci_mcd->mcd_mount_count = cpu_to_le64(mds->mds_mount_count);
75 mci->mci_mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
77 mds_fs_set_last_rcvd(mds, handle);
78 rc = lustre_fwrite(mds->mds_rcvd_filp, (char *)mci->mci_mcd,
79 sizeof(*mci->mci_mcd), &off);
80 CDEBUG(D_INODE, "wrote trans #%Ld for client '%s' at #%d: rc = %d\n",
81 mds->mds_last_rcvd, mci->mci_mcd->mcd_uuid, mci->mci_off, rc);
82 // store new value and last committed value in req struct
84 if (rc == sizeof(*mci->mci_mcd))
87 CERROR("error writing to last_rcvd file: rc = %d\n", rc);
95 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
96 struct ptlrpc_request *req)
98 struct mds_obd *mds = &req->rq_obd->u.mds;
104 de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
105 if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_SETATTR)) {
106 GOTO(out_setattr, rc = -ESTALE);
109 CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
111 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE,
112 de->d_inode->i_sb->s_dev);
114 handle = mds_fs_start(mds, de->d_inode, MDS_FSOP_SETATTR);
116 GOTO(out_setattr_de, rc = PTR_ERR(handle));
117 rc = mds_fs_setattr(mds, de, handle, &rec->ur_iattr);
120 rc = mds_update_last_rcvd(mds, handle, req);
122 err = mds_fs_commit(mds, de->d_inode, handle);
124 CERROR("error on commit: err = %d\n", err);
136 static int mds_reint_recreate(struct mds_update_record *rec, int offset,
137 struct ptlrpc_request *req)
139 struct dentry *de = NULL;
140 struct mds_obd *mds = &req->rq_obd->u.mds;
141 struct dentry *dchild = NULL;
146 de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
147 if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE)) {
149 GOTO(out_create_de, rc = -ESTALE);
152 CDEBUG(D_INODE, "parent ino %ld\n", dir->i_ino);
155 dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
156 if (IS_ERR(dchild)) {
157 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
160 GOTO(out_create_dchild, rc = -ESTALE);
163 if (dchild->d_inode) {
164 struct mds_body *body;
166 body = lustre_msg_buf(req->rq_repmsg, 0);
167 body->ino = dchild->d_inode->i_ino;
168 body->generation = dchild->d_inode->i_generation;
170 CERROR("child doesn't exist (dir %ld, name %s)\n",
171 dir->i_ino, rec->ur_name);
185 static int mds_reint_create(struct mds_update_record *rec, int offset,
186 struct ptlrpc_request *req)
188 struct dentry *de = NULL;
189 struct mds_obd *mds = &req->rq_obd->u.mds;
190 struct dentry *dchild = NULL;
193 struct ldlm_lock *lock;
194 struct lustre_handle lockh;
195 int rc = 0, err, flags, lock_mode, type = rec->ur_mode & S_IFMT;
196 __u64 res_id[3] = {0,0,0};
199 /* requests were at offset 2, replies go back at 1 */
203 if (strcmp(req->rq_obd->obd_type->typ_name, "mds") != 0)
206 de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
207 if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE)) {
209 GOTO(out_create_de, rc = -ESTALE);
212 CDEBUG(D_INODE, "parent ino %ld name %s mode %o\n",
213 dir->i_ino, rec->ur_name, rec->ur_mode);
215 lock_mode = (req->rq_reqmsg->opc == MDS_REINT) ? LCK_CW : LCK_PW;
216 res_id[0] = dir->i_ino;
218 rc = ldlm_lock_match(mds->mds_local_namespace, res_id, LDLM_PLAIN,
219 NULL, 0, lock_mode, &lockh);
221 LDLM_DEBUG_NOLOCK("enqueue res %Lu", res_id[0]);
222 rc = ldlm_cli_enqueue(mds->mds_ldlm_client, mds->mds_ldlm_conn,
223 NULL, mds->mds_local_namespace, NULL,
224 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
225 &flags, (void *)mds_lock_callback, NULL,
227 if (rc != ELDLM_OK) {
228 CERROR("lock enqueue: err: %d\n", rc);
229 GOTO(out_create_de, rc = -EIO);
232 lock = lustre_handle2object(&lockh);
233 LDLM_DEBUG(lock, "matched");
235 ldlm_lock_dump((void *)(unsigned long)lockh.addr);
238 dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
239 if (IS_ERR(dchild)) {
240 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
243 GOTO(out_create_dchild, rc = -ESTALE);
246 if (dchild->d_inode) {
247 struct mds_body *body;
249 struct inode *inode = dchild->d_inode;
250 CDEBUG(D_INODE, "child exists (dir %ld, name %s, ino %ld)\n",
251 dir->i_ino, rec->ur_name, dchild->d_inode->i_ino);
253 body = lustre_msg_buf(req->rq_repmsg, offset);
254 mds_pack_inode2fid(&body->fid1, inode);
255 mds_pack_inode2body(body, inode);
256 #warning FIXME: This ext3/N-specific code does not belong here
257 /* If i_file_acl is set, this inode has an EA */
258 if (S_ISREG(inode->i_mode) && inode->u.ext3_i.i_file_acl) {
259 obdo = lustre_msg_buf(req->rq_repmsg, offset + 1);
260 mds_fs_get_obdo(mds, inode, obdo);
262 /* now a normal case for intent locking */
263 GOTO(out_create_dchild, rc = -EEXIST);
266 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb->s_dev);
268 if (dir->i_mode & S_ISGID) {
269 rec->ur_gid = dir->i_gid;
270 if (S_ISDIR(rec->ur_mode))
271 rec->ur_mode |= S_ISGID;
276 handle = mds_fs_start(mds, dir, MDS_FSOP_CREATE);
278 GOTO(out_create_dchild, PTR_ERR(handle));
279 rc = vfs_create(dir, dchild, rec->ur_mode);
284 handle = mds_fs_start(mds, dir, MDS_FSOP_MKDIR);
286 GOTO(out_create_dchild, PTR_ERR(handle));
287 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
292 handle = mds_fs_start(mds, dir, MDS_FSOP_SYMLINK);
294 GOTO(out_create_dchild, PTR_ERR(handle));
295 rc = vfs_symlink(dir, dchild, rec->ur_tgt);
303 int rdev = rec->ur_rdev;
304 handle = mds_fs_start(mds, dir, MDS_FSOP_MKNOD);
306 GOTO(out_create_dchild, PTR_ERR(handle));
307 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
312 CERROR("bad file type %d for create of %s\n",type,rec->ur_name);
313 GOTO(out_create_dchild, rc = -EINVAL);
317 CERROR("error during create: %d\n", rc);
320 GOTO(out_create_commit, rc);
323 struct inode *inode = dchild->d_inode;
324 struct mds_body *body;
326 CDEBUG(D_INODE, "created ino %ld\n", dchild->d_inode->i_ino);
327 if (!offset && type == S_IFREG) {
329 obdo = lustre_msg_buf(req->rq_reqmsg, 2);
330 rc = mds_fs_set_obdo(mds, inode, handle, obdo);
332 CERROR("error %d setting obdo for %ld\n",
334 GOTO(out_create_unlink, rc);
338 iattr.ia_atime = rec->ur_time;
339 iattr.ia_ctime = rec->ur_time;
340 iattr.ia_mtime = rec->ur_time;
341 iattr.ia_uid = rec->ur_uid;
342 iattr.ia_gid = rec->ur_gid;
343 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
344 ATTR_MTIME | ATTR_CTIME;
346 rc = mds_fs_setattr(mds, dchild, handle, &iattr);
348 CERROR("error on setattr: rc = %d\n", rc);
349 /* XXX should we abort here in case of error? */
352 rc = mds_update_last_rcvd(mds, handle, req);
354 CERROR("error on update_last_rcvd: rc = %d\n", rc);
355 /* XXX should we abort here in case of error? */
358 body = lustre_msg_buf(req->rq_repmsg, offset);
359 body->ino = inode->i_ino;
360 body->generation = inode->i_generation;
364 err = mds_fs_commit(mds, dir, handle);
366 CERROR("error on commit: err = %d\n", err);
373 lock = lustre_handle2object(&lockh);
374 ldlm_lock_decref(lock, lock_mode);
381 /* Destroy the file we just created. This should not need extra
382 * journal credits, as we have already modified all of the blocks
383 * needed in order to create the file in the first place.
387 err = vfs_rmdir(dir, dchild);
389 CERROR("failed rmdir in error path: rc = %d\n", err);
392 err = vfs_unlink(dir, dchild);
394 CERROR("failed unlink in error path: rc = %d\n", err);
397 goto out_create_commit;
400 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
401 struct ptlrpc_request *req)
403 struct dentry *de = NULL;
404 struct dentry *dchild = NULL;
405 struct mds_obd *mds = &req->rq_obd->u.mds;
407 struct inode *dir, *inode;
408 int lock_mode, flags;
409 __u64 res_id[3] = {0};
410 struct lustre_handle lockh;
411 struct ldlm_lock *lock;
417 de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
418 if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK)) {
420 GOTO(out_unlink, rc = -ESTALE);
424 CDEBUG(D_INODE, "parent ino %ld\n", dir->i_ino);
425 lock_mode = (req->rq_reqmsg->opc == MDS_REINT) ? LCK_CW : LCK_PW;
426 res_id[0] = dir->i_ino;
428 rc = ldlm_lock_match(mds->mds_local_namespace, res_id, LDLM_PLAIN,
429 NULL, 0, lock_mode, &lockh);
431 LDLM_DEBUG_NOLOCK("enqueue res %Lu", res_id[0]);
432 rc = ldlm_cli_enqueue(mds->mds_ldlm_client, mds->mds_ldlm_conn,
433 NULL, mds->mds_local_namespace, NULL,
434 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
435 &flags, (void *)mds_lock_callback, NULL,
437 if (rc != ELDLM_OK) {
438 CERROR("lock enqueue: err: %d\n", rc);
439 GOTO(out_unlink_de, rc = -EIO);
442 lock = lustre_handle2object(&lockh);
443 LDLM_DEBUG(lock, "matched");
445 ldlm_lock_dump((void *)(unsigned long)lockh.addr);
448 dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
449 if (IS_ERR(dchild)) {
450 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
452 GOTO(out_unlink_de, rc = -ESTALE);
455 inode = dchild->d_inode;
457 CERROR("child doesn't exist (dir %ld, name %s\n",
458 dir->i_ino, rec->ur_name);
459 GOTO(out_unlink_dchild, rc = -ENOENT);
462 #if 0 /* in intent case the client doesn't have the inode */
463 if (inode->i_ino != rec->ur_fid2->id) {
464 CERROR("inode and FID ID do not match (%ld != %Ld)\n",
465 inode->i_ino, rec->ur_fid2->id);
467 GOTO(out_unlink_dchild, rc = -ESTALE);
469 if (inode->i_generation != rec->ur_fid2->generation) {
470 CERROR("inode and FID GENERATION do not match (%d != %d)\n",
471 inode->i_generation, rec->ur_fid2->generation);
473 GOTO(out_unlink_dchild, rc = -ESTALE);
477 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dir->i_sb->s_dev);
479 switch (dchild->d_inode->i_mode & S_IFMT) {
481 handle = mds_fs_start(mds, dir, MDS_FSOP_RMDIR);
483 GOTO(out_unlink_dchild, rc = PTR_ERR(handle));
484 rc = vfs_rmdir(dir, dchild);
488 obdo = lustre_msg_buf(req->rq_repmsg, 1);
489 rc = mds_fs_get_obdo(mds, inode, obdo);
491 CDEBUG(D_INFO, "No obdo for ino %ld err %d\n",
495 handle = mds_fs_start(mds, dir, MDS_FSOP_UNLINK);
497 GOTO(out_unlink_dchild, rc = PTR_ERR(handle));
498 rc = vfs_unlink(dir, dchild);
503 rc = mds_update_last_rcvd(mds, handle, req);
504 err = mds_fs_commit(mds, dir, handle);
506 CERROR("error on commit: err = %d\n", err);
514 res_id[0] = inode->i_ino;
518 lock = lustre_handle2object(&lockh);
519 ldlm_lock_decref(lock, lock_mode);
521 /* Take an exclusive lock on the resource that we're
522 * about to free, to force everyone to drop their
524 LDLM_DEBUG_NOLOCK("getting EX lock res %Lu", res_id[0]);
525 rc = ldlm_cli_enqueue(mds->mds_ldlm_client, mds->mds_ldlm_conn,
526 NULL, mds->mds_local_namespace, NULL,
528 LDLM_PLAIN, NULL, 0, LCK_EX, &flags,
529 (void *)mds_lock_callback, NULL, 0,
532 CERROR("failed to get child inode lock (child ino %Ld, "
534 res_id[0], de->d_inode->i_ino);
540 lock = lustre_handle2object(&lockh);
541 ldlm_lock_decref(lock, LCK_EX);
542 rc = ldlm_cli_cancel(lockh);
544 CERROR("failed to cancel child inode lock ino "
545 "%Ld: %d\n", res_id[0], rc);
553 static int mds_reint_link(struct mds_update_record *rec, int offset,
554 struct ptlrpc_request *req)
556 struct dentry *de_src = NULL;
557 struct dentry *de_tgt_dir = NULL;
558 struct dentry *dchild = NULL;
559 struct mds_obd *mds = &req->rq_obd->u.mds;
565 de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
566 if (IS_ERR(de_src) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK)) {
567 GOTO(out_link, rc = -ESTALE);
570 de_tgt_dir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
571 if (IS_ERR(de_tgt_dir)) {
572 GOTO(out_link_de_src, rc = -ESTALE);
575 down(&de_tgt_dir->d_inode->i_sem);
576 dchild = lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen - 1);
577 if (IS_ERR(dchild)) {
578 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
579 GOTO(out_link_de_tgt_dir, rc = -ESTALE);
582 if (dchild->d_inode) {
583 CERROR("child exists (dir %ld, name %s\n",
584 de_tgt_dir->d_inode->i_ino, rec->ur_name);
585 GOTO(out_link_dchild, rc = -EEXIST);
588 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE,
589 de_src->d_inode->i_sb->s_dev);
591 handle = mds_fs_start(mds, de_tgt_dir->d_inode, MDS_FSOP_LINK);
593 GOTO(out_link_dchild, rc = PTR_ERR(handle));
595 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
598 rc = mds_update_last_rcvd(mds, handle, req);
600 err = mds_fs_commit(mds, de_tgt_dir->d_inode, handle);
602 CERROR("error on commit: err = %d\n", err);
611 up(&de_tgt_dir->d_inode->i_sem);
620 static int mds_reint_rename(struct mds_update_record *rec, int offset,
621 struct ptlrpc_request *req)
623 struct dentry *de_srcdir = NULL;
624 struct dentry *de_tgtdir = NULL;
625 struct dentry *de_old = NULL;
626 struct dentry *de_new = NULL;
627 struct mds_obd *mds = &req->rq_obd->u.mds;
633 de_srcdir = mds_fid2dentry(mds, rec->ur_fid1, NULL);
634 if (IS_ERR(de_srcdir)) {
635 GOTO(out_rename, rc = -ESTALE);
638 de_tgtdir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
639 if (IS_ERR(de_tgtdir)) {
640 GOTO(out_rename_srcdir, rc = -ESTALE);
643 #warning FIXME: This needs locking attention
645 de_old = lookup_one_len(rec->ur_name, de_srcdir, rec->ur_namelen - 1);
646 if (IS_ERR(de_old)) {
647 CERROR("old child lookup error %ld\n", PTR_ERR(de_old));
648 GOTO(out_rename_tgtdir, rc = -ESTALE);
651 de_new = lookup_one_len(rec->ur_tgt, de_tgtdir, rec->ur_tgtlen - 1);
652 if (IS_ERR(de_new)) {
653 CERROR("new child lookup error %ld\n", PTR_ERR(de_new));
654 GOTO(out_rename_deold, rc = -ESTALE);
657 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
658 de_srcdir->d_inode->i_sb->s_dev);
660 handle = mds_fs_start(mds, de_tgtdir->d_inode, MDS_FSOP_RENAME);
662 GOTO(out_rename_denew, rc = PTR_ERR(handle));
663 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new,
667 rc = mds_update_last_rcvd(mds, handle, req);
669 err = mds_fs_commit(mds, de_tgtdir->d_inode, handle);
671 CERROR("error on commit: err = %d\n", err);
690 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
691 struct ptlrpc_request *);
693 static mds_reinter reinters[REINT_MAX+1] = {
694 [REINT_SETATTR] mds_reint_setattr,
695 [REINT_CREATE] mds_reint_create,
696 [REINT_UNLINK] mds_reint_unlink,
697 [REINT_LINK] mds_reint_link,
698 [REINT_RENAME] mds_reint_rename,
699 [REINT_RECREATE] mds_reint_recreate,
702 int mds_reint_rec(struct mds_update_record *rec, int offset,
703 struct ptlrpc_request *req)
705 struct mds_obd *mds = &req->rq_obd->u.mds;
706 struct obd_run_ctxt saved;
710 if (rec->ur_opcode < 1 || rec->ur_opcode > REINT_MAX) {
711 CERROR("opcode %d not valid\n", rec->ur_opcode);
712 rc = req->rq_status = -EINVAL;
716 push_ctxt(&saved, &mds->mds_ctxt);
717 rc = reinters[rec->ur_opcode](rec, offset, req);