+/* lock object for open */
+static int mdt_object_open_lock(struct mdt_thread_info *info,
+ struct mdt_object *obj,
+ struct mdt_lock_handle *lhc,
+ __u64 *ibits)
+{
+ struct md_attr *ma = &info->mti_attr;
+ __u64 open_flags = info->mti_spec.sp_cr_flags;
+ ldlm_mode_t lm = LCK_CR;
+ bool acq_lease = !!(open_flags & MDS_OPEN_LEASE);
+ bool try_layout = false;
+ bool create_layout = false;
+ int rc = 0;
+ ENTRY;
+
+ *ibits = 0;
+ mdt_lock_handle_init(lhc);
+
+ if (req_is_replay(mdt_info_req(info)))
+ RETURN(0);
+
+ if (S_ISREG(lu_object_attr(&obj->mot_obj))) {
+ if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV) &&
+ md_should_create(open_flags))
+ create_layout = true;
+ if (exp_connect_layout(info->mti_exp) && !create_layout &&
+ ma->ma_need & MA_LOV)
+ try_layout = true;
+ }
+
+ if (acq_lease) {
+ /* lease open, acquire write mode of open sem */
+ down_write(&obj->mot_open_sem);
+
+ /* Lease exists and ask for new lease */
+ if (atomic_read(&obj->mot_lease_count) > 0) {
+ /* only exclusive open is supported, so lease
+ * are conflicted to each other */
+ GOTO(out, rc = -EBUSY);
+ }
+
+ /* Lease must be with open lock */
+ if (!(open_flags & MDS_OPEN_LOCK)) {
+ CERROR("Request lease for file:"DFID ", but open lock "
+ "is missed, open_flags = "LPO64".\n",
+ PFID(mdt_object_fid(obj)), open_flags);
+ GOTO(out, rc = -EPROTO);
+ }
+
+ /* XXX: only exclusive open is supported. */
+ lm = LCK_EX;
+ *ibits = MDS_INODELOCK_OPEN;
+
+ /* never grant LCK_EX layout lock to client */
+ try_layout = false;
+ } else { /* normal open */
+ /* normal open holds read mode of open sem */
+ down_read(&obj->mot_open_sem);
+
+ if (open_flags & MDS_OPEN_LOCK) {
+ if (open_flags & FMODE_WRITE)
+ lm = LCK_CW;
+ /* if file is released, we can't deny write because we must
+ * restore (write) it to access it. */
+ else if ((open_flags & MDS_FMODE_EXEC) &&
+ !((ma->ma_valid & MA_HSM) &&
+ (ma->ma_hsm.mh_flags & HS_RELEASED)))
+ lm = LCK_PR;
+ else
+ lm = LCK_CR;
+
+ *ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN;
+ } else if (atomic_read(&obj->mot_lease_count) > 0) {
+ if (open_flags & FMODE_WRITE)
+ lm = LCK_CW;
+ else
+ lm = LCK_CR;
+
+ /* revoke lease */
+ *ibits = MDS_INODELOCK_OPEN;
+ try_layout = false;
+
+ lhc = &info->mti_lh[MDT_LH_LOCAL];
+ }
+ CDEBUG(D_INODE, "normal open:"DFID" lease count: %d, lm: %d\n",
+ PFID(mdt_object_fid(obj)),
+ atomic_read(&obj->mot_open_count), lm);
+ }
+
+ mdt_lock_reg_init(lhc, lm);
+
+ /* one problem to return layout lock on open is that it may result
+ * in too many layout locks cached on the client side. */
+ if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_OPEN) && try_layout) {
+ /* return lookup lock to validate inode at the client side,
+ * this is pretty important otherwise mdt will return layout
+ * lock for each open.
+ * However this is a double-edged sword because changing
+ * permission will revoke huge # of LOOKUP locks. */
+ *ibits |= MDS_INODELOCK_LAYOUT | MDS_INODELOCK_LOOKUP;
+ if (!mdt_object_lock_try(info, obj, lhc, *ibits,
+ MDT_CROSS_LOCK)) {
+ *ibits &= ~(MDS_INODELOCK_LAYOUT|MDS_INODELOCK_LOOKUP);
+ if (*ibits != 0)
+ rc = mdt_object_lock(info, obj, lhc, *ibits,
+ MDT_CROSS_LOCK);
+ }
+ } else if (*ibits != 0) {
+ rc = mdt_object_lock(info, obj, lhc, *ibits, MDT_CROSS_LOCK);
+ }
+
+ CDEBUG(D_INODE, "Requested bits lock:"DFID ", ibits = "LPX64
+ ", open_flags = "LPO64", try_layout = %d, rc = %d\n",
+ PFID(mdt_object_fid(obj)), *ibits, open_flags, try_layout, rc);
+
+ /* will change layout, revoke layout locks by enqueuing EX lock. */
+ if (rc == 0 && create_layout) {
+ struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LAYOUT];
+
+ CDEBUG(D_INODE, "Will create layout, get EX layout lock:"DFID
+ ", open_flags = "LPO64"\n",
+ PFID(mdt_object_fid(obj)), open_flags);
+
+ LASSERT(!try_layout);
+ mdt_lock_handle_init(ll);
+ mdt_lock_reg_init(ll, LCK_EX);
+ rc = mdt_object_lock(info, obj, ll, MDS_INODELOCK_LAYOUT,
+ MDT_LOCAL_LOCK);
+
+ OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LL_BLOCK, 2);
+ }
+
+ /* Check if there is any other open handles after acquiring
+ * open lock. At this point, caching open handles have been revoked
+ * by open lock.
+ * XXX: Now only exclusive open is supported. Need to check the
+ * type of open for generic lease support. */
+ if (rc == 0 && acq_lease) {
+ struct ptlrpc_request *req = mdt_info_req(info);
+ struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
+ struct mdt_file_data *mfd;
+ bool is_replay_or_resent;
+ int open_count = 0;
+
+ /* For lease: application can open a file and then apply lease,
+ * @handle contains original open handle in that case.
+ * In recovery, open REQ will be replayed and the lease REQ may
+ * be resent that means the open handle is already stale, so we
+ * need to fix it up here by finding new handle. */
+ is_replay_or_resent = req_is_replay(req) ||
+ lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT;
+
+ /* if the request is _not_ a replay request, rr_handle
+ * may be used to hold an openhandle which is issuing the
+ * lease request, so that this openhandle doesn't count. */
+ mfd = mdt_handle2mfd(med, info->mti_rr.rr_handle,
+ is_replay_or_resent);
+ if (mfd != NULL)
+ ++open_count;
+
+ CDEBUG(D_INODE, "acq_lease "DFID": openers: %d, want: %d\n",
+ PFID(mdt_object_fid(obj)),
+ atomic_read(&obj->mot_open_count), open_count);
+
+ if (atomic_read(&obj->mot_open_count) > open_count)
+ GOTO(out, rc = -EBUSY);
+ }
+ GOTO(out, rc);
+
+out:
+ RETURN(rc);
+}
+
+static void mdt_object_open_unlock(struct mdt_thread_info *info,
+ struct mdt_object *obj,
+ struct mdt_lock_handle *lhc,
+ __u64 ibits, int rc)
+{
+ __u64 open_flags = info->mti_spec.sp_cr_flags;
+ struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LOCAL];
+ ENTRY;
+
+ if (req_is_replay(mdt_info_req(info)))
+ RETURN_EXIT;
+
+ /* Release local lock - the lock put in MDT_LH_LOCAL will never
+ * return to client side. */
+ if (lustre_handle_is_used(&ll->mlh_reg_lh))
+ mdt_object_unlock(info, obj, ll, 1);
+
+ ll = &info->mti_lh[MDT_LH_LAYOUT];
+ /* Release local layout lock, layout was created */
+ if (lustre_handle_is_used(&ll->mlh_reg_lh)) {
+ LASSERT(!(ibits & MDS_INODELOCK_LAYOUT));
+ mdt_object_unlock(info, obj, ll, 1);
+ }
+
+ if (open_flags & MDS_OPEN_LEASE)
+ up_write(&obj->mot_open_sem);
+ else
+ up_read(&obj->mot_open_sem);
+
+ /* Cross-ref case, the lock should be returned to the client */
+ if (ibits == 0 || rc == -EREMOTE)
+ RETURN_EXIT;
+
+ if (!(open_flags & MDS_OPEN_LOCK) && !(ibits & MDS_INODELOCK_LAYOUT)) {
+ /* for the open request, the lock will only return to client
+ * if open or layout lock is granted. */
+ rc = 1;
+ }
+
+ if (rc != 0) {
+ struct ldlm_reply *ldlm_rep;
+
+ ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
+ mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
+ mdt_object_unlock(info, obj, lhc, 1);
+ }
+ RETURN_EXIT;
+}
+
+/**
+ * Check release is permitted for the current HSM flags.
+ */
+static bool mdt_hsm_release_allow(struct md_attr *ma)
+{
+ if (!(ma->ma_valid & MA_HSM))
+ return false;
+
+ if (ma->ma_hsm.mh_flags & (HS_DIRTY|HS_NORELEASE|HS_LOST))
+ return false;
+
+ if (!(ma->ma_hsm.mh_flags & HS_ARCHIVED))
+ return false;
+
+ return true;
+}
+
+int mdt_open_by_fid_lock(struct mdt_thread_info *info, struct ldlm_reply *rep,
+ struct mdt_lock_handle *lhc)
+{
+ const struct lu_env *env = info->mti_env;
+ struct mdt_device *mdt = info->mti_mdt;
+ __u64 flags = info->mti_spec.sp_cr_flags;
+ struct mdt_reint_record *rr = &info->mti_rr;
+ struct md_attr *ma = &info->mti_attr;
+ struct mdt_object *parent= NULL;
+ struct mdt_object *o;
+ int rc;
+ __u64 ibits = 0;
+ ENTRY;
+
+ if (md_should_create(flags) && !(flags & MDS_OPEN_HAS_EA)) {
+ if (!lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
+ parent = mdt_object_find(env, mdt, rr->rr_fid1);
+ if (IS_ERR(parent)) {
+ CDEBUG(D_INODE, "Fail to find parent "DFID
+ " for anonymous created %ld, try to"
+ " use server-side parent.\n",
+ PFID(rr->rr_fid1), PTR_ERR(parent));
+ parent = NULL;
+ }
+ }
+ if (parent == NULL)
+ ma->ma_need |= MA_PFID;
+ }
+
+ o = mdt_object_find(env, mdt, rr->rr_fid2);
+ if (IS_ERR(o))
+ RETURN(rc = PTR_ERR(o));
+
+ if (mdt_object_remote(o)) {
+ CDEBUG(D_INFO, "%s: "DFID" is on remote MDT.\n",
+ mdt_obd_name(info->mti_mdt),
+ PFID(rr->rr_fid2));
+ GOTO(out, rc = -EREMOTE);
+ } else if (!mdt_object_exists(o)) {
+ mdt_set_disposition(info, rep,
+ DISP_IT_EXECD |
+ DISP_LOOKUP_EXECD |
+ DISP_LOOKUP_NEG);
+ GOTO(out, rc = -ENOENT);
+ }
+
+ mdt_set_disposition(info, rep, (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
+
+ if (flags & MDS_OPEN_RELEASE)
+ ma->ma_need |= MA_HSM;
+ rc = mdt_attr_get_complex(info, o, ma);
+ if (rc)
+ GOTO(out, rc);
+
+ /* If a release request, check file flags are fine and ask for an
+ * exclusive open access. */
+ if (flags & MDS_OPEN_RELEASE && !mdt_hsm_release_allow(ma))
+ GOTO(out, rc = -EPERM);
+
+ rc = mdt_object_open_lock(info, o, lhc, &ibits);
+ if (rc)
+ GOTO(out_unlock, rc);
+
+ if (ma->ma_valid & MA_PFID) {
+ parent = mdt_object_find(env, mdt, &ma->ma_pfid);
+ if (IS_ERR(parent)) {
+ CDEBUG(D_INODE, "Fail to find parent "DFID
+ " for anonymous created %ld, try to"
+ " use system default.\n",
+ PFID(&ma->ma_pfid), PTR_ERR(parent));
+ parent = NULL;
+ }
+ }
+
+ rc = mdt_finish_open(info, parent, o, flags, 0, rep);
+ if (!rc) {
+ mdt_set_disposition(info, rep, DISP_LOOKUP_POS);
+ if (flags & MDS_OPEN_LOCK)
+ mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
+ if (flags & MDS_OPEN_LEASE)
+ mdt_set_disposition(info, rep, DISP_OPEN_LEASE);
+ }
+ GOTO(out_unlock, rc);
+
+out_unlock:
+ mdt_object_open_unlock(info, o, lhc, ibits, rc);
+out:
+ mdt_object_put(env, o);
+ if (parent != NULL)
+ mdt_object_put(env, parent);
+ return rc;
+}
+