4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2017, Intel Corporation.
28 * Author: Mikhail Pershin <mike.pershin@intel.com>
31 #define DEBUG_SUBSYSTEM S_FILTER
33 #include <dt_object.h>
34 #include "mdt_internal.h"
36 /* functions below are stubs for now, they will be implemented with
37 * grant support on MDT */
38 static inline void mdt_io_counter_incr(struct obd_export *exp, int opcode,
39 char *jobid, long amount)
44 static inline void mdt_dom_read_lock(struct mdt_object *mo)
46 down_read(&mo->mot_dom_sem);
49 static inline void mdt_dom_read_unlock(struct mdt_object *mo)
51 up_read(&mo->mot_dom_sem);
54 static inline void mdt_dom_write_lock(struct mdt_object *mo)
56 down_write(&mo->mot_dom_sem);
59 static inline void mdt_dom_write_unlock(struct mdt_object *mo)
61 up_write(&mo->mot_dom_sem);
65 * Lock prolongation for Data-on-MDT.
66 * This is similar to OFD code but for DOM ibits lock.
68 static inline time64_t prolong_timeout(struct ptlrpc_request *req)
70 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
74 return obd_timeout / 2;
76 req_timeout = req->rq_deadline - req->rq_arrival_time.tv_sec;
77 return max_t(time64_t, at_est2timeout(at_get(&svcpt->scp_at_estimate)),
81 static void mdt_prolong_dom_lock(struct tgt_session_info *tsi,
82 struct ldlm_prolong_args *data)
84 struct obdo *oa = &tsi->tsi_ost_body->oa;
85 struct ldlm_lock *lock;
89 data->lpa_timeout = prolong_timeout(tgt_ses_req(tsi));
90 data->lpa_export = tsi->tsi_exp;
91 data->lpa_resid = tsi->tsi_resid;
93 CDEBUG(D_RPCTRACE, "Prolong DOM lock for req %p with x%llu\n",
94 tgt_ses_req(tsi), tgt_ses_req(tsi)->rq_xid);
96 if (oa->o_valid & OBD_MD_FLHANDLE) {
97 /* mostly a request should be covered by only one lock, try
99 lock = ldlm_handle2lock(&oa->o_handle);
101 LASSERT(lock->l_export == data->lpa_export);
102 ldlm_lock_prolong_one(lock, data);
103 lock->l_last_used = ktime_get();
111 static int mdt_rw_hpreq_lock_match(struct ptlrpc_request *req,
112 struct ldlm_lock *lock)
114 struct obd_ioobj *ioo;
116 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
120 if (!(lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_DOM))
123 ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
124 LASSERT(ioo != NULL);
126 LASSERT(lock->l_resource != NULL);
127 if (!fid_res_name_eq(&ioo->ioo_oid.oi_fid, &lock->l_resource->lr_name))
130 /* a bulk write can only hold a reference on a PW extent lock. */
133 /* whereas a bulk read can be protected by either a PR or PW
137 if (!(lock->l_granted_mode & mode))
143 static int mdt_rw_hpreq_check(struct ptlrpc_request *req)
145 struct tgt_session_info *tsi;
146 struct obd_ioobj *ioo;
147 struct niobuf_remote *rnb;
149 struct ldlm_prolong_args pa = { 0 };
153 /* Don't use tgt_ses_info() to get session info, because lock_match()
154 * can be called while request has no processing thread yet. */
155 tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
158 * Use LASSERT below because malformed RPCs should have
159 * been filtered out in tgt_hpreq_handler().
161 opc = lustre_msg_get_opc(req->rq_reqmsg);
162 LASSERT(opc == OST_READ || opc == OST_WRITE);
164 ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
165 LASSERT(ioo != NULL);
167 rnb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
168 LASSERT(rnb != NULL);
169 LASSERT(!(rnb->rnb_flags & OBD_BRW_SRVLOCK));
171 pa.lpa_mode = LCK_PW;
173 pa.lpa_mode |= LCK_PR;
175 DEBUG_REQ(D_RPCTRACE, req, "%s %s: refresh rw locks: "DFID"\n",
176 tgt_name(tsi->tsi_tgt), current->comm, PFID(&tsi->tsi_fid));
178 mdt_prolong_dom_lock(tsi, &pa);
180 if (pa.lpa_blocks_cnt > 0) {
182 "%s: refreshed %u locks timeout for req %p.\n",
183 tgt_name(tsi->tsi_tgt), pa.lpa_blocks_cnt, req);
187 RETURN(pa.lpa_locks_cnt > 0 ? 0 : -ESTALE);
190 static void mdt_rw_hpreq_fini(struct ptlrpc_request *req)
192 mdt_rw_hpreq_check(req);
195 static struct ptlrpc_hpreq_ops mdt_hpreq_rw = {
196 .hpreq_lock_match = mdt_rw_hpreq_lock_match,
197 .hpreq_check = mdt_rw_hpreq_check,
198 .hpreq_fini = mdt_rw_hpreq_fini
202 * Assign high priority operations to an IO request.
204 * Check if the incoming request is a candidate for
205 * high-priority processing. If it is, assign it a high
206 * priority operations table.
208 * \param[in] tsi target session environment for this request
210 void mdt_hp_brw(struct tgt_session_info *tsi)
212 struct niobuf_remote *rnb;
213 struct obd_ioobj *ioo;
217 ioo = req_capsule_client_get(tsi->tsi_pill, &RMF_OBD_IOOBJ);
218 LASSERT(ioo != NULL); /* must exist after request preprocessing */
219 if (ioo->ioo_bufcnt > 0) {
220 rnb = req_capsule_client_get(tsi->tsi_pill, &RMF_NIOBUF_REMOTE);
221 LASSERT(rnb != NULL); /* must exist after preprocessing */
223 /* no high priority if server lock is needed */
224 if (rnb->rnb_flags & OBD_BRW_SRVLOCK ||
225 (lustre_msg_get_flags(tgt_ses_req(tsi)->rq_reqmsg) &
229 tgt_ses_req(tsi)->rq_ops = &mdt_hpreq_rw;
232 static int mdt_punch_hpreq_lock_match(struct ptlrpc_request *req,
233 struct ldlm_lock *lock)
235 struct tgt_session_info *tsi;
240 /* Don't use tgt_ses_info() to get session info, because lock_match()
241 * can be called while request has no processing thread yet. */
242 tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
245 * Use LASSERT below because malformed RPCs should have
246 * been filtered out in tgt_hpreq_handler().
248 LASSERT(tsi->tsi_ost_body != NULL);
249 if (tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLHANDLE &&
250 tsi->tsi_ost_body->oa.o_handle.cookie == lock->l_handle.h_cookie)
253 oa = &tsi->tsi_ost_body->oa;
255 LASSERT(lock->l_resource != NULL);
256 if (!fid_res_name_eq(&oa->o_oi.oi_fid, &lock->l_resource->lr_name))
259 if (!(lock->l_granted_mode & LCK_PW))
266 * Implementation of ptlrpc_hpreq_ops::hpreq_lock_check for OST_PUNCH request.
268 * High-priority queue request check for whether the given punch request
269 * (\a req) is blocking an LDLM lock cancel. Also checks whether the request is
270 * covered by an LDLM lock.
274 * \param[in] req the incoming request
276 * \retval 1 if \a req is blocking an LDLM lock cancel
277 * \retval 0 if it is not
278 * \retval -ESTALE if lock is not found
280 static int mdt_punch_hpreq_check(struct ptlrpc_request *req)
282 struct tgt_session_info *tsi;
284 struct ldlm_prolong_args pa = { 0 };
288 /* Don't use tgt_ses_info() to get session info, because lock_match()
289 * can be called while request has no processing thread yet. */
290 tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
291 LASSERT(tsi != NULL);
292 oa = &tsi->tsi_ost_body->oa;
294 LASSERT(!(oa->o_valid & OBD_MD_FLFLAGS &&
295 oa->o_flags & OBD_FL_SRVLOCK));
297 pa.lpa_mode = LCK_PW;
299 CDEBUG(D_DLMTRACE, "%s: refresh DOM lock for "DFID"\n",
300 tgt_name(tsi->tsi_tgt), PFID(&tsi->tsi_fid));
302 mdt_prolong_dom_lock(tsi, &pa);
305 if (pa.lpa_blocks_cnt > 0) {
307 "%s: refreshed %u locks timeout for req %p.\n",
308 tgt_name(tsi->tsi_tgt), pa.lpa_blocks_cnt, req);
312 RETURN(pa.lpa_locks_cnt > 0 ? 0 : -ESTALE);
316 * Implementation of ptlrpc_hpreq_ops::hpreq_lock_fini for OST_PUNCH request.
318 * Called after the request has been handled. It refreshes lock timeout again
319 * so that client has more time to send lock cancel RPC.
321 * \param[in] req request which is being processed.
323 static void mdt_punch_hpreq_fini(struct ptlrpc_request *req)
325 mdt_punch_hpreq_check(req);
328 static struct ptlrpc_hpreq_ops mdt_hpreq_punch = {
329 .hpreq_lock_match = mdt_punch_hpreq_lock_match,
330 .hpreq_check = mdt_punch_hpreq_check,
331 .hpreq_fini = mdt_punch_hpreq_fini
334 void mdt_hp_punch(struct tgt_session_info *tsi)
336 LASSERT(tsi->tsi_ost_body != NULL); /* must exists if we are here */
337 /* no high-priority if server lock is needed */
338 if ((tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLFLAGS &&
339 tsi->tsi_ost_body->oa.o_flags & OBD_FL_SRVLOCK) ||
340 tgt_conn_flags(tsi) & OBD_CONNECT_MDS ||
341 lustre_msg_get_flags(tgt_ses_req(tsi)->rq_reqmsg) & MSG_REPLAY)
343 tgt_ses_req(tsi)->rq_ops = &mdt_hpreq_punch;
346 static int mdt_preprw_read(const struct lu_env *env, struct obd_export *exp,
347 struct mdt_device *mdt, struct mdt_object *mo,
348 struct lu_attr *la, int niocount,
349 struct niobuf_remote *rnb, int *nr_local,
350 struct niobuf_local *lnb, char *jobid)
352 struct dt_object *dob;
353 int i, j, rc, tot_bytes = 0;
357 mdt_dom_read_lock(mo);
358 if (!mdt_object_exists(mo))
359 GOTO(unlock, rc = -ENOENT);
361 dob = mdt_obj2dt(mo);
362 /* parse remote buffers to local buffers and prepare the latter */
364 for (i = 0, j = 0; i < niocount; i++) {
365 rc = dt_bufs_get(env, dob, rnb + i, lnb + j, 0);
366 if (unlikely(rc < 0))
368 /* correct index for local buffers to continue with */
371 tot_bytes += rnb[i].rnb_len;
374 rc = dt_attr_get(env, dob, la);
378 rc = dt_read_prep(env, dob, lnb, *nr_local);
382 mdt_io_counter_incr(exp, LPROC_MDT_IO_READ, jobid, tot_bytes);
385 dt_bufs_put(env, dob, lnb, *nr_local);
387 mdt_dom_read_unlock(mo);
391 static int mdt_preprw_write(const struct lu_env *env, struct obd_export *exp,
392 struct mdt_device *mdt, struct mdt_object *mo,
393 struct lu_attr *la, struct obdo *oa,
394 int objcount, struct obd_ioobj *obj,
395 struct niobuf_remote *rnb, int *nr_local,
396 struct niobuf_local *lnb, char *jobid)
398 struct dt_object *dob;
399 int i, j, k, rc = 0, tot_bytes = 0;
403 /* Process incoming grant info, set OBD_BRW_GRANTED flag and grant some
404 * space back if possible */
405 tgt_grant_prepare_write(env, exp, oa, rnb, obj->ioo_bufcnt);
407 mdt_dom_read_lock(mo);
408 if (!mdt_object_exists(mo)) {
409 CDEBUG(D_ERROR, "%s: BRW to missing obj "DFID"\n",
410 exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)));
411 GOTO(unlock, rc = -ENOENT);
414 dob = mdt_obj2dt(mo);
415 /* parse remote buffers to local buffers and prepare the latter */
417 for (i = 0, j = 0; i < obj->ioo_bufcnt; i++) {
418 rc = dt_bufs_get(env, dob, rnb + i, lnb + j, 1);
419 if (unlikely(rc < 0))
421 /* correct index for local buffers to continue with */
422 for (k = 0; k < rc; k++) {
423 lnb[j + k].lnb_flags = rnb[i].rnb_flags;
424 if (!(rnb[i].rnb_flags & OBD_BRW_GRANTED))
425 lnb[j + k].lnb_rc = -ENOSPC;
429 tot_bytes += rnb[i].rnb_len;
432 rc = dt_write_prep(env, dob, lnb, *nr_local);
436 mdt_io_counter_incr(exp, LPROC_MDT_IO_WRITE, jobid, tot_bytes);
439 dt_bufs_put(env, dob, lnb, *nr_local);
441 mdt_dom_read_unlock(mo);
442 /* tgt_grant_prepare_write() was called, so we must commit */
443 tgt_grant_commit(exp, oa->o_grant_used, rc);
444 /* let's still process incoming grant information packed in the oa,
445 * but without enforcing grant since we won't proceed with the write.
446 * Just like a read request actually. */
447 tgt_grant_prepare_read(env, exp, oa);
451 int mdt_obd_preprw(const struct lu_env *env, int cmd, struct obd_export *exp,
452 struct obdo *oa, int objcount, struct obd_ioobj *obj,
453 struct niobuf_remote *rnb, int *nr_local,
454 struct niobuf_local *lnb)
456 struct tgt_session_info *tsi = tgt_ses_info(env);
457 struct mdt_thread_info *info = tsi2mdt_info(tsi);
458 struct lu_attr *la = &info->mti_attr.ma_attr;
459 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
460 struct mdt_object *mo;
464 /* The default value PTLRPC_MAX_BRW_PAGES is set in tgt_brw_write()
465 * but for MDT it is different, correct it here. */
466 if (*nr_local > MD_MAX_BRW_PAGES)
467 *nr_local = MD_MAX_BRW_PAGES;
469 jobid = tsi->tsi_jobid;
471 if (!oa || objcount != 1 || obj->ioo_bufcnt == 0) {
472 CERROR("%s: bad parameters %p/%i/%i\n",
473 exp->exp_obd->obd_name, oa, objcount, obj->ioo_bufcnt);
477 mo = mdt_object_find(env, mdt, &tsi->tsi_fid);
479 GOTO(out, rc = PTR_ERR(mo));
481 LASSERT(info->mti_object == NULL);
482 info->mti_object = mo;
484 if (cmd == OBD_BRW_WRITE) {
485 la_from_obdo(la, oa, OBD_MD_FLGETATTR);
486 rc = mdt_preprw_write(env, exp, mdt, mo, la, oa,
487 objcount, obj, rnb, nr_local, lnb,
489 } else if (cmd == OBD_BRW_READ) {
490 tgt_grant_prepare_read(env, exp, oa);
491 rc = mdt_preprw_read(env, exp, mdt, mo, la,
492 obj->ioo_bufcnt, rnb, nr_local, lnb,
494 obdo_from_la(oa, la, LA_ATIME);
496 CERROR("%s: wrong cmd %d received!\n",
497 exp->exp_obd->obd_name, cmd);
501 lu_object_put(env, &mo->mot_obj);
502 info->mti_object = NULL;
508 static int mdt_commitrw_read(const struct lu_env *env, struct mdt_device *mdt,
509 struct mdt_object *mo, int objcount, int niocount,
510 struct niobuf_local *lnb)
512 struct dt_object *dob;
517 LASSERT(niocount > 0);
519 dob = mdt_obj2dt(mo);
521 dt_bufs_put(env, dob, lnb, niocount);
523 mdt_dom_read_unlock(mo);
527 static int mdt_commitrw_write(const struct lu_env *env, struct obd_export *exp,
528 struct mdt_device *mdt, struct mdt_object *mo,
529 struct lu_attr *la, int objcount, int niocount,
530 struct niobuf_local *lnb, unsigned long granted,
533 struct dt_device *dt = mdt->mdt_bottom;
534 struct dt_object *dob;
542 dob = mdt_obj2dt(mo);
545 GOTO(out, rc = old_rc);
547 la->la_valid &= LA_ATIME | LA_MTIME | LA_CTIME;
549 if (!dt_object_exists(dob))
550 GOTO(out, rc = -ENOENT);
552 th = dt_trans_create(env, dt);
554 GOTO(out, rc = PTR_ERR(th));
556 for (i = 0; i < niocount; i++) {
557 if (!(lnb[i].lnb_flags & OBD_BRW_ASYNC)) {
563 if (OBD_FAIL_CHECK(OBD_FAIL_OST_DQACQ_NET))
564 GOTO(out_stop, rc = -EINPROGRESS);
566 rc = dt_declare_write_commit(env, dob, lnb, niocount, th);
571 /* update [mac]time if needed */
572 rc = dt_declare_attr_set(env, dob, la, th);
577 tgt_vbr_obj_set(env, dob);
578 rc = dt_trans_start(env, dt, th);
582 dt_write_lock(env, dob, 0);
583 rc = dt_write_commit(env, dob, lnb, niocount, th);
588 rc = dt_attr_set(env, dob, la, th);
592 /* get attr to return */
593 rc = dt_attr_get(env, dob, la);
595 dt_write_unlock(env, dob);
598 /* Force commit to make the just-deleted blocks
599 * reusable. LU-456 */
604 if (rc == 0 && granted > 0) {
605 if (tgt_grant_commit_cb_add(th, exp, granted) == 0)
610 dt_trans_stop(env, dt, th);
611 if (rc == -ENOSPC && retries++ < 3) {
612 CDEBUG(D_INODE, "retry after force commit, retries:%d\n",
618 dt_bufs_put(env, dob, lnb, niocount);
619 mdt_dom_read_unlock(mo);
621 tgt_grant_commit(exp, granted, old_rc);
625 void mdt_dom_obj_lvb_update(const struct lu_env *env, struct mdt_object *mo,
628 struct mdt_device *mdt = mdt_dev(mo->mot_obj.lo_dev);
629 struct ldlm_res_id resid;
630 struct ldlm_resource *res;
632 fid_build_reg_res_name(mdt_object_fid(mo), &resid);
633 res = ldlm_resource_get(mdt->mdt_namespace, NULL, &resid,
638 /* Update lvbo data if exists. */
639 if (mdt_dom_lvb_is_valid(res))
640 mdt_dom_disk_lvbo_update(env, mo, res, increase_only);
641 ldlm_resource_putref(res);
644 int mdt_obd_commitrw(const struct lu_env *env, int cmd, struct obd_export *exp,
645 struct obdo *oa, int objcount, struct obd_ioobj *obj,
646 struct niobuf_remote *rnb, int npages,
647 struct niobuf_local *lnb, int old_rc)
649 struct mdt_thread_info *info = mdt_th_info(env);
650 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
651 struct mdt_object *mo = info->mti_object;
652 struct lu_attr *la = &info->mti_attr.ma_attr;
657 CERROR("%s: no pages to commit\n",
658 exp->exp_obd->obd_name);
664 if (cmd == OBD_BRW_WRITE) {
665 /* Don't update timestamps if this write is older than a
666 * setattr which modifies the timestamps. b=10150 */
668 /* XXX when we start having persistent reservations this needs
669 * to be changed to ofd_fmd_get() to create the fmd if it
670 * doesn't already exist so we can store the reservation handle
672 valid = OBD_MD_FLUID | OBD_MD_FLGID;
673 valid |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME;
675 la_from_obdo(la, oa, valid);
677 rc = mdt_commitrw_write(env, exp, mdt, mo, la, objcount,
678 npages, lnb, oa->o_grant_used, old_rc);
680 obdo_from_la(oa, la, VALID_FLAGS | LA_GID | LA_UID);
682 obdo_from_la(oa, la, LA_GID | LA_UID);
684 mdt_dom_obj_lvb_update(env, mo, false);
685 /* don't report overquota flag if we failed before reaching
687 if (old_rc == 0 && (rc == 0 || rc == -EDQUOT)) {
688 /* return the overquota flags to client */
689 if (lnb[0].lnb_flags & OBD_BRW_OVER_USRQUOTA) {
690 if (oa->o_valid & OBD_MD_FLFLAGS)
691 oa->o_flags |= OBD_FL_NO_USRQUOTA;
693 oa->o_flags = OBD_FL_NO_USRQUOTA;
696 if (lnb[0].lnb_flags & OBD_BRW_OVER_GRPQUOTA) {
697 if (oa->o_valid & OBD_MD_FLFLAGS)
698 oa->o_flags |= OBD_FL_NO_GRPQUOTA;
700 oa->o_flags = OBD_FL_NO_GRPQUOTA;
703 oa->o_valid |= OBD_MD_FLFLAGS | OBD_MD_FLUSRQUOTA |
706 } else if (cmd == OBD_BRW_READ) {
707 /* If oa != NULL then mdt_preprw_read updated the inode
708 * atime and we should update the lvb so that other glimpses
709 * will also get the updated value. bug 5972 */
711 mdt_dom_obj_lvb_update(env, mo, true);
712 rc = mdt_commitrw_read(env, mdt, mo, objcount, npages, lnb);
718 /* this put is pair to object_get in ofd_preprw_write */
719 mdt_thread_info_fini(info);
723 int mdt_object_punch(const struct lu_env *env, struct dt_device *dt,
724 struct dt_object *dob, __u64 start, __u64 end,
732 /* we support truncate, not punch yet */
733 LASSERT(end == OBD_OBJECT_EOF);
735 if (!dt_object_exists(dob))
738 th = dt_trans_create(env, dt);
742 rc = dt_declare_attr_set(env, dob, la, th);
746 rc = dt_declare_punch(env, dob, start, OBD_OBJECT_EOF, th);
750 tgt_vbr_obj_set(env, dob);
751 rc = dt_trans_start(env, dt, th);
755 dt_write_lock(env, dob, 0);
756 rc = dt_punch(env, dob, start, OBD_OBJECT_EOF, th);
759 rc = dt_attr_set(env, dob, la, th);
763 dt_write_unlock(env, dob);
766 dt_trans_stop(env, dt, th);
770 int mdt_punch_hdl(struct tgt_session_info *tsi)
772 const struct obdo *oa = &tsi->tsi_ost_body->oa;
773 struct ost_body *repbody;
774 struct mdt_thread_info *info;
776 struct ldlm_namespace *ns = tsi->tsi_tgt->lut_obd->obd_namespace;
777 struct obd_export *exp = tsi->tsi_exp;
778 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
779 struct mdt_object *mo;
780 struct dt_object *dob;
782 struct lustre_handle lh = { 0, };
789 /* check that we do support OBD_CONNECT_TRUNCLOCK. */
790 CLASSERT(OST_CONNECT_SUPPORTED & OBD_CONNECT_TRUNCLOCK);
792 if ((oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
793 (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
794 RETURN(err_serious(-EPROTO));
796 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
798 RETURN(err_serious(-ENOMEM));
800 /* punch start,end are passed in o_size,o_blocks throught wire */
804 if (end != OBD_OBJECT_EOF) /* Only truncate is supported */
807 info = tsi2mdt_info(tsi);
808 la = &info->mti_attr.ma_attr;
809 /* standard truncate optimization: if file body is completely
810 * destroyed, don't send data back to the server. */
812 flags |= LDLM_FL_AST_DISCARD_DATA;
814 repbody->oa.o_oi = oa->o_oi;
815 repbody->oa.o_valid = OBD_MD_FLID;
817 srvlock = (exp_connect_flags(exp) & OBD_CONNECT_SRVLOCK) &&
818 oa->o_valid & OBD_MD_FLFLAGS &&
819 oa->o_flags & OBD_FL_SRVLOCK;
822 rc = tgt_mdt_data_lock(ns, &tsi->tsi_resid, &lh, LCK_PW,
828 CDEBUG(D_INODE, "calling punch for object "DFID", valid = %#llx"
829 ", start = %lld, end = %lld\n", PFID(&tsi->tsi_fid),
830 oa->o_valid, start, end);
832 mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
834 GOTO(out_unlock, rc = PTR_ERR(mo));
836 if (!mdt_object_exists(mo))
837 GOTO(out_put, rc = -ENOENT);
839 /* Shouldn't happen on dirs */
840 if (S_ISDIR(lu_object_attr(&mo->mot_obj))) {
842 CERROR("%s: Truncate on dir "DFID": rc = %d\n",
843 exp->exp_obd->obd_name, PFID(&tsi->tsi_fid), rc);
847 mdt_dom_write_lock(mo);
848 dob = mdt_obj2dt(mo);
850 la_from_obdo(la, oa, OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
852 la->la_valid |= LA_SIZE;
854 rc = mdt_object_punch(tsi->tsi_env, mdt->mdt_bottom, dob,
856 mdt_dom_write_unlock(mo);
860 mdt_dom_obj_lvb_update(tsi->tsi_env, mo, false);
861 mdt_io_counter_incr(tsi->tsi_exp, LPROC_MDT_IO_PUNCH,
865 lu_object_put(tsi->tsi_env, &mo->mot_obj);
868 tgt_extent_unlock(&lh, LCK_PW);
870 mdt_thread_info_fini(info);
875 * MDT glimpse for Data-on-MDT
877 * If there is write lock on client then function issues glimpse_ast to get
878 * an actual size from that client.
881 int mdt_do_glimpse(const struct lu_env *env, struct ldlm_namespace *ns,
882 struct ldlm_resource *res)
884 union ldlm_policy_data policy;
885 struct lustre_handle lockh;
887 struct ldlm_lock *lock;
888 struct ldlm_glimpse_work *gl_work;
889 struct list_head gl_list;
894 /* There can be only one write lock covering data, try to match it. */
895 policy.l_inodebits.bits = MDS_INODELOCK_DOM;
896 mode = ldlm_lock_match(ns, LDLM_FL_TEST_LOCK,
897 &res->lr_name, LDLM_IBITS, &policy,
900 /* There is no PW lock on this object; finished. */
904 lock = ldlm_handle2lock(&lockh);
909 * This check is for lock taken in mdt_reint_unlink() that does
910 * not have l_glimpse_ast set. So the logic is: if there is a lock
911 * with no l_glimpse_ast set, this object is being destroyed already.
912 * Hence, if you are grabbing DLM locks on the server, always set
913 * non-NULL glimpse_ast (e.g., ldlm_request.c::ldlm_glimpse_ast()).
915 if (lock->l_glimpse_ast == NULL) {
916 LDLM_DEBUG(lock, "no l_glimpse_ast");
917 GOTO(out, rc = -ENOENT);
920 OBD_SLAB_ALLOC_PTR_GFP(gl_work, ldlm_glimpse_work_kmem, GFP_ATOMIC);
922 GOTO(out, rc = -ENOMEM);
924 /* Populate the gl_work structure.
925 * Grab additional reference on the lock which will be released in
926 * ldlm_work_gl_ast_lock() */
927 gl_work->gl_lock = LDLM_LOCK_GET(lock);
928 /* The glimpse callback is sent to one single IO lock. As a result,
929 * the gl_work list is just composed of one element */
930 INIT_LIST_HEAD(&gl_list);
931 list_add_tail(&gl_work->gl_list, &gl_list);
932 /* There is actually no need for a glimpse descriptor when glimpsing
934 gl_work->gl_desc = NULL;
935 /* the ldlm_glimpse_work structure is allocated on the stack */
936 gl_work->gl_flags = LDLM_GL_WORK_SLAB_ALLOCATED;
938 ldlm_glimpse_locks(res, &gl_list); /* this will update the LVB */
940 /* If the list is not empty, we failed to glimpse a lock and
941 * must clean it up. Usually due to a race with unlink.*/
942 if (!list_empty(&gl_list)) {
943 LDLM_LOCK_RELEASE(lock);
944 OBD_SLAB_FREE_PTR(gl_work, ldlm_glimpse_work_kmem);
953 static void mdt_lvb2body(struct ldlm_resource *res, struct mdt_body *mb)
955 struct ost_lvb *res_lvb;
958 res_lvb = res->lr_lvb_data;
959 mb->mbo_dom_size = res_lvb->lvb_size;
960 mb->mbo_dom_blocks = res_lvb->lvb_blocks;
961 mb->mbo_mtime = res_lvb->lvb_mtime;
962 mb->mbo_ctime = res_lvb->lvb_ctime;
963 mb->mbo_atime = res_lvb->lvb_atime;
965 CDEBUG(D_DLMTRACE, "size %llu\n", res_lvb->lvb_size);
967 mb->mbo_valid |= OBD_MD_FLATIME | OBD_MD_FLCTIME | OBD_MD_FLMTIME |
973 * MDT glimpse for Data-on-MDT
975 * This function is called when MDT get attributes for the DoM object.
976 * If there is write lock on client then function issues glimpse_ast to get
977 * an actual size from that client.
979 int mdt_dom_object_size(const struct lu_env *env, struct mdt_device *mdt,
980 const struct lu_fid *fid, struct mdt_body *mb,
983 struct ldlm_res_id resid;
984 struct ldlm_resource *res;
989 fid_build_reg_res_name(fid, &resid);
990 res = ldlm_resource_get(mdt->mdt_namespace, NULL, &resid,
995 /* Update lvbo data if DoM lock returned or if LVB is not yet valid. */
996 if (dom_lock || !mdt_dom_lvb_is_valid(res))
997 mdt_dom_lvbo_update(res, NULL, NULL, false);
999 mdt_lvb2body(res, mb);
1000 ldlm_resource_putref(res);
1005 * MDT DoM lock intent policy (glimpse)
1007 * Intent policy is called when lock has an intent, for DoM file that
1008 * means glimpse lock and policy fills Lock Value Block (LVB).
1010 * If already granted lock is found it will be placed in \a lockp and
1011 * returned back to caller function.
1013 * \param[in] tsi session info
1014 * \param[in,out] lockp pointer to the lock
1015 * \param[in] flags LDLM flags
1017 * \retval ELDLM_LOCK_REPLACED if already granted lock was found
1018 * and placed in \a lockp
1019 * \retval ELDLM_LOCK_ABORTED in other cases except error
1020 * \retval negative value on error
1022 int mdt_glimpse_enqueue(struct mdt_thread_info *mti, struct ldlm_namespace *ns,
1023 struct ldlm_lock **lockp, __u64 flags)
1025 struct ldlm_lock *lock = *lockp;
1026 struct ldlm_resource *res = lock->l_resource;
1027 ldlm_processing_policy policy;
1028 struct ldlm_reply *rep;
1029 struct mdt_body *mbo;
1034 policy = ldlm_get_processing_policy(res);
1035 LASSERT(policy != NULL);
1037 req_capsule_set_size(mti->mti_pill, &RMF_MDT_MD, RCL_SERVER, 0);
1038 req_capsule_set_size(mti->mti_pill, &RMF_ACL, RCL_SERVER, 0);
1039 rc = req_capsule_server_pack(mti->mti_pill);
1041 RETURN(err_serious(rc));
1043 rep = req_capsule_server_get(mti->mti_pill, &RMF_DLM_REP);
1047 mbo = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1052 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
1053 * lock was found by ldlm_handle_enqueue(); if so no need to grant
1055 if (flags & LDLM_FL_RESENT) {
1056 rc = LDLM_ITER_CONTINUE;
1059 enum ldlm_error err;
1061 rc = policy(lock, &tmpflags, LDLM_PROCESS_RESCAN, &err, NULL);
1062 check_res_locked(res);
1066 /* The lock met with no resistance; we're finished. */
1067 if (rc == LDLM_ITER_CONTINUE) {
1068 GOTO(fill_mbo, rc = ELDLM_LOCK_REPLACED);
1069 } else if (flags & LDLM_FL_BLOCK_NOWAIT) {
1070 /* LDLM_FL_BLOCK_NOWAIT means it is for AGL. Do not send glimpse
1071 * callback for glimpse size. The real size user will trigger
1072 * the glimpse callback when necessary. */
1073 GOTO(fill_mbo, rc = ELDLM_LOCK_ABORTED);
1076 rc = mdt_do_glimpse(mti->mti_env, ns, res);
1077 if (rc == -ENOENT) {
1078 /* We are racing with unlink(); just return -ENOENT */
1079 rep->lock_policy_res2 = ptlrpc_status_hton(-ENOENT);
1081 } else if (rc == -EINVAL) {
1082 /* this is possible is client lock has been cancelled but
1083 * still exists on server. If that lock was found on server
1084 * as only conflicting lock then the client has already
1085 * size authority and glimpse is not needed. */
1086 CDEBUG(D_DLMTRACE, "Glimpse from the client owning lock\n");
1088 } else if (rc < 0) {
1091 rc = ELDLM_LOCK_ABORTED;
1093 /* LVB can be without valid data in case of DOM */
1094 if (!mdt_dom_lvb_is_valid(res))
1095 mdt_dom_lvbo_update(res, lock, NULL, false);
1096 mdt_lvb2body(res, mbo);
1100 int mdt_brw_enqueue(struct mdt_thread_info *mti, struct ldlm_namespace *ns,
1101 struct ldlm_lock **lockp, __u64 flags)
1103 struct tgt_session_info *tsi = tgt_ses_info(mti->mti_env);
1104 struct lu_fid *fid = &tsi->tsi_fid;
1105 struct ldlm_lock *lock = *lockp;
1106 struct ldlm_resource *res = lock->l_resource;
1107 struct ldlm_reply *rep;
1108 struct mdt_body *mbo;
1109 struct mdt_lock_handle *lhc = &mti->mti_lh[MDT_LH_RMT];
1110 struct mdt_object *mo;
1115 /* Get lock from request for possible resent case. */
1116 mdt_intent_fixup_resent(mti, *lockp, lhc, flags);
1117 req_capsule_set_size(mti->mti_pill, &RMF_MDT_MD, RCL_SERVER, 0);
1118 req_capsule_set_size(mti->mti_pill, &RMF_ACL, RCL_SERVER, 0);
1119 rc = req_capsule_server_pack(mti->mti_pill);
1121 RETURN(err_serious(rc));
1123 rep = req_capsule_server_get(mti->mti_pill, &RMF_DLM_REP);
1127 mbo = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1131 fid_extract_from_res_name(fid, &res->lr_name);
1132 mo = mdt_object_find(mti->mti_env, mti->mti_mdt, fid);
1133 if (unlikely(IS_ERR(mo)))
1134 RETURN(PTR_ERR(mo));
1136 if (!mdt_object_exists(mo))
1137 GOTO(out, rc = -ENOENT);
1139 if (mdt_object_remote(mo))
1140 GOTO(out, rc = -EPROTO);
1143 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1144 mdt_lock_handle_init(lhc);
1145 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
1146 /* This will block MDT thread but it should be fine until
1147 * client caches small amount of data for DoM, which should be
1148 * smaller than one BRW RPC and should be able to be
1149 * piggybacked by lock cancel RPC.
1150 * If the client could hold the lock too long, this code can be
1151 * revised to call mdt_object_lock_try(). And if fails, it will
1152 * return ELDLM_OK here and fall back into normal lock enqueue
1155 rc = mdt_object_lock(mti, mo, lhc, MDS_INODELOCK_DOM);
1160 if (!mdt_dom_lvb_is_valid(res)) {
1161 rc = mdt_dom_lvb_alloc(res);
1164 mdt_dom_disk_lvbo_update(mti->mti_env, mo, res, false);
1166 mdt_lvb2body(res, mbo);
1168 rep->lock_policy_res2 = clear_serious(rc);
1169 if (rep->lock_policy_res2) {
1170 lhc->mlh_reg_lh.cookie = 0ull;
1171 GOTO(out, rc = ELDLM_LOCK_ABORTED);
1174 rc = mdt_intent_lock_replace(mti, lockp, lhc, flags, rc);
1176 mdt_object_put(mti->mti_env, mo);
1180 void mdt_dom_discard_data(struct mdt_thread_info *info,
1181 const struct lu_fid *fid)
1183 struct mdt_device *mdt = info->mti_mdt;
1184 union ldlm_policy_data *policy = &info->mti_policy;
1185 struct ldlm_res_id *res_id = &info->mti_res_id;
1186 struct lustre_handle dom_lh;
1187 __u64 flags = LDLM_FL_AST_DISCARD_DATA;
1190 policy->l_inodebits.bits = MDS_INODELOCK_DOM;
1191 policy->l_inodebits.try_bits = 0;
1192 fid_build_reg_res_name(fid, res_id);
1194 /* Tell the clients that the object is gone now and that they should
1195 * throw away any cached pages. */
1196 rc = ldlm_cli_enqueue_local(mdt->mdt_namespace, res_id, LDLM_IBITS,
1197 policy, LCK_PW, &flags, ldlm_blocking_ast,
1198 ldlm_completion_ast, NULL, NULL, 0,
1199 LVB_T_NONE, NULL, &dom_lh);
1201 /* We only care about the side-effects, just drop the lock. */
1203 ldlm_lock_decref(&dom_lh, LCK_PW);
1206 /* check if client has already DoM lock for given resource */
1207 bool mdt_dom_client_has_lock(struct mdt_thread_info *info,
1208 const struct lu_fid *fid)
1210 struct mdt_device *mdt = info->mti_mdt;
1211 union ldlm_policy_data *policy = &info->mti_policy;
1212 struct ldlm_res_id *res_id = &info->mti_res_id;
1213 struct lustre_handle lockh;
1214 enum ldlm_mode mode;
1215 struct ldlm_lock *lock;
1218 policy->l_inodebits.bits = MDS_INODELOCK_DOM;
1219 fid_build_reg_res_name(fid, res_id);
1221 mode = ldlm_lock_match(mdt->mdt_namespace, LDLM_FL_BLOCK_GRANTED |
1222 LDLM_FL_TEST_LOCK, res_id, LDLM_IBITS, policy,
1225 /* There is no other PW lock on this object; finished. */
1229 lock = ldlm_handle2lock(&lockh);
1233 /* check if lock from the same client */
1234 rc = (lock->l_export->exp_handle.h_cookie ==
1235 info->mti_exp->exp_handle.h_cookie);
1236 LDLM_LOCK_PUT(lock);
1241 * MDT request handler for OST_GETATTR RPC.
1243 * This is data-specific request to get object and layout versions under
1244 * IO lock. It is reliable only for Data-on-MDT files.
1246 * \param[in] tsi target session environment for this request
1248 * \retval 0 if successful
1249 * \retval negative value on error
1251 int mdt_data_version_get(struct tgt_session_info *tsi)
1253 struct mdt_thread_info *mti = mdt_th_info(tsi->tsi_env);
1254 struct mdt_device *mdt = mti->mti_mdt;
1255 struct mdt_body *repbody;
1256 struct mdt_object *mo = mti->mti_object;
1257 struct lov_comp_md_v1 *comp;
1258 struct lustre_handle lh = { 0 };
1261 enum ldlm_mode lock_mode = LCK_PR;
1267 req_capsule_set_size(tsi->tsi_pill, &RMF_MDT_MD, RCL_SERVER, 0);
1268 req_capsule_set_size(tsi->tsi_pill, &RMF_ACL, RCL_SERVER, 0);
1269 rc = req_capsule_server_pack(tsi->tsi_pill);
1270 if (unlikely(rc != 0))
1271 RETURN(err_serious(rc));
1273 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1274 if (repbody == NULL)
1277 srvlock = tsi->tsi_mdt_body->mbo_valid & OBD_MD_FLFLAGS &&
1278 tsi->tsi_mdt_body->mbo_flags & OBD_FL_SRVLOCK;
1281 if (unlikely(tsi->tsi_mdt_body->mbo_flags & OBD_FL_FLUSH))
1284 fid_build_reg_res_name(&tsi->tsi_fid, &tsi->tsi_resid);
1285 rc = tgt_mdt_data_lock(mdt->mdt_namespace, &tsi->tsi_resid,
1286 &lh, lock_mode, &flags);
1291 if (!mdt_object_exists(mo))
1292 GOTO(out, rc = -ENOENT);
1293 if (mdt_object_remote(mo))
1294 GOTO(out, rc = -EREMOTE);
1295 if (!S_ISREG(lu_object_attr(&mo->mot_obj)))
1296 GOTO(out, rc = -EBADF);
1298 /* Get version first */
1299 version = dt_version_get(tsi->tsi_env, mdt_obj2dt(mo));
1300 if (version && version != -EOPNOTSUPP) {
1301 repbody->mbo_valid |= OBD_MD_FLDATAVERSION;
1302 /* re-use mbo_ioepoch to transfer version */
1303 repbody->mbo_version = version;
1306 /* Read layout to get its version */
1307 rc = mdt_big_xattr_get(mti, mo, XATTR_NAME_LOV);
1308 if (rc == -ENODATA) /* File has no layout yet */
1313 comp = mti->mti_buf.lb_buf;
1314 if (le32_to_cpu(comp->lcm_magic) != LOV_MAGIC_COMP_V1) {
1315 CDEBUG(D_INFO, DFID" has no composite layout",
1316 PFID(&tsi->tsi_fid));
1317 GOTO(out, rc = -ESTALE);
1320 CDEBUG(D_INODE, DFID": layout version: %u\n",
1321 PFID(&tsi->tsi_fid), le32_to_cpu(comp->lcm_layout_gen));
1323 repbody->mbo_valid |= OBD_MD_LAYOUT_VERSION;
1324 /* re-use mbo_rdev for that */
1325 repbody->mbo_layout_gen = le32_to_cpu(comp->lcm_layout_gen);
1329 tgt_mdt_data_unlock(&lh, lock_mode);
1331 repbody->mbo_valid |= OBD_MD_FLFLAGS;
1332 repbody->mbo_flags = OBD_FL_FLUSH;