Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / mds / mds_open.c
index d12865b..9b613a1 100644 (file)
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/version.h>
-#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
-# include <linux/buffer_head.h>
-# include <linux/workqueue.h>
-#else
-# include <linux/locks.h>
-#endif
+#include <linux/buffer_head.h>
+#include <linux/workqueue.h>
 
 #include <obd_class.h>
 #include <obd_lov.h>
@@ -110,8 +106,7 @@ static void mds_mfd_put(struct mds_file_data *mfd)
         LASSERT(atomic_read(&mfd->mfd_refcount) > 0 &&
                 atomic_read(&mfd->mfd_refcount) < 0x5a5a);
         if (atomic_dec_and_test(&mfd->mfd_refcount)) {
-                LASSERT(list_empty(&mfd->mfd_handle.h_link));
-                OBD_FREE(mfd, sizeof *mfd);
+                OBD_FREE_RCU(mfd, sizeof *mfd, &mfd->mfd_handle);
         }
 }
 
@@ -308,7 +303,7 @@ static int mds_create_objects(struct ptlrpc_request *req, int offset,
                               struct mds_update_record *rec,
                               struct mds_obd *mds, struct obd_device *obd,
                               struct dentry *dchild, void **handle,
-                              obd_id **ids)
+                              struct lov_mds_md **objid)
 {
         struct inode *inode = dchild->d_inode;
         struct obd_trans_info oti = { 0 };
@@ -321,8 +316,7 @@ static int mds_create_objects(struct ptlrpc_request *req, int offset,
 
         if (!S_ISREG(inode->i_mode))
                 RETURN(0);
-        if (rec->ur_flags & MDS_OPEN_DELAY_CREATE ||
-            !(rec->ur_flags & FMODE_WRITE))
+        if (!md_should_create(rec->ur_flags))
                 RETURN(0);
 
         body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF, sizeof(*body));
@@ -330,11 +324,7 @@ static int mds_create_objects(struct ptlrpc_request *req, int offset,
         if (body->valid & OBD_MD_FLEASIZE)
                 RETURN(0);
 
-        OBD_ALLOC(*ids, mds->mds_lov_desc.ld_tgt_count * sizeof(**ids));
-        if (*ids == NULL)
-                RETURN(-ENOMEM);
         oti_init(&oti, req);
-        oti.oti_objid = *ids;
 
         /* replay case */
         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
@@ -356,8 +346,6 @@ static int mds_create_objects(struct ptlrpc_request *req, int offset,
                         GOTO(out_ids, rc);
                 }
 
-                mds_objids_from_lmm(*ids, lmm, &mds->mds_lov_desc);
-
                 rc = fsfilt_set_md(obd, inode, *handle, lmm, lmm_size, "lov");
                 if (rc)
                         CERROR("open replay failed to set md:%d\n", rc);
@@ -365,13 +353,14 @@ static int mds_create_objects(struct ptlrpc_request *req, int offset,
                 LASSERT(lmm_buf);
                 memcpy(lmm_buf, lmm, lmm_size);
 
+                *objid = lmm_buf;
                 RETURN(rc);
         }
 
-        if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_ALLOC_OBDO))
+        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
                 GOTO(out_ids, rc = -ENOMEM);
 
-        oinfo.oi_oa = obdo_alloc();
+        OBDO_ALLOC(oinfo.oi_oa);
         if (oinfo.oi_oa == NULL)
                 GOTO(out_ids, rc = -ENOMEM);
         oinfo.oi_oa->o_uid = 0; /* must have 0 uid / gid on OST */
@@ -409,7 +398,7 @@ static int mds_create_objects(struct ptlrpc_request *req, int offset,
                         if (rc)
                                 GOTO(out_oa, rc);
                 }
