Whamcloud - gitweb
- except for fixing the segfault in the documentation build, this is
[fs/lustre-release.git] / lustre / mds / mds_reint.c
index a52c02f..6d2659f 100644 (file)
@@ -27,6 +27,8 @@
 #include <linux/lustre_dlm.h>
 #include <linux/obd_class.h>
 
+extern inline struct mds_obd *mds_req2mds(struct ptlrpc_request *req);
+
 struct mds_client_info *mds_uuid_to_mci(struct mds_obd *mds, __u8 *uuid)
 {
         struct list_head *p;
@@ -48,11 +50,11 @@ struct mds_client_info *mds_uuid_to_mci(struct mds_obd *mds, __u8 *uuid)
         return NULL;
 }
 
+/* Assumes caller has already pushed us into the kernel context. */
 int mds_update_last_rcvd(struct mds_obd *mds, void *handle,
                          struct ptlrpc_request *req)
 {
         /* get from req->rq_connection-> or req->rq_client */
-        struct obd_run_ctxt saved;
         struct mds_client_info *mci;
         loff_t off;
         int rc;
@@ -75,10 +77,8 @@ int mds_update_last_rcvd(struct mds_obd *mds, void *handle,
         mci->mci_mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
 
         mds_fs_set_last_rcvd(mds, handle);
-        push_ctxt(&saved, &mds->mds_ctxt);
         rc = lustre_fwrite(mds->mds_rcvd_filp, (char *)mci->mci_mcd,
                            sizeof(*mci->mci_mcd), &off);
-        pop_ctxt(&saved);
         CDEBUG(D_INODE, "wrote trans #%Ld for client '%s' at #%d: rc = %d\n",
                mds->mds_last_rcvd, mci->mci_mcd->mcd_uuid, mci->mci_off, rc);
         // store new value and last committed value in req struct
@@ -97,7 +97,7 @@ int mds_update_last_rcvd(struct mds_obd *mds, void *handle,
 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
                              struct ptlrpc_request *req)
 {
-        struct mds_obd *mds = &req->rq_obd->u.mds;
+        struct mds_obd *mds = mds_req2mds(req);
         struct dentry *de;
         void *handle;
         int rc = 0;
@@ -139,7 +139,7 @@ static int mds_reint_recreate(struct mds_update_record *rec, int offset,
                             struct ptlrpc_request *req)
 {
         struct dentry *de = NULL;
-        struct mds_obd *mds = &req->rq_obd->u.mds;
+        struct mds_obd *mds = mds_req2mds(req);
         struct dentry *dchild = NULL;
         struct inode *dir;
         int rc = 0;
@@ -188,11 +188,10 @@ static int mds_reint_create(struct mds_update_record *rec, int offset,
                             struct ptlrpc_request *req)
 {
         struct dentry *de = NULL;
-        struct mds_obd *mds = &req->rq_obd->u.mds;
+        struct mds_obd *mds = mds_req2mds(req);
         struct dentry *dchild = NULL;
         struct inode *dir;
         void *handle;
-        struct ldlm_lock *lock;
         struct lustre_handle lockh;
         int rc = 0, err, flags, lock_mode, type = rec->ur_mode & S_IFMT;
         __u64 res_id[3] = {0,0,0};
@@ -202,7 +201,7 @@ static int mds_reint_create(struct mds_update_record *rec, int offset,
         if (offset)
                 offset = 1;
 
-        if (strcmp(req->rq_obd->obd_type->typ_name, "mds") != 0)
+        if (strcmp(req->rq_export->export_obd->obd_type->typ_name, "mds") != 0)
                 LBUG();
 
         de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
@@ -217,10 +216,12 @@ static int mds_reint_create(struct mds_update_record *rec, int offset,
         lock_mode = (req->rq_reqmsg->opc == MDS_REINT) ? LCK_CW : LCK_PW;
         res_id[0] = dir->i_ino;
 
-        rc = ldlm_local_lock_match(mds->mds_local_namespace, res_id, LDLM_PLAIN,
-                                   NULL, 0, lock_mode, &lockh);
+        rc = ldlm_lock_match(mds->mds_local_namespace, res_id, LDLM_PLAIN,
+                             NULL, 0, lock_mode, &lockh);
         if (rc == 0) {
+                LDLM_DEBUG_NOLOCK("enqueue res %Lu", res_id[0]);
                 rc = ldlm_cli_enqueue(mds->mds_ldlm_client, mds->mds_ldlm_conn,
+                                      (struct lustre_handle *)&mds->mds_connh,
                                       NULL, mds->mds_local_namespace, NULL,
                                       res_id, LDLM_PLAIN, NULL, 0, lock_mode,
                                       &flags, (void *)mds_lock_callback, NULL,
@@ -245,13 +246,15 @@ static int mds_reint_create(struct mds_update_record *rec, int offset,
                 struct mds_body *body;
                 struct obdo *obdo;
                 struct inode *inode = dchild->d_inode;
-                CERROR("child exists (dir %ld, name %s, ino %ld)\n",
+                CDEBUG(D_INODE, "child exists (dir %ld, name %s, ino %ld)\n",
                        dir->i_ino, rec->ur_name, dchild->d_inode->i_ino);
 
                 body = lustre_msg_buf(req->rq_repmsg, offset);
                 mds_pack_inode2fid(&body->fid1, inode);
                 mds_pack_inode2body(body, inode);
-                if (S_ISREG(inode->i_mode)) {
+#warning FIXME: This ext3/N-specific code does not belong here
+                /* If i_file_acl is set, this inode has an EA */
+                if (S_ISREG(inode->i_mode) && inode->u.ext3_i.i_file_acl) {
                         obdo = lustre_msg_buf(req->rq_repmsg, offset + 1);
                         mds_fs_get_obdo(mds, inode, obdo);
                 }
@@ -261,6 +264,12 @@ static int mds_reint_create(struct mds_update_record *rec, int offset,
 
         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb->s_dev);
 
+        if (dir->i_mode & S_ISGID) {
+                rec->ur_gid = dir->i_gid;
+                if (S_ISDIR(rec->ur_mode))
+                        rec->ur_mode |= S_ISGID;
+        }
+
         switch (type) {
         case S_IFREG: {
                 handle = mds_fs_start(mds, dir, MDS_FSOP_CREATE);
@@ -305,7 +314,8 @@ static int mds_reint_create(struct mds_update_record *rec, int offset,
 
         if (rc) {
                 CERROR("error during create: %d\n", rc);
-                if (rc != -ENOSPC) LBUG();
+                if (rc != -ENOSPC)
+                        LBUG();
                 GOTO(out_create_commit, rc);
         } else {
                 struct iattr iattr;
@@ -313,13 +323,15 @@ static int mds_reint_create(struct mds_update_record *rec, int offset,
                 struct mds_body *body;
 
                 CDEBUG(D_INODE, "created ino %ld\n", dchild->d_inode->i_ino);
-                if (type == S_IFREG) {
+                if (!offset && type == S_IFREG) {
                         struct obdo *obdo;
                         obdo = lustre_msg_buf(req->rq_reqmsg, 2);
                         rc = mds_fs_set_obdo(mds, inode, handle, obdo);
-                        if (rc)
-                                CERROR("error %d setting objid for %ld\n",
+                        if (rc) {
+                                CERROR("error %d setting obdo for %ld\n",
                                        rc, inode->i_ino);
+                                GOTO(out_create_unlink, rc);
+                        }
                 }
 
                 iattr.ia_atime = rec->ur_time;
@@ -357,12 +369,30 @@ out_create_commit:
 out_create_dchild:
         l_dput(dchild);
         up(&dir->i_sem);
-        lock = lustre_handle2object(&lockh);
-        ldlm_lock_decref(lock, lock_mode);
+        ldlm_lock_decref(&lockh, lock_mode);
 out_create_de:
         l_dput(de);
         req->rq_status = rc;
         return 0;
+
+out_create_unlink:
+        /* Destroy the file we just created.  This should not need extra
+         * journal credits, as we have already modified all of the blocks
+         * needed in order to create the file in the first place.
+         */
+        switch(type) {
+        case S_IFDIR:
+                err = vfs_rmdir(dir, dchild);
+                if (err)
+                        CERROR("failed rmdir in error path: rc = %d\n", err);
+                break;
+        default:
+                err = vfs_unlink(dir, dchild);
+                if (err)
+                        CERROR("failed unlink in error path: rc = %d\n", err);
+        }
+
+        goto out_create_commit;
 }
 
 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
@@ -370,8 +400,12 @@ static int mds_reint_unlink(struct mds_update_record *rec, int offset,
 {
         struct dentry *de = NULL;
         struct dentry *dchild = NULL;
-        struct mds_obd *mds = &req->rq_obd->u.mds;
+        struct mds_obd *mds = mds_req2mds(req);
+        struct obdo *obdo;
         struct inode *dir, *inode;
+        int lock_mode, flags;
+        __u64 res_id[3] = {0};
+        struct lustre_handle lockh;
         void *handle;
         int rc = 0;
         int err;
@@ -382,8 +416,29 @@ static int mds_reint_unlink(struct mds_update_record *rec, int offset,
                 LBUG();
                 GOTO(out_unlink, rc = -ESTALE);
         }
+
         dir = de->d_inode;
         CDEBUG(D_INODE, "parent ino %ld\n", dir->i_ino);
+        lock_mode = (req->rq_reqmsg->opc == MDS_REINT) ? LCK_CW : LCK_PW;
+        res_id[0] = dir->i_ino;
+
+        rc = ldlm_lock_match(mds->mds_local_namespace, res_id, LDLM_PLAIN,
+                                   NULL, 0, lock_mode, &lockh);
+        if (rc == 0) {
+                LDLM_DEBUG_NOLOCK("enqueue res %Lu", res_id[0]);
+                rc = ldlm_cli_enqueue(mds->mds_ldlm_client, mds->mds_ldlm_conn,
+                                      (struct lustre_handle *)&mds->mds_connh,
+                                      NULL, mds->mds_local_namespace, NULL,
+                                      res_id, LDLM_PLAIN, NULL, 0, lock_mode,
+                                      &flags, (void *)mds_lock_callback, NULL,
+                                      0, &lockh);
+                if (rc != ELDLM_OK) {
+                        CERROR("lock enqueue: err: %d\n", rc);
+                        GOTO(out_unlink_de, rc = -EIO);
+                }
+        } else
+
+        ldlm_lock_dump((void *)(unsigned long)lockh.addr);
 
         down(&dir->i_sem);
         dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
@@ -397,10 +452,10 @@ static int mds_reint_unlink(struct mds_update_record *rec, int offset,
         if (!inode) {
                 CERROR("child doesn't exist (dir %ld, name %s\n",
                        dir->i_ino, rec->ur_name);
-                LBUG();
-                GOTO(out_unlink_dchild, rc = -ESTALE);
+                GOTO(out_unlink_dchild, rc = -ENOENT);
         }
 
+#if 0 /* in intent case the client doesn't have the inode */
         if (inode->i_ino != rec->ur_fid2->id) {
                 CERROR("inode and FID ID do not match (%ld != %Ld)\n",
                        inode->i_ino, rec->ur_fid2->id);
@@ -413,6 +468,7 @@ static int mds_reint_unlink(struct mds_update_record *rec, int offset,
                 LBUG();
                 GOTO(out_unlink_dchild, rc = -ESTALE);
         }
+#endif
 
         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dir->i_sb->s_dev);
 
@@ -424,6 +480,14 @@ static int mds_reint_unlink(struct mds_update_record *rec, int offset,
                 rc = vfs_rmdir(dir, dchild);
                 break;
         default:
+                if (offset) {
+                        obdo = lustre_msg_buf(req->rq_repmsg, 1); 
+                        rc = mds_fs_get_obdo(mds, inode, obdo);
+                        if (rc < 0)
+                                CDEBUG(D_INFO, "No obdo for ino %ld err %d\n",
+                                       inode->i_ino, rc);
+                }
+
                 handle = mds_fs_start(mds, dir, MDS_FSOP_UNLINK);
                 if (!handle)
                         GOTO(out_unlink_dchild, rc = PTR_ERR(handle));
@@ -442,10 +506,41 @@ static int mds_reint_unlink(struct mds_update_record *rec, int offset,
 
         EXIT;
 out_unlink_dchild:
+        if (!rc)
+                res_id[0] = inode->i_ino;
         l_dput(dchild);
 out_unlink_de:
         up(&dir->i_sem);
+        ldlm_lock_decref(&lockh
+, lock_mode);
+        if (!rc) { 
+                /* Take an exclusive lock on the resource that we're
+                 * about to free, to force everyone to drop their
+                 * locks. */
+                LDLM_DEBUG_NOLOCK("getting EX lock res %Lu", res_id[0]);
+                rc = ldlm_cli_enqueue(mds->mds_ldlm_client, mds->mds_ldlm_conn,
+                                      (struct lustre_handle *)&mds->mds_connh,
+                                      NULL, mds->mds_local_namespace, NULL, 
+                                      res_id,
+                                      LDLM_PLAIN, NULL, 0, LCK_EX, &flags,
+                                      (void *)mds_lock_callback, NULL, 0, 
+                                      &lockh);
+                if (rc) 
+                        CERROR("failed to get child inode lock (child ino %Ld, "
+                               "dir ino %ld)\n",
+                               res_id[0], de->d_inode->i_ino);
+        }
+
         l_dput(de);
+
+        if (!rc) { 
+                ldlm_lock_decref(&lockh, LCK_EX);
+                rc = ldlm_cli_cancel(&lockh, NULL);
+                if (rc < 0)
+                        CERROR("failed to cancel child inode lock ino "
+                               "%Ld: %d\n", res_id[0], rc);
+        }
+
 out_unlink:
         req->rq_status = rc;
         return 0;
@@ -457,7 +552,7 @@ static int mds_reint_link(struct mds_update_record *rec, int offset,
         struct dentry *de_src = NULL;
         struct dentry *de_tgt_dir = NULL;
         struct dentry *dchild = NULL;
-        struct mds_obd *mds = &req->rq_obd->u.mds;
+        struct mds_obd *mds = mds_req2mds(req);
         void *handle;
         int rc = 0;
         int err;
@@ -525,7 +620,7 @@ static int mds_reint_rename(struct mds_update_record *rec, int offset,
         struct dentry *de_tgtdir = NULL;
         struct dentry *de_old = NULL;
         struct dentry *de_new = NULL;
-        struct mds_obd *mds = &req->rq_obd->u.mds;
+        struct mds_obd *mds = mds_req2mds(req);
         void *handle;
         int rc = 0;
         int err;
@@ -603,6 +698,9 @@ static mds_reinter reinters[REINT_MAX+1] = {
 int mds_reint_rec(struct mds_update_record *rec, int offset,
                   struct ptlrpc_request *req)
 {
+        struct mds_obd *mds = mds_req2mds(req);
+        struct obd_run_ctxt saved;
+
         int rc;
 
         if (rec->ur_opcode < 1 || rec->ur_opcode > REINT_MAX) {
@@ -611,7 +709,9 @@ int mds_reint_rec(struct mds_update_record *rec, int offset,
                 RETURN(rc);
         }
 
+        push_ctxt(&saved, &mds->mds_ctxt);
         rc = reinters[rec->ur_opcode](rec, offset, req);
+        pop_ctxt(&saved);
 
         return rc;
 }