+/**
+ * MDT request handler for OST_GETATTR RPC.
+ *
+ * This is data-specific request to get object and layout versions under
+ * IO lock. It is reliable only for Data-on-MDT files.
+ *
+ * \param[in] tsi target session environment for this request
+ *
+ * \retval 0 if successful
+ * \retval negative value on error
+ */
+int mdt_data_version_get(struct tgt_session_info *tsi)
+{
+ struct mdt_thread_info *mti = mdt_th_info(tsi->tsi_env);
+ struct mdt_device *mdt = mti->mti_mdt;
+ struct mdt_body *repbody;
+ struct mdt_object *mo = mti->mti_object;
+ struct lov_comp_md_v1 *comp;
+ struct lustre_handle lh = { 0 };
+ __u64 flags = 0;
+ __s64 version;
+ enum ldlm_mode lock_mode = LCK_PR;
+ bool srvlock;
+ int rc;
+
+ ENTRY;
+
+ req_capsule_set_size(tsi->tsi_pill, &RMF_MDT_MD, RCL_SERVER, 0);
+ req_capsule_set_size(tsi->tsi_pill, &RMF_ACL, RCL_SERVER, 0);
+ rc = req_capsule_server_pack(tsi->tsi_pill);
+ if (unlikely(rc != 0))
+ RETURN(err_serious(rc));
+
+ repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
+ if (repbody == NULL)
+ RETURN(-ENOMEM);
+
+ srvlock = tsi->tsi_mdt_body->mbo_valid & OBD_MD_FLFLAGS &&
+ tsi->tsi_mdt_body->mbo_flags & OBD_FL_SRVLOCK;
+
+ if (srvlock) {
+ if (unlikely(tsi->tsi_mdt_body->mbo_flags & OBD_FL_FLUSH))
+ lock_mode = LCK_PW;
+
+ fid_build_reg_res_name(&tsi->tsi_fid, &tsi->tsi_resid);
+ rc = tgt_mdt_data_lock(mdt->mdt_namespace, &tsi->tsi_resid,
+ &lh, lock_mode, &flags);
+ if (rc != 0)
+ RETURN(rc);
+ }
+
+ if (!mdt_object_exists(mo))
+ GOTO(out, rc = -ENOENT);
+ if (mdt_object_remote(mo))
+ GOTO(out, rc = -EREMOTE);
+ if (!S_ISREG(lu_object_attr(&mo->mot_obj)))
+ GOTO(out, rc = -EBADF);
+
+ /* Get version first */
+ version = dt_version_get(tsi->tsi_env, mdt_obj2dt(mo));
+ if (version && version != -EOPNOTSUPP) {
+ repbody->mbo_valid |= OBD_MD_FLDATAVERSION;
+ /* re-use mbo_ioepoch to transfer version */
+ repbody->mbo_version = version;
+ }
+
+ /* Read layout to get its version */
+ rc = mdt_big_xattr_get(mti, mo, XATTR_NAME_LOV);
+ if (rc == -ENODATA) /* File has no layout yet */
+ GOTO(out, rc = 0);
+ else if (rc < 0)
+ GOTO(out, rc);
+
+ comp = mti->mti_buf.lb_buf;
+ if (le32_to_cpu(comp->lcm_magic) != LOV_MAGIC_COMP_V1) {
+ CDEBUG(D_INFO, DFID" has no composite layout",
+ PFID(&tsi->tsi_fid));
+ GOTO(out, rc = -ESTALE);
+ }
+
+ CDEBUG(D_INODE, DFID": layout version: %u\n",
+ PFID(&tsi->tsi_fid), le32_to_cpu(comp->lcm_layout_gen));
+
+ repbody->mbo_valid |= OBD_MD_LAYOUT_VERSION;
+ /* re-use mbo_rdev for that */
+ repbody->mbo_layout_gen = le32_to_cpu(comp->lcm_layout_gen);
+ rc = 0;
+out:
+ if (srvlock)
+ tgt_data_unlock(&lh, lock_mode);
+
+ repbody->mbo_valid |= OBD_MD_FLFLAGS;
+ repbody->mbo_flags = OBD_FL_FLUSH;
+ RETURN(rc);
+}
+
+/* read file data to the buffer */
+int mdt_dom_read_on_open(struct mdt_thread_info *mti, struct mdt_device *mdt,
+ struct lustre_handle *lh)
+{
+ const struct lu_env *env = mti->mti_env;
+ struct tgt_session_info *tsi = tgt_ses_info(env);
+ struct req_capsule *pill = tsi->tsi_pill;
+ const struct lu_fid *fid;
+ struct ptlrpc_request *req = tgt_ses_req(tsi);
+ struct mdt_body *mbo;
+ struct dt_device *dt = mdt->mdt_bottom;
+ struct dt_object *mo;
+ void *buf;
+ struct niobuf_remote *rnb = NULL;
+ struct niobuf_local *lnb;
+ int rc;
+ loff_t offset;
+ unsigned int len, copied = 0;
+ __u64 real_dom_size;
+ int lnbs, nr_local, i;
+ bool dom_lock = false;
+
+ ENTRY;
+
+ if (!req_capsule_field_present(pill, &RMF_NIOBUF_INLINE, RCL_SERVER)) {
+ /* There is no reply buffers for this field, this means that
+ * client has no support for data in reply.
+ */
+ RETURN(0);
+ }
+
+ mbo = req_capsule_server_get(pill, &RMF_MDT_BODY);
+ if (!(mbo->mbo_valid & OBD_MD_DOM_SIZE))
+ RETURN(0);
+
+ if (!mbo->mbo_dom_size)
+ RETURN(0);
+
+ if (lustre_handle_is_used(lh)) {
+ struct ldlm_lock *lock;
+
+ lock = ldlm_handle2lock(lh);
+ if (lock) {
+ dom_lock = ldlm_has_dom(lock) && ldlm_has_layout(lock);
+ LDLM_LOCK_PUT(lock);
+ }
+ }
+
+ /* return data along with open only along with DoM lock */
+ if (!dom_lock || !mdt->mdt_opts.mo_dom_read_open)
+ RETURN(0);
+
+ /* if DoM object holds encrypted content, we need to make sure we
+ * send whole encryption units, or client will read corrupted content
+ */
+ if (mbo->mbo_valid & LA_FLAGS && mbo->mbo_flags & LUSTRE_ENCRYPT_FL &&
+ mbo->mbo_dom_size & ~LUSTRE_ENCRYPTION_MASK)
+ real_dom_size = (mbo->mbo_dom_size & LUSTRE_ENCRYPTION_MASK) +
+ LUSTRE_ENCRYPTION_UNIT_SIZE;
+ else
+ real_dom_size = mbo->mbo_dom_size;
+
+ CDEBUG(D_INFO, "File size %llu, reply sizes %d/%d\n",
+ real_dom_size, req->rq_reqmsg->lm_repsize, req->rq_replen);
+ len = req->rq_reqmsg->lm_repsize - req->rq_replen;
+
+ /* NB: at this moment we have the following sizes:
+ * - req->rq_replen: used data in reply
+ * - req->rq_reqmsg->lm_repsize: total allocated reply buffer at client
+ *
+ * Ideal case when file size fits in allocated reply buffer,
+ * that mean we can return whole data in reply. We can also fit more
+ * data up to max_reply_size in total reply size, but this will cause
+ * re-allocation on client and resend with larger buffer. This is still
+ * faster than separate READ IO.
+ * Third case if file is too big to fit even in maximum size, in that
+ * case we return just tail to optimize possible append.
+ *
+ * At the moment the following strategy is used:
+ * 1) try to fit into the buffer we have
+ * 2) return just file tail otherwise.
+ */
+ if (real_dom_size <= len) {
+ /* can fit whole data */
+ len = real_dom_size;
+ offset = 0;
+ } else if (real_dom_size <
+ mdt_lmm_dom_stripesize(mti->mti_attr.ma_lmm)) {
+ int tail, pgbits;
+
+ /* File tail offset must be aligned with larger page size
+ * between client and server, so the maximum page size is
+ * used here to align offset.
+ *
+ * NB: DOM feature was introduced when server supports pagebits
+ * already, so it should be always non-zero value. Report error
+ * if it is not for some reason.
+ */
+ if (!req->rq_export->exp_target_data.ted_pagebits) {
+ CERROR("%s: client page bits are not saved on server\n",
+ mdt_obd_name(mdt));
+ RETURN(0);
+ }
+ pgbits = max_t(int, PAGE_SHIFT,
+ req->rq_export->exp_target_data.ted_pagebits);
+ tail = real_dom_size % (1 << pgbits);
+
+ /* no partial tail or tail can't fit in reply */
+ if (tail == 0 || len < tail)
+ RETURN(0);
+
+ len = tail;
+ offset = real_dom_size - len;
+ } else {
+ /* DOM stripe is fully written, so don't expect its tail
+ * will be used by append.
+ */
+ RETURN(0);
+ }
+
+ LASSERT((offset & ~PAGE_MASK) == 0);
+ rc = req_capsule_server_grow(pill, &RMF_NIOBUF_INLINE,
+ sizeof(*rnb) + len);
+ if (rc != 0) {
+ /* failed to grow data buffer, just exit */
+ GOTO(out, rc = -E2BIG);
+ }
+
+ /* re-take MDT_BODY and NIOBUF_INLINE buffers after the buffer grow */
+ mbo = req_capsule_server_get(pill, &RMF_MDT_BODY);
+ fid = &mbo->mbo_fid1;
+ if (!fid_is_sane(fid))
+ GOTO(out, rc = -EINVAL);
+
+ rnb = req_capsule_server_get(tsi->tsi_pill, &RMF_NIOBUF_INLINE);
+ if (rnb == NULL)
+ GOTO(out, rc = -EPROTO);
+
+ buf = (char *)rnb + sizeof(*rnb);
+ rnb->rnb_len = len;
+ rnb->rnb_offset = offset;
+
+ mo = dt_locate(env, dt, fid);
+ if (IS_ERR(mo))
+ GOTO(out_rnb, rc = PTR_ERR(mo));
+ LASSERT(mo != NULL);
+
+ dt_read_lock(env, mo, 0);
+ if (!dt_object_exists(mo))
+ GOTO(unlock, rc = -ENOENT);
+
+ /* parse remote buffers to local buffers and prepare the latter */
+ lnbs = (len >> PAGE_SHIFT) + 1;
+ OBD_ALLOC_PTR_ARRAY(lnb, lnbs);
+ if (lnb == NULL)
+ GOTO(unlock, rc = -ENOMEM);
+
+ rc = dt_bufs_get(env, mo, rnb, lnb, lnbs, 0);
+ if (unlikely(rc < 0))
+ GOTO(free, rc);
+ LASSERT(rc <= lnbs);
+ nr_local = rc;
+ rc = dt_read_prep(env, mo, lnb, nr_local);
+ if (unlikely(rc))
+ GOTO(buf_put, rc);
+ /* copy data to the buffer finally */
+ for (i = 0; i < nr_local; i++) {
+ char *p = kmap(lnb[i].lnb_page);
+ long off;
+
+ LASSERT(lnb[i].lnb_page_offset == 0);
+ off = lnb[i].lnb_len & ~PAGE_MASK;
+ if (off > 0)
+ memset(p + off, 0, PAGE_SIZE - off);
+
+ memcpy(buf + (i << PAGE_SHIFT), p, lnb[i].lnb_len);
+ kunmap(lnb[i].lnb_page);
+ copied += lnb[i].lnb_len;
+ LASSERT(rc <= len);
+ }
+ CDEBUG(D_INFO, "Read %i (wanted %u) bytes from %llu\n", copied,
+ len, offset);
+ if (copied < len) {
+ CWARN("%s: read %i bytes for "DFID
+ " but wanted %u, is size wrong?\n",
+ tsi->tsi_exp->exp_obd->obd_name, copied,
+ PFID(&tsi->tsi_fid), len);
+ /* Ignore partially copied data */
+ copied = 0;
+ }
+ EXIT;
+buf_put:
+ dt_bufs_put(env, mo, lnb, nr_local);
+free:
+ OBD_FREE_PTR_ARRAY(lnb, lnbs);
+unlock:
+ dt_read_unlock(env, mo);
+ lu_object_put(env, &mo->do_lu);
+out_rnb:
+ rnb->rnb_len = copied;
+out:
+ /* Don't fail OPEN request if read-on-open is failed, but drop
+ * a message in log about the error.
+ */
+ if (rc)
+ CDEBUG(D_INFO, "Read-on-open is failed, rc = %d", rc);
+
+ RETURN(0);
+}
+
+/**
+ * Completion AST for DOM discard locks:
+ *
+ * CP AST an DOM discard lock is called always right after enqueue or from
+ * reprocess if lock was blocked, in the latest case l_ast_data is set to
+ * the mdt_object which is kept while there are pending locks on it.
+ */
+int ldlm_dom_discard_cp_ast(struct ldlm_lock *lock, __u64 flags, void *data)
+{
+ struct mdt_object *mo;
+ struct lustre_handle dom_lh;
+ struct lu_env *env;
+
+ ENTRY;
+
+ /* l_ast_data is set when lock was not granted immediately
+ * in mdt_dom_discard_data() below but put into waiting list,
+ * so this CP callback means we are finished and corresponding
+ * MDT object should be released finally as well as lock itself.
+ */
+ lock_res_and_lock(lock);
+ if (!lock->l_ast_data) {
+ unlock_res_and_lock(lock);
+ RETURN(0);
+ }
+
+ mo = lock->l_ast_data;
+ lock->l_ast_data = NULL;
+ unlock_res_and_lock(lock);
+
+ ldlm_lock2handle(lock, &dom_lh);
+ ldlm_lock_decref(&dom_lh, LCK_PW);
+
+ env = lu_env_find();
+ LASSERT(env);
+ mdt_object_put(env, mo);
+
+ RETURN(0);
+}
+
+void mdt_dom_discard_data(struct mdt_thread_info *info,
+ struct mdt_object *mo)
+{
+ struct ptlrpc_request *req = mdt_info_req(info);
+ struct mdt_device *mdt = mdt_dev(mo->mot_obj.lo_dev);
+ union ldlm_policy_data policy;
+ struct ldlm_res_id res_id;
+ struct lustre_handle dom_lh;
+ struct ldlm_lock *lock;
+ __u64 flags = LDLM_FL_AST_DISCARD_DATA;
+ int rc = 0;
+ bool old_client;
+
+ ENTRY;
+
+ if (req && req_is_replay(req))
+ RETURN_EXIT;
+
+ policy.l_inodebits.bits = MDS_INODELOCK_DOM;
+ policy.l_inodebits.try_bits = 0;
+ fid_build_reg_res_name(mdt_object_fid(mo), &res_id);
+
+ /* Keep blocking version of discard for an old client to avoid
+ * crashes on non-patched clients. LU-11359.
+ */
+ old_client = req && !(exp_connect_flags2(req->rq_export) &
+ OBD_CONNECT2_ASYNC_DISCARD);
+
+ /* Tell the clients that the object is gone now and that they should
+ * throw away any cached pages. */
+ rc = ldlm_cli_enqueue_local(info->mti_env, mdt->mdt_namespace, &res_id,
+ LDLM_IBITS, &policy, LCK_PW, &flags,
+ ldlm_blocking_ast, old_client ?
+ ldlm_completion_ast :
+ ldlm_dom_discard_cp_ast,
+ NULL, NULL, 0, LVB_T_NONE, NULL, &dom_lh);
+ if (rc != ELDLM_OK) {
+ CDEBUG(D_DLMTRACE,
+ "Failed to issue discard lock, rc = %d\n", rc);
+ RETURN_EXIT;
+ }
+
+ lock = ldlm_handle2lock(&dom_lh);
+ lock_res_and_lock(lock);
+ /* if lock is not granted then there are BL ASTs in progress and
+ * lock will be granted in result of reprocessing with CP callback
+ * notifying about that. The mdt object has to be kept until that and
+ * it is saved in l_ast_data of the lock. Lock reference is kept too
+ * until that to prevent it from canceling.
+ */
+ if (!is_granted_or_cancelled_nolock(lock)) {
+ mdt_object_get(info->mti_env, mo);
+ lock->l_ast_data = mo;
+ unlock_res_and_lock(lock);
+ } else {
+ unlock_res_and_lock(lock);
+ ldlm_lock_decref_and_cancel(&dom_lh, LCK_PW);
+ }
+ LDLM_LOCK_PUT(lock);
+
+ RETURN_EXIT;
+}