-                rc = obd_create(mds->mds_osc_exp, oinfo.oi_oa, 
+                rc = obd_create(mds->mds_osc_exp, oinfo.oi_oa,
                                 &oinfo.oi_md, &oti);
                 if (rc) {
                         int level = D_ERROR;
@@ -432,11 +421,12 @@ static int mds_create_objects(struct ptlrpc_request *req, int offset,
                         GOTO(out_oa, rc);
                 }
                 oinfo.oi_md->lsm_object_id = oinfo.oi_oa->o_id;
+                oinfo.oi_md->lsm_object_gr = oinfo.oi_oa->o_gr;
         }
-        if (inode->i_size) {
-                oinfo.oi_oa->o_size = inode->i_size;
-                obdo_from_inode(oinfo.oi_oa, inode, OBD_MD_FLTYPE | 
-                                OBD_MD_FLATIME | OBD_MD_FLMTIME | 
+        if (i_size_read(inode)) {
+                oinfo.oi_oa->o_size = i_size_read(inode);
+                obdo_from_inode(oinfo.oi_oa, inode, OBD_MD_FLTYPE |
+                                OBD_MD_FLATIME | OBD_MD_FLMTIME |
                                 OBD_MD_FLCTIME | OBD_MD_FLSIZE);
 
                 /* pack lustre id to OST */
@@ -475,22 +465,22 @@ static int mds_create_objects(struct ptlrpc_request *req, int offset,
         if (IS_ERR(*handle)) {
                 rc = PTR_ERR(*handle);
                 *handle = NULL;
-                GOTO(out_oa, rc);
+                GOTO(free_diskmd, rc);
         }
 
         rc = fsfilt_set_md(obd, inode, *handle, lmm, lmm_size, "lov");
         lmm_buf = lustre_msg_buf(req->rq_repmsg, offset, lmm_size);
         LASSERT(lmm_buf);
         memcpy(lmm_buf, lmm, lmm_size);
+
+        *objid = lmm_buf; // save for mds_lov_update_objid
+
+free_diskmd:
         obd_free_diskmd(mds->mds_osc_exp, &lmm);
- out_oa:
+out_oa:
         oti_free_cookies(&oti);
-        obdo_free(oinfo.oi_oa);
- out_ids:
-        if (rc) {
-                OBD_FREE(*ids, mds->mds_lov_desc.ld_tgt_count * sizeof(**ids));
-                *ids = NULL;
-        }
+        OBDO_FREE(oinfo.oi_oa);
+out_ids:
         if (oinfo.oi_md)
                 obd_free_memmd(mds->mds_osc_exp, &oinfo.oi_md);
         RETURN(rc);
@@ -673,7 +663,7 @@ static int mds_finish_open(struct ptlrpc_request *req, struct dentry *dchild,
         struct mds_obd *mds = mds_req2mds(req);
         struct obd_device *obd = req->rq_export->exp_obd;
         struct mds_file_data *mfd = NULL;
-        obd_id *ids = NULL; /* object IDs created */
+        struct lov_mds_md *lmm; /* object IDs created */
         int rc = 0;
         ENTRY;
 
@@ -706,7 +696,7 @@ static int mds_finish_open(struct ptlrpc_request *req, struct dentry *dchild,
                     !(body->valid & OBD_MD_FLMODEASIZE)) {
                         /* no EA: create objects */
                         rc = mds_create_objects(req, DLM_REPLY_REC_OFF + 1, rec,
-                                                mds, obd, dchild, handle, &ids);
+                                                mds, obd, dchild, handle, &lmm);
                         if (rc) {
                                 CERROR("mds_create_objects: rc = %d\n", rc);
                                 UNLOCK_INODE_MUTEX(dchild->d_inode);
@@ -746,10 +736,9 @@ static int mds_finish_open(struct ptlrpc_request *req, struct dentry *dchild,
         CDEBUG(D_INODE, "mfd %p, cookie "LPX64"\n", mfd,
                mfd->mfd_handle.h_cookie);
 
-        if (ids != NULL) {
-                mds_lov_update_objids(obd, ids);
-                OBD_FREE(ids, sizeof(*ids) * mds->mds_lov_desc.ld_tgt_count);
-        }
+        if (lmm != NULL)
+                mds_lov_update_objids(obd, lmm);
+
         if (rc) /* coverity[deadcode] */
                 mds_mfd_unlink(mfd, 1);
 
@@ -797,7 +786,7 @@ static int mds_open_by_fid(struct ptlrpc_request *req, struct ll_fid *fid,
 
         rc = mds_finish_open(req, dchild, body, flags, &handle, rec, rep, NULL);
         rc = mds_finish_transno(mds, dchild->d_inode, handle,
-                                req, rc, rep ? rep->lock_policy_res1 : 0);
+                                req, rc, rep ? rep->lock_policy_res1 : 0, 0);
         /* XXX what do we do here if mds_finish_transno itself failed? */
 
         l_dput(dchild);
@@ -817,6 +806,7 @@ int mds_pin(struct ptlrpc_request *req, int offset)
         rc = lustre_pack_reply(req, 2, size, NULL);
         if (rc)
                 RETURN(rc);
+
         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
                                  sizeof(*repbody));
 
@@ -843,7 +833,7 @@ int mds_lock_new_child(struct obd_device *obd, struct inode *inode,
         if (child_lockh == NULL)
                 child_lockh = &lockh;
 
-        rc = ldlm_cli_enqueue_local(obd->obd_namespace, child_res_id,
+        rc = ldlm_cli_enqueue_local(obd->obd_namespace, &child_res_id,
                                     LDLM_PLAIN, NULL, LCK_EX, &lock_flags,
                                     ldlm_blocking_ast, ldlm_completion_ast,
                                     NULL, NULL, 0, NULL, child_lockh);
@@ -882,8 +872,8 @@ int mds_open(struct mds_update_record *rec, int offset,
 
         mds_counter_incr(req->rq_export, LPROC_MDS_OPEN);
 
-        OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PAUSE_OPEN | OBD_FAIL_ONCE,
-                         (obd_timeout + 1) / 4);
+        OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_MDS_PAUSE_OPEN, OBD_FAIL_ONCE,
+                               (obd_timeout + 1) / 4);
 
         CLASSERT(MAXQUOTAS < 4);
         if (offset == DLM_INTENT_REC_OFF) { /* intent */
@@ -954,9 +944,7 @@ int mds_open(struct mds_update_record *rec, int offset,
         if (rec->ur_flags & (MDS_OPEN_CREAT | MDS_OPEN_JOIN_FILE))
                 parent_mode = LCK_EX;
         dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, parent_mode,
-                                        &parent_lockh, rec->ur_name,
-                                        rec->ur_namelen - 1,
-                                        MDS_INODELOCK_UPDATE);
+                                        &parent_lockh, MDS_INODELOCK_UPDATE);
         if (IS_ERR(dparent)) {
                 rc = PTR_ERR(dparent);
                 if (rc != -ENOENT) {
@@ -982,7 +970,20 @@ int mds_open(struct mds_update_record *rec, int offset,
         }
 
         /* Step 2: Lookup the child */
-        dchild = ll_lookup_one_len(rec->ur_name, dparent, rec->ur_namelen - 1);
+
+        if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
+            (rec->ur_flags & MDS_OPEN_LOCK) && (rec->ur_namelen == 1)) {
+                /* hack for nfsd with no_subtree_check, it will use anon
+                 * dentry w/o filename to open the file. the anon dentry's
+                 * parent was set to itself, so rec->ur_fid1 is the file.
+                 * And in MDC it cannot derive the dentry's parent dentry,
+                 * hence the file's name, so we hack here in MDS,
+                 * refer to bug 13030. */
+                dchild = mds_fid2dentry(mds, rec->ur_fid1, NULL);
+        } else {
+                dchild = ll_lookup_one_len(rec->ur_name, dparent,
+                                           rec->ur_namelen - 1);
+        }
         if (IS_ERR(dchild)) {
                 rc = PTR_ERR(dchild);
                 dchild = NULL; /* don't confuse mds_finish_transno() below */
@@ -1127,8 +1128,8 @@ found_child:
                 GOTO(cleanup, rc = -ENOTDIR);
         }
 
-        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_CREATE)) {
-                obd_fail_loc = OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE;
+        if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_OPEN_CREATE,
+                                 OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE)) {
                 GOTO(cleanup, rc = -EAGAIN);
         }
 
@@ -1144,16 +1145,16 @@ found_child:
         else
                 child_mode = LCK_CR;
 
-        if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) && 
+        if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
              (rec->ur_flags & MDS_OPEN_LOCK)) {
                 /* In case of replay we do not get a lock assuming that the
                    caller has it already */
                 child_res_id.name[0] = dchild->d_inode->i_ino;
                 child_res_id.name[1] = dchild->d_inode->i_generation;
 
-                rc = ldlm_cli_enqueue_local(obd->obd_namespace, child_res_id,
-                                            LDLM_IBITS, &policy, child_mode, 
-                                            &lock_flags, ldlm_blocking_ast, 
+                rc = ldlm_cli_enqueue_local(obd->obd_namespace, &child_res_id,
+                                            LDLM_IBITS, &policy, child_mode,
+                                            &lock_flags, ldlm_blocking_ast,
                                             ldlm_completion_ast, NULL, NULL,
                                             0, NULL, child_lockh);
                 if (rc != ELDLM_OK)
@@ -1179,7 +1180,7 @@ found_child:
 
  cleanup:
         rc = mds_finish_transno(mds, dchild ? dchild->d_inode : NULL, handle,
-                                req, rc, rep ? rep->lock_policy_res1 : 0);
+                                req, rc, rep ? rep->lock_policy_res1 : 0, 0);
 
  cleanup_no_trans:
         switch (cleanup_phase) {
@@ -1216,6 +1217,13 @@ found_child:
         /* trigger dqacq on the owner of child and parent */
         lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
                       FSFILT_OP_CREATE);
+
+        /* If we have not taken the "open" lock, we may not return 0 here,
+           because caller expects 0 to mean "lock is taken", and it needs
+           nonzero return here for caller to return EDLM_LOCK_ABORTED to
+           client. Later caller should rewrite the return value back to zero
+           if it to be used any further
+         */
         RETURN(rc);
 }
 
@@ -1228,9 +1236,9 @@ found_child:
  * (it will not even _have_ an entry in last_rcvd anymore).
  *
  * Returns EAGAIN if the client needs to get more data and re-close. */
-int mds_mfd_close(struct ptlrpc_request *req, int offset,
-                  struct obd_device *obd, struct mds_file_data *mfd,
-                  int unlink_orphan, struct lov_mds_md *lmm, int lmm_size,
+int mds_mfd_close(struct ptlrpc_request *req, int offset,struct obd_device *obd,
+                  struct mds_file_data *mfd, int unlink_orphan,
+                  struct lov_mds_md *lmm, int lmm_size,
                   struct llog_cookie *logcookies, int cookies_size,
                   __u64 *valid)
 {
@@ -1259,8 +1267,7 @@ int mds_mfd_close(struct ptlrpc_request *req, int offset,
                inode->i_nlink, mds_orphan_open_count(inode));
 
         last_orphan = mds_orphan_open_dec_test(inode) &&
-                mds_inode_is_orphan(inode);
-        MDS_UP_WRITE_ORPHAN_SEM(inode);
+                      mds_inode_is_orphan(inode);
 
         /* this is half of the actual "close" */
         if (mfd->mfd_mode & FMODE_WRITE) {
@@ -1269,12 +1276,15 @@ int mds_mfd_close(struct ptlrpc_request *req, int offset,
         } else if (mfd->mfd_mode & MDS_FMODE_EXEC) {
                 mds_allow_write_access(inode);
         }
+        /* here writecount change also needs protection from orphan write sem. 
+         * so drop orphan write sem after mds_put_write_access, bz 12888. */
+        MDS_UP_WRITE_ORPHAN_SEM(inode);
 
         if (last_orphan && unlink_orphan) {
                 int stripe_count = 0;
                 LASSERT(rc == 0); /* mds_put_write_access must have succeeded */
 
-                CDEBUG(D_HA, "destroying orphan object %s\n", fidname);
+                CDEBUG(D_INODE, "destroying orphan object %s\n", fidname);
 
                 if ((S_ISREG(inode->i_mode) && inode->i_nlink != 1) ||
                     (S_ISDIR(inode->i_mode) && inode->i_nlink != 2))
@@ -1313,6 +1323,7 @@ int mds_mfd_close(struct ptlrpc_request *req, int offset,
                         handle = NULL;
                         GOTO(cleanup, rc);
                 }
+
                 if (lmm != NULL && (*valid & OBD_MD_FLEASIZE) &&
                     mds_log_op_unlink(obd, lmm, lmm_size,
                                       logcookies, cookies_size) > 0) {
@@ -1390,7 +1401,7 @@ out:
 
  cleanup:
         if (req != NULL && reply_body != NULL) {
-                rc = mds_finish_transno(mds, pending_dir, handle, req, rc, 0);
+                rc = mds_finish_transno(mds, pending_dir, handle, req, rc, 0, 0);
         } else if (handle) {
                 int err = fsfilt_commit(obd, pending_dir, handle, 0);
                 if (err) {
@@ -1429,15 +1440,13 @@ int mds_close(struct ptlrpc_request *req, int offset)
         ENTRY;
 
         rc = lustre_pack_reply(req, 4, repsize, NULL);
-        if (rc) {
-                CERROR("lustre_pack_reply: rc = %d\n", rc);
+        if (rc)
                 req->rq_status = rc;
                 /* continue on to drop local open even if we can't send reply */
-        } else {
+        else
                 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
-        }
 
-        CDEBUG(D_HA, "close req->rep_len %d mdsize %d cookiesize %d\n",
+        CDEBUG(D_INODE, "close req->rep_len %d mdsize %d cookiesize %d\n",
                req->rq_replen,
                obd->u.mds.mds_max_mdsize, obd->u.mds.mds_max_cookiesize);
         mds_counter_incr(req->rq_export, LPROC_MDS_CLOSE);
@@ -1450,9 +1459,6 @@ int mds_close(struct ptlrpc_request *req, int offset)
                 RETURN(-EFAULT);
         }
 
-        if (body->flags & MDS_BFLAG_UNCOMMITTED_WRITES)
-                /* do some stuff */ ;
-
         spin_lock(&med->med_open_lock);
         mfd = mds_handle2mfd(&body->handle);
         if (mfd == NULL) {
@@ -1520,10 +1526,8 @@ int mds_done_writing(struct ptlrpc_request *req, int offset)
         }
 
         rc = lustre_pack_reply(req, 2, size, NULL);
-        if (rc) {
-                CERROR("lustre_pack_reply: rc = %d\n", rc);
+        if (rc)
                 req->rq_status = rc;
-        }
 
         RETURN(0);
 }