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_dom_read_lock(struct mdt_object *mo)
40 down_read(&mo->mot_dom_sem);
43 static inline void mdt_dom_read_unlock(struct mdt_object *mo)
45 up_read(&mo->mot_dom_sem);
48 static inline void mdt_dom_write_lock(struct mdt_object *mo)
50 down_write(&mo->mot_dom_sem);
53 static inline void mdt_dom_write_unlock(struct mdt_object *mo)
55 up_write(&mo->mot_dom_sem);
58 static void mdt_dom_resource_prolong(struct ldlm_prolong_args *arg)
60 struct ldlm_resource *res;
61 struct ldlm_lock *lock;
65 res = ldlm_resource_get(arg->lpa_export->exp_obd->obd_namespace, NULL,
66 &arg->lpa_resid, LDLM_IBITS, 0);
69 "Failed to get resource for resid %llu/%llu\n",
70 arg->lpa_resid.name[0], arg->lpa_resid.name[1]);
75 list_for_each_entry(lock, &res->lr_granted, l_res_link) {
76 if (ldlm_has_dom(lock)) {
77 LDLM_DEBUG(lock, "DOM lock to prolong ");
78 ldlm_lock_prolong_one(lock, arg);
79 /* only one PW or EX lock can be granted,
80 * no need to continue search
82 if (lock->l_granted_mode & (LCK_PW | LCK_EX))
87 ldlm_resource_putref(res);
92 static void mdt_prolong_dom_lock(struct tgt_session_info *tsi,
93 struct ldlm_prolong_args *data)
95 struct obdo *oa = &tsi->tsi_ost_body->oa;
96 struct ldlm_lock *lock;
100 data->lpa_timeout = prolong_timeout(tgt_ses_req(tsi));
101 data->lpa_export = tsi->tsi_exp;
102 data->lpa_resid = tsi->tsi_resid;
104 CDEBUG(D_RPCTRACE, "Prolong DOM lock for req %p with x%llu\n",
105 tgt_ses_req(tsi), tgt_ses_req(tsi)->rq_xid);
107 if (oa->o_valid & OBD_MD_FLHANDLE) {
108 /* mostly a request should be covered by only one lock, try
110 lock = ldlm_handle2lock(&oa->o_handle);
112 LASSERT(lock->l_export == data->lpa_export);
113 ldlm_lock_prolong_one(lock, data);
114 lock->l_last_used = ktime_get();
116 if (data->lpa_locks_cnt > 0)
120 mdt_dom_resource_prolong(data);
124 static int mdt_rw_hpreq_lock_match(struct ptlrpc_request *req,
125 struct ldlm_lock *lock)
127 struct obd_ioobj *ioo;
129 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
133 if (!(lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_DOM))
136 ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
137 LASSERT(ioo != NULL);
139 LASSERT(lock->l_resource != NULL);
140 if (!fid_res_name_eq(&ioo->ioo_oid.oi_fid, &lock->l_resource->lr_name))
143 /* a bulk write can only hold a reference on a PW extent lock. */
144 mode = LCK_PW | LCK_GROUP;
146 /* whereas a bulk read can be protected by either a PR or PW
150 if (!(lock->l_granted_mode & mode))
156 static int mdt_rw_hpreq_check(struct ptlrpc_request *req)
158 struct tgt_session_info *tsi;
159 struct obd_ioobj *ioo;
160 struct niobuf_remote *rnb;
162 struct ldlm_prolong_args pa = { 0 };
166 /* Don't use tgt_ses_info() to get session info, because lock_match()
167 * can be called while request has no processing thread yet. */
168 tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
171 * Use LASSERT below because malformed RPCs should have
172 * been filtered out in tgt_hpreq_handler().
174 opc = lustre_msg_get_opc(req->rq_reqmsg);
175 LASSERT(opc == OST_READ || opc == OST_WRITE);
177 ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
178 LASSERT(ioo != NULL);
180 rnb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
181 LASSERT(rnb != NULL);
182 LASSERT(!(rnb->rnb_flags & OBD_BRW_SRVLOCK));
184 pa.lpa_mode = LCK_PW | LCK_GROUP;
186 pa.lpa_mode |= LCK_PR;
188 DEBUG_REQ(D_RPCTRACE, req, "%s %s: refresh rw locks for " DFID,
189 tgt_name(tsi->tsi_tgt), current->comm, PFID(&tsi->tsi_fid));
191 mdt_prolong_dom_lock(tsi, &pa);
193 if (pa.lpa_blocks_cnt > 0) {
195 "%s: refreshed %u locks timeout for req %p\n",
196 tgt_name(tsi->tsi_tgt), pa.lpa_blocks_cnt, req);
200 RETURN(pa.lpa_locks_cnt > 0 ? 0 : -ESTALE);
203 static void mdt_rw_hpreq_fini(struct ptlrpc_request *req)
205 mdt_rw_hpreq_check(req);
208 static struct ptlrpc_hpreq_ops mdt_hpreq_rw = {
209 .hpreq_lock_match = mdt_rw_hpreq_lock_match,
210 .hpreq_check = mdt_rw_hpreq_check,
211 .hpreq_fini = mdt_rw_hpreq_fini
215 * Assign high priority operations to an IO request.
217 * Check if the incoming request is a candidate for
218 * high-priority processing. If it is, assign it a high
219 * priority operations table.
221 * \param[in] tsi target session environment for this request
223 void mdt_hp_brw(struct tgt_session_info *tsi)
225 struct niobuf_remote *rnb;
226 struct obd_ioobj *ioo;
230 ioo = req_capsule_client_get(tsi->tsi_pill, &RMF_OBD_IOOBJ);
231 LASSERT(ioo != NULL); /* must exist after request preprocessing */
232 if (ioo->ioo_bufcnt > 0) {
233 rnb = req_capsule_client_get(tsi->tsi_pill, &RMF_NIOBUF_REMOTE);
234 LASSERT(rnb != NULL); /* must exist after preprocessing */
236 /* no high priority if server lock is needed */
237 if (rnb->rnb_flags & OBD_BRW_SRVLOCK ||
238 (lustre_msg_get_flags(tgt_ses_req(tsi)->rq_reqmsg) &
242 tgt_ses_req(tsi)->rq_ops = &mdt_hpreq_rw;
245 static int mdt_punch_hpreq_lock_match(struct ptlrpc_request *req,
246 struct ldlm_lock *lock)
248 struct tgt_session_info *tsi;
253 /* Don't use tgt_ses_info() to get session info, because lock_match()
254 * can be called while request has no processing thread yet. */
255 tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
258 * Use LASSERT below because malformed RPCs should have
259 * been filtered out in tgt_hpreq_handler().
261 LASSERT(tsi->tsi_ost_body != NULL);
262 if (tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLHANDLE &&
263 tsi->tsi_ost_body->oa.o_handle.cookie == lock->l_handle.h_cookie)
266 oa = &tsi->tsi_ost_body->oa;
268 LASSERT(lock->l_resource != NULL);
269 if (!fid_res_name_eq(&oa->o_oi.oi_fid, &lock->l_resource->lr_name))
272 if (!(lock->l_granted_mode & (LCK_PW | LCK_GROUP)))
279 * Implementation of ptlrpc_hpreq_ops::hpreq_lock_check for OST_PUNCH request.
281 * High-priority queue request check for whether the given punch request
282 * (\a req) is blocking an LDLM lock cancel. Also checks whether the request is
283 * covered by an LDLM lock.
287 * \param[in] req the incoming request
289 * \retval 1 if \a req is blocking an LDLM lock cancel
290 * \retval 0 if it is not
291 * \retval -ESTALE if lock is not found
293 static int mdt_punch_hpreq_check(struct ptlrpc_request *req)
295 struct tgt_session_info *tsi;
297 struct ldlm_prolong_args pa = { 0 };
301 /* Don't use tgt_ses_info() to get session info, because lock_match()
302 * can be called while request has no processing thread yet. */
303 tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
304 LASSERT(tsi != NULL);
305 oa = &tsi->tsi_ost_body->oa;
307 LASSERT(!(oa->o_valid & OBD_MD_FLFLAGS &&
308 oa->o_flags & OBD_FL_SRVLOCK));
310 pa.lpa_mode = LCK_PW | LCK_GROUP;
312 CDEBUG(D_DLMTRACE, "%s: refresh DOM lock for "DFID"\n",
313 tgt_name(tsi->tsi_tgt), PFID(&tsi->tsi_fid));
315 mdt_prolong_dom_lock(tsi, &pa);
317 if (pa.lpa_blocks_cnt > 0) {
319 "%s: refreshed %u locks timeout for req %p.\n",
320 tgt_name(tsi->tsi_tgt), pa.lpa_blocks_cnt, req);
324 RETURN(pa.lpa_locks_cnt > 0 ? 0 : -ESTALE);
328 * Implementation of ptlrpc_hpreq_ops::hpreq_lock_fini for OST_PUNCH request.
330 * Called after the request has been handled. It refreshes lock timeout again
331 * so that client has more time to send lock cancel RPC.
333 * \param[in] req request which is being processed.
335 static void mdt_punch_hpreq_fini(struct ptlrpc_request *req)
337 mdt_punch_hpreq_check(req);
340 static struct ptlrpc_hpreq_ops mdt_hpreq_punch = {
341 .hpreq_lock_match = mdt_punch_hpreq_lock_match,
342 .hpreq_check = mdt_punch_hpreq_check,
343 .hpreq_fini = mdt_punch_hpreq_fini
346 void mdt_hp_punch(struct tgt_session_info *tsi)
348 LASSERT(tsi->tsi_ost_body != NULL); /* must exists if we are here */
349 /* no high-priority if server lock is needed */
350 if ((tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLFLAGS &&
351 tsi->tsi_ost_body->oa.o_flags & OBD_FL_SRVLOCK) ||
352 tgt_conn_flags(tsi) & OBD_CONNECT_MDS ||
353 lustre_msg_get_flags(tgt_ses_req(tsi)->rq_reqmsg) & MSG_REPLAY)
355 tgt_ses_req(tsi)->rq_ops = &mdt_hpreq_punch;
358 static int mdt_preprw_read(const struct lu_env *env, struct obd_export *exp,
359 struct mdt_device *mdt, struct mdt_object *mo,
360 struct lu_attr *la, int niocount,
361 struct niobuf_remote *rnb, int *nr_local,
362 struct niobuf_local *lnb, char *jobid)
364 struct tgt_session_info *tsi = tgt_ses_info(env);
365 struct ptlrpc_request *req = tgt_ses_req(tsi);
366 struct dt_object *dob;
367 int i, j, rc, tot_bytes = 0;
368 int maxlnb = *nr_local;
373 mdt_dom_read_lock(mo);
375 /* the only valid case when READ can find object is missing or stale
376 * when export is just evicted and open files are closed forcefully
377 * on server while client's READ can be in progress.
378 * This should not happen on healthy export, object can't be missing
379 * or dying because both states means it was finally destroyed.
381 level = exp->exp_failed ? D_INFO : D_ERROR;
382 if (!mdt_object_exists(mo)) {
384 "%s: READ IO to missing obj "DFID": rc = %d\n",
385 exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)),
387 /* return 0 and continue with empty commit to skip such READ
388 * without more BRW errors.
392 if (lu_object_is_dying(&mo->mot_header)) {
394 "%s: READ IO to stale obj "DFID": rc = %d\n",
395 exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)),
397 /* return 0 and continue with empty commit to skip such READ
398 * without more BRW errors.
403 dob = mdt_obj2dt(mo);
404 /* parse remote buffers to local buffers and prepare the latter */
405 for (i = 0, j = 0; i < niocount; i++) {
406 rc = dt_bufs_get(env, dob, rnb + i, lnb + j, maxlnb, 0);
407 if (unlikely(rc < 0))
409 /* correct index for local buffers to continue with */
413 tot_bytes += rnb[i].rnb_len;
416 rc = dt_attr_get(env, dob, la);
420 rc = dt_read_prep(env, dob, lnb, *nr_local);
424 mdt_counter_incr(req, LPROC_MDT_IO_READ, tot_bytes);
427 dt_bufs_put(env, dob, lnb, *nr_local);
428 mdt_dom_read_unlock(mo);
432 static int mdt_preprw_write(const struct lu_env *env, struct obd_export *exp,
433 struct mdt_device *mdt, struct mdt_object *mo,
434 struct lu_attr *la, struct obdo *oa,
435 int objcount, struct obd_ioobj *obj,
436 struct niobuf_remote *rnb, int *nr_local,
437 struct niobuf_local *lnb, char *jobid)
439 struct tgt_session_info *tsi = tgt_ses_info(env);
440 struct ptlrpc_request *req = tgt_ses_req(tsi);
441 struct dt_object *dob;
442 int i, j, k, rc = 0, tot_bytes = 0;
443 int maxlnb = *nr_local;
447 /* Process incoming grant info, set OBD_BRW_GRANTED flag and grant some
448 * space back if possible */
449 tgt_grant_prepare_write(env, exp, oa, rnb, obj->ioo_bufcnt);
451 mdt_dom_read_lock(mo);
453 /* don't report error in cases with failed export */
454 if (!mdt_object_exists(mo)) {
455 int level = exp->exp_failed ? D_INFO : D_ERROR;
459 "%s: WRITE IO to missing obj "DFID": rc = %d\n",
460 exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)),
462 /* exit with no data written, note nr_local = 0 above */
465 if (lu_object_is_dying(&mo->mot_header)) {
466 /* This is possible race between object destroy followed by
467 * discard BL AST and client cache flushing. Object is
468 * referenced until discard finish.
470 CDEBUG(D_INODE, "WRITE IO to stale object "DFID"\n",
471 PFID(mdt_object_fid(mo)));
472 /* Note: continue with no error here to don't cause BRW errors
473 * but skip transaction in commitrw silently so no data is
478 dob = mdt_obj2dt(mo);
479 /* parse remote buffers to local buffers and prepare the latter */
480 for (i = 0, j = 0; i < obj->ioo_bufcnt; i++) {
481 rc = dt_bufs_get(env, dob, rnb + i, lnb + j, maxlnb, 1);
482 if (unlikely(rc < 0))
484 /* correct index for local buffers to continue with */
485 for (k = 0; k < rc; k++) {
486 lnb[j + k].lnb_flags = rnb[i].rnb_flags;
487 if (!(rnb[i].rnb_flags & OBD_BRW_GRANTED))
488 lnb[j + k].lnb_rc = -ENOSPC;
493 tot_bytes += rnb[i].rnb_len;
496 rc = dt_write_prep(env, dob, lnb, *nr_local);
500 mdt_counter_incr(req, LPROC_MDT_IO_WRITE, tot_bytes);
503 dt_bufs_put(env, dob, lnb, *nr_local);
505 mdt_dom_read_unlock(mo);
506 /* tgt_grant_prepare_write() was called, so we must commit */
507 tgt_grant_commit(exp, oa->o_grant_used, rc);
508 /* let's still process incoming grant information packed in the oa,
509 * but without enforcing grant since we won't proceed with the write.
510 * Just like a read request actually. */
511 tgt_grant_prepare_read(env, exp, oa);
515 int mdt_obd_preprw(const struct lu_env *env, int cmd, struct obd_export *exp,
516 struct obdo *oa, int objcount, struct obd_ioobj *obj,
517 struct niobuf_remote *rnb, int *nr_local,
518 struct niobuf_local *lnb)
520 struct tgt_session_info *tsi = tgt_ses_info(env);
521 struct mdt_thread_info *info = tsi2mdt_info(tsi);
522 struct lu_attr *la = &info->mti_attr.ma_attr;
523 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
524 struct mdt_object *mo;
528 /* The default value PTLRPC_MAX_BRW_PAGES is set in tgt_brw_write()
529 * but for MDT it is different, correct it here. */
530 if (*nr_local > MD_MAX_BRW_PAGES)
531 *nr_local = MD_MAX_BRW_PAGES;
533 jobid = tsi->tsi_jobid;
535 if (!oa || objcount != 1 || obj->ioo_bufcnt == 0) {
536 CERROR("%s: bad parameters %p/%i/%i\n",
537 exp->exp_obd->obd_name, oa, objcount, obj->ioo_bufcnt);
541 mo = mdt_object_find(env, mdt, &tsi->tsi_fid);
543 GOTO(out, rc = PTR_ERR(mo));
545 LASSERT(info->mti_object == NULL);
546 info->mti_object = mo;
548 if (cmd == OBD_BRW_WRITE) {
549 la_from_obdo(la, oa, OBD_MD_FLGETATTR);
550 rc = mdt_preprw_write(env, exp, mdt, mo, la, oa,
551 objcount, obj, rnb, nr_local, lnb,
553 } else if (cmd == OBD_BRW_READ) {
554 tgt_grant_prepare_read(env, exp, oa);
555 rc = mdt_preprw_read(env, exp, mdt, mo, la,
556 obj->ioo_bufcnt, rnb, nr_local, lnb,
558 obdo_from_la(oa, la, LA_ATIME);
560 CERROR("%s: wrong cmd %d received!\n",
561 exp->exp_obd->obd_name, cmd);
565 lu_object_put(env, &mo->mot_obj);
566 info->mti_object = NULL;
572 static int mdt_commitrw_read(const struct lu_env *env, struct mdt_device *mdt,
573 struct mdt_object *mo, int objcount, int niocount,
574 struct niobuf_local *lnb)
576 struct dt_object *dob;
581 dob = mdt_obj2dt(mo);
584 dt_bufs_put(env, dob, lnb, niocount);
586 mdt_dom_read_unlock(mo);
590 static int mdt_commitrw_write(const struct lu_env *env, struct obd_export *exp,
591 struct mdt_device *mdt, struct mdt_object *mo,
592 struct lu_attr *la, struct obdo *oa, int objcount,
593 int niocount, struct niobuf_local *lnb,
594 unsigned long granted, int old_rc)
596 struct dt_device *dt = mdt->mdt_bottom;
597 struct dt_object *dob;
605 dob = mdt_obj2dt(mo);
608 GOTO(out, rc = old_rc);
610 la->la_valid &= LA_ATIME | LA_MTIME | LA_CTIME;
612 if (!dt_object_exists(dob))
613 GOTO(out, rc = -ENOENT);
617 DEBUG_REQ(D_WARNING, tgt_ses_req(tgt_ses_info(env)),
618 "%s: commit with no pages for "DFID": rc = %d\n",
619 exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)), rc);
623 CFS_FAIL_TIMEOUT(OBD_FAIL_MDS_COMMITRW_DELAY, cfs_fail_val);
625 th = dt_trans_create(env, dt);
627 GOTO(out, rc = PTR_ERR(th));
629 for (i = 0; i < niocount; i++) {
630 if (!(lnb[i].lnb_flags & OBD_BRW_ASYNC)) {
636 if (OBD_FAIL_CHECK(OBD_FAIL_OST_DQACQ_NET))
637 GOTO(out_stop, rc = -EINPROGRESS);
639 rc = dt_declare_write_commit(env, dob, lnb, niocount, th);
644 /* update [mac]time if needed */
645 rc = dt_declare_attr_set(env, dob, la, th);
650 tgt_vbr_obj_set(env, dob);
651 rc = dt_trans_start(env, dt, th);
655 dt_write_lock(env, dob, 0);
656 if (lu_object_is_dying(&mo->mot_header)) {
657 /* Commit to stale object can be just skipped silently. */
658 CDEBUG(D_INODE, "skip commit to stale object "DFID"\n",
659 PFID(mdt_object_fid(mo)));
660 GOTO(unlock, rc = 0);
662 rc = dt_write_commit(env, dob, lnb, niocount, th, oa->o_size);
664 restart = th->th_restart_tran;
669 rc = dt_attr_set(env, dob, la, th);
673 /* get attr to return */
674 rc = dt_attr_get(env, dob, la);
676 dt_write_unlock(env, dob);
679 /* Force commit to make the just-deleted blocks
680 * reusable. LU-456 */
685 if (rc == 0 && granted > 0) {
686 if (tgt_grant_commit_cb_add(th, exp, granted) == 0)
690 th->th_result = restart ? 0 : rc;
691 dt_trans_stop(env, dt, th);
692 if (rc == -ENOSPC && retries++ < 3) {
693 CDEBUG(D_INODE, "retry after force commit, retries:%d\n",
700 if (retries % 10000 == 0)
701 CERROR("%s: restart IO write too many times: %d\n",
702 exp->exp_obd->obd_name, retries);
703 CDEBUG(D_INODE, "retry transaction, retries:%d\n",
709 dt_bufs_put(env, dob, lnb, niocount);
710 mdt_dom_read_unlock(mo);
712 tgt_grant_commit(exp, granted, old_rc);
716 void mdt_dom_obj_lvb_update(const struct lu_env *env, struct mdt_object *mo,
719 struct mdt_device *mdt = mdt_dev(mo->mot_obj.lo_dev);
720 struct ldlm_res_id resid;
721 struct ldlm_resource *res;
723 fid_build_reg_res_name(mdt_object_fid(mo), &resid);
724 res = ldlm_resource_get(mdt->mdt_namespace, NULL, &resid,
729 /* Update lvbo data if exists. */
730 if (mdt_dom_lvb_is_valid(res))
731 mdt_dom_disk_lvbo_update(env, mo, res, increase_only);
732 ldlm_resource_putref(res);
735 int mdt_obd_commitrw(const struct lu_env *env, int cmd, struct obd_export *exp,
736 struct obdo *oa, int objcount, struct obd_ioobj *obj,
737 struct niobuf_remote *rnb, int npages,
738 struct niobuf_local *lnb, int old_rc)
740 struct mdt_thread_info *info = mdt_th_info(env);
741 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
742 struct mdt_object *mo = info->mti_object;
743 struct lu_attr *la = &info->mti_attr.ma_attr;
749 if (cmd == OBD_BRW_WRITE) {
750 /* Don't update timestamps if this write is older than a
751 * setattr which modifies the timestamps. b=10150 */
753 /* XXX when we start having persistent reservations this needs
754 * to be changed to ofd_fmd_get() to create the fmd if it
755 * doesn't already exist so we can store the reservation handle
757 valid = OBD_MD_FLUID | OBD_MD_FLGID;
758 if (tgt_fmd_check(exp, mdt_object_fid(mo),
759 mdt_info_req(info)->rq_xid))
760 valid |= OBD_MD_FLATIME | OBD_MD_FLMTIME |
763 la_from_obdo(la, oa, valid);
765 rc = mdt_commitrw_write(env, exp, mdt, mo, la, oa, objcount,
766 npages, lnb, oa->o_grant_used, old_rc);
768 obdo_from_la(oa, la, VALID_FLAGS | LA_GID | LA_UID);
770 obdo_from_la(oa, la, LA_GID | LA_UID);
772 mdt_dom_obj_lvb_update(env, mo, false);
773 /* don't report overquota flag if we failed before reaching
775 if (old_rc == 0 && (rc == 0 || rc == -EDQUOT)) {
776 /* return the overquota flags to client */
777 if (lnb[0].lnb_flags & OBD_BRW_OVER_USRQUOTA) {
778 if (oa->o_valid & OBD_MD_FLFLAGS)
779 oa->o_flags |= OBD_FL_NO_USRQUOTA;
781 oa->o_flags = OBD_FL_NO_USRQUOTA;
784 if (lnb[0].lnb_flags & OBD_BRW_OVER_GRPQUOTA) {
785 if (oa->o_valid & OBD_MD_FLFLAGS)
786 oa->o_flags |= OBD_FL_NO_GRPQUOTA;
788 oa->o_flags = OBD_FL_NO_GRPQUOTA;
791 if (lnb[0].lnb_flags & OBD_BRW_OVER_PRJQUOTA) {
792 if (oa->o_valid & OBD_MD_FLFLAGS)
793 oa->o_flags |= OBD_FL_NO_PRJQUOTA;
795 oa->o_flags = OBD_FL_NO_PRJQUOTA;
798 oa->o_valid |= OBD_MD_FLFLAGS | OBD_MD_FLUSRQUOTA |
799 OBD_MD_FLGRPQUOTA | OBD_MD_FLPRJQUOTA;
801 } else if (cmd == OBD_BRW_READ) {
802 /* If oa != NULL then mdt_preprw_read updated the inode
803 * atime and we should update the lvb so that other glimpses
804 * will also get the updated value. bug 5972 */
806 mdt_dom_obj_lvb_update(env, mo, true);
807 rc = mdt_commitrw_read(env, mdt, mo, objcount, npages, lnb);
813 mdt_thread_info_fini(info);
817 int mdt_object_punch(const struct lu_env *env, struct dt_device *dt,
818 struct dt_object *dob, __u64 start, __u64 end,
826 /* we support truncate, not punch yet */
827 LASSERT(end == OBD_OBJECT_EOF);
829 if (!dt_object_exists(dob))
832 th = dt_trans_create(env, dt);
836 rc = dt_declare_attr_set(env, dob, la, th);
840 rc = dt_declare_punch(env, dob, start, OBD_OBJECT_EOF, th);
844 tgt_vbr_obj_set(env, dob);
845 rc = dt_trans_start(env, dt, th);
849 dt_write_lock(env, dob, 0);
850 rc = dt_punch(env, dob, start, OBD_OBJECT_EOF, th);
853 rc = dt_attr_set(env, dob, la, th);
857 dt_write_unlock(env, dob);
860 dt_trans_stop(env, dt, th);
864 int mdt_punch_hdl(struct tgt_session_info *tsi)
866 const struct obdo *oa = &tsi->tsi_ost_body->oa;
867 struct ptlrpc_request *req = tgt_ses_req(tsi);
868 struct ost_body *repbody;
869 struct mdt_thread_info *info;
871 struct ldlm_namespace *ns = tsi->tsi_tgt->lut_obd->obd_namespace;
872 struct obd_export *exp = tsi->tsi_exp;
873 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
874 struct mdt_object *mo;
875 struct dt_object *dob;
877 struct lustre_handle lh = { 0, };
878 ktime_t kstart = ktime_get();
885 if ((oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
886 (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
887 RETURN(err_serious(-EPROTO));
889 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
891 RETURN(err_serious(-ENOMEM));
893 /* punch start,end are passed in o_size,o_blocks throught wire */
897 if (end != OBD_OBJECT_EOF) /* Only truncate is supported */
900 info = tsi2mdt_info(tsi);
901 la = &info->mti_attr.ma_attr;
902 /* standard truncate optimization: if file body is completely
903 * destroyed, don't send data back to the server. */
905 flags |= LDLM_FL_AST_DISCARD_DATA;
907 repbody->oa.o_oi = oa->o_oi;
908 repbody->oa.o_valid = OBD_MD_FLID;
910 srvlock = (exp_connect_flags(exp) & OBD_CONNECT_SRVLOCK) &&
911 oa->o_valid & OBD_MD_FLFLAGS &&
912 oa->o_flags & OBD_FL_SRVLOCK;
915 rc = tgt_mdt_data_lock(ns, &tsi->tsi_resid, &lh, LCK_PW,
921 CDEBUG(D_INODE, "calling punch for object "DFID", valid = %#llx"
922 ", start = %lld, end = %lld\n", PFID(&tsi->tsi_fid),
923 oa->o_valid, start, end);
925 mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
927 GOTO(out_unlock, rc = PTR_ERR(mo));
929 if (!mdt_object_exists(mo))
930 GOTO(out_put, rc = -ENOENT);
932 /* Shouldn't happen on dirs */
933 if (S_ISDIR(lu_object_attr(&mo->mot_obj))) {
935 CERROR("%s: Truncate on dir "DFID": rc = %d\n",
936 exp->exp_obd->obd_name, PFID(&tsi->tsi_fid), rc);
940 mdt_dom_write_lock(mo);
941 dob = mdt_obj2dt(mo);
943 la_from_obdo(la, oa, OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
945 la->la_valid |= LA_SIZE;
947 /* MDT supports FMD for Data-on-MDT needs */
948 if (la->la_valid & (LA_ATIME | LA_MTIME | LA_CTIME))
949 tgt_fmd_update(tsi->tsi_exp, &tsi->tsi_fid,
950 tgt_ses_req(tsi)->rq_xid);
952 rc = mdt_object_punch(tsi->tsi_env, mdt->mdt_bottom, dob,
954 mdt_dom_write_unlock(mo);
958 mdt_dom_obj_lvb_update(tsi->tsi_env, mo, false);
959 mdt_counter_incr(req, LPROC_MDT_IO_PUNCH,
960 ktime_us_delta(ktime_get(), kstart));
963 lu_object_put(tsi->tsi_env, &mo->mot_obj);
966 tgt_data_unlock(&lh, LCK_PW);
968 mdt_thread_info_fini(info);
973 * MDT glimpse for Data-on-MDT
975 * If there is write lock on client then function issues glimpse_ast to get
976 * an actual size from that client.
979 int mdt_do_glimpse(const struct lu_env *env, struct ldlm_namespace *ns,
980 struct ldlm_resource *res)
982 union ldlm_policy_data policy;
983 struct lustre_handle lockh;
985 struct ldlm_lock *lock;
986 struct ldlm_glimpse_work *gl_work;
992 /* There can be only one write lock covering data, try to match it. */
993 policy.l_inodebits.bits = MDS_INODELOCK_DOM;
994 mode = ldlm_lock_match(ns, LDLM_FL_TEST_LOCK,
995 &res->lr_name, LDLM_IBITS, &policy,
998 /* There is no PW lock on this object; finished. */
1002 lock = ldlm_handle2lock(&lockh);
1007 * This check is for lock taken in mdt_reint_unlink() that does
1008 * not have l_glimpse_ast set. So the logic is: if there is a lock
1009 * with no l_glimpse_ast set, this object is being destroyed already.
1010 * Hence, if you are grabbing DLM locks on the server, always set
1011 * non-NULL glimpse_ast (e.g., ldlm_request.c::ldlm_glimpse_ast()).
1013 if (lock->l_glimpse_ast == NULL) {
1014 LDLM_DEBUG(lock, "no l_glimpse_ast");
1015 GOTO(out, rc = -ENOENT);
1018 OBD_SLAB_ALLOC_PTR_GFP(gl_work, ldlm_glimpse_work_kmem, GFP_ATOMIC);
1020 GOTO(out, rc = -ENOMEM);
1022 /* Populate the gl_work structure.
1023 * Grab additional reference on the lock which will be released in
1024 * ldlm_work_gl_ast_lock() */
1025 gl_work->gl_lock = LDLM_LOCK_GET(lock);
1026 /* The glimpse callback is sent to one single IO lock. As a result,
1027 * the gl_work list is just composed of one element */
1028 list_add_tail(&gl_work->gl_list, &gl_list);
1029 /* There is actually no need for a glimpse descriptor when glimpsing
1031 gl_work->gl_desc = NULL;
1032 /* the ldlm_glimpse_work structure is allocated on the stack */
1033 gl_work->gl_flags = LDLM_GL_WORK_SLAB_ALLOCATED;
1035 ldlm_glimpse_locks(res, &gl_list); /* this will update the LVB */
1037 /* If the list is not empty, we failed to glimpse a lock and
1038 * must clean it up. Usually due to a race with unlink.*/
1039 if (!list_empty(&gl_list)) {
1040 LDLM_LOCK_RELEASE(lock);
1041 OBD_SLAB_FREE_PTR(gl_work, ldlm_glimpse_work_kmem);
1046 LDLM_LOCK_PUT(lock);
1050 static void mdt_lvb2reply(struct ldlm_resource *res, struct mdt_body *mb,
1051 struct ost_lvb *lvb)
1053 struct ost_lvb *res_lvb;
1056 res_lvb = res->lr_lvb_data;
1061 mb->mbo_dom_size = res_lvb->lvb_size;
1062 mb->mbo_dom_blocks = res_lvb->lvb_blocks;
1063 mb->mbo_mtime = res_lvb->lvb_mtime;
1064 mb->mbo_ctime = res_lvb->lvb_ctime;
1065 mb->mbo_atime = res_lvb->lvb_atime;
1066 mb->mbo_valid |= OBD_MD_FLATIME | OBD_MD_FLCTIME |
1067 OBD_MD_FLMTIME | OBD_MD_DOM_SIZE;
1069 CDEBUG(D_DLMTRACE, "size %llu\n", res_lvb->lvb_size);
1074 * MDT glimpse for Data-on-MDT
1076 * This function is called when MDT get attributes for the DoM object.
1077 * If there is write lock on client then function issues glimpse_ast to get
1078 * an actual size from that client.
1080 int mdt_dom_object_size(const struct lu_env *env, struct mdt_device *mdt,
1081 const struct lu_fid *fid, struct mdt_body *mb,
1084 struct ldlm_res_id resid;
1085 struct ldlm_resource *res;
1090 fid_build_reg_res_name(fid, &resid);
1091 res = ldlm_resource_get(mdt->mdt_namespace, NULL, &resid,
1096 /* Update lvbo data if DoM lock returned or if LVB is not yet valid. */
1097 if (dom_lock || !mdt_dom_lvb_is_valid(res))
1098 mdt_dom_lvbo_update(res, NULL, NULL, false);
1100 mdt_lvb2reply(res, mb, NULL);
1101 ldlm_resource_putref(res);
1106 * MDT DoM lock intent policy (glimpse)
1108 * Intent policy is called when lock has an intent, for DoM file that
1109 * means glimpse lock and policy fills Lock Value Block (LVB).
1111 * If already granted lock is found it will be placed in \a lockp and
1112 * returned back to caller function.
1114 * \param[in] tsi session info
1115 * \param[in,out] lockp pointer to the lock
1116 * \param[in] flags LDLM flags
1118 * \retval ELDLM_LOCK_REPLACED if already granted lock was found
1119 * and placed in \a lockp
1120 * \retval ELDLM_LOCK_ABORTED in other cases except error
1121 * \retval negative value on error
1123 int mdt_glimpse_enqueue(struct mdt_thread_info *mti, struct ldlm_namespace *ns,
1124 struct ldlm_lock **lockp, __u64 flags)
1126 struct ldlm_lock *lock = *lockp;
1127 struct ldlm_resource *res = lock->l_resource;
1128 ldlm_processing_policy policy;
1129 struct ldlm_reply *rep;
1130 struct mdt_body *mbo;
1131 struct ost_lvb *lvb;
1132 bool old_client = !exp_connect_dom_lvb(mti->mti_exp);
1137 policy = ldlm_get_processing_policy(res);
1138 LASSERT(policy != NULL);
1140 if (unlikely(old_client)) {
1141 req_capsule_set_size(mti->mti_pill, &RMF_MDT_MD, RCL_SERVER, 0);
1142 req_capsule_set_size(mti->mti_pill, &RMF_ACL, RCL_SERVER, 0);
1144 req_capsule_set_size(mti->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
1147 rc = req_capsule_server_pack(mti->mti_pill);
1149 RETURN(err_serious(rc));
1151 rep = req_capsule_server_get(mti->mti_pill, &RMF_DLM_REP);
1153 if (unlikely(old_client)) {
1154 mbo = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1158 lvb = req_capsule_server_get(mti->mti_pill, &RMF_DLM_LVB);
1164 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
1165 * lock was found by ldlm_handle_enqueue(); if so no need to grant
1167 if (flags & LDLM_FL_RESENT) {
1168 rc = LDLM_ITER_CONTINUE;
1170 __u64 tmpflags = LDLM_FL_BLOCK_NOWAIT;
1171 enum ldlm_error err;
1173 rc = policy(lock, &tmpflags, LDLM_PROCESS_RESCAN, &err, NULL);
1174 check_res_locked(res);
1178 /* The lock met with no resistance; we're finished. */
1179 if (rc == LDLM_ITER_CONTINUE) {
1180 GOTO(fill_mbo, rc = ELDLM_LOCK_REPLACED);
1181 } else if (flags & LDLM_FL_BLOCK_NOWAIT) {
1182 /* LDLM_FL_BLOCK_NOWAIT means it is for AGL. Do not send glimpse
1183 * callback for glimpse size. The real size user will trigger
1184 * the glimpse callback when necessary. */
1185 GOTO(fill_mbo, rc = ELDLM_LOCK_ABORTED);
1188 rc = mdt_do_glimpse(mti->mti_env, ns, res);
1189 if (rc == -ENOENT) {
1190 /* We are racing with unlink(); just return -ENOENT */
1191 rep->lock_policy_res2 = ptlrpc_status_hton(-ENOENT);
1192 } else if (rc == -EINVAL) {
1193 /* this is possible is client lock has been cancelled but
1194 * still exists on server. If that lock was found on server
1195 * as only conflicting lock then the client has already
1196 * size authority and glimpse is not needed. */
1197 CDEBUG(D_DLMTRACE, "Glimpse from the client owning lock\n");
1198 } else if (rc < 0) {
1201 rc = ELDLM_LOCK_ABORTED;
1203 /* LVB can be without valid data in case of DOM */
1204 if (!mdt_dom_lvb_is_valid(res))
1205 mdt_dom_lvbo_update(res, lock, NULL, false);
1206 mdt_lvb2reply(res, mbo, lvb);
1211 int mdt_brw_enqueue(struct mdt_thread_info *mti, struct ldlm_namespace *ns,
1212 struct ldlm_lock **lockp, __u64 flags)
1214 struct tgt_session_info *tsi = tgt_ses_info(mti->mti_env);
1215 struct lu_fid *fid = &tsi->tsi_fid;
1216 struct ldlm_lock *lock = *lockp;
1217 struct ldlm_resource *res = lock->l_resource;
1218 struct ldlm_reply *rep;
1219 struct mdt_body *mbo;
1220 struct mdt_lock_handle *lhc = &mti->mti_lh[MDT_LH_RMT];
1221 struct mdt_object *mo;
1226 req_capsule_set_size(mti->mti_pill, &RMF_MDT_MD, RCL_SERVER, 0);
1227 req_capsule_set_size(mti->mti_pill, &RMF_ACL, RCL_SERVER, 0);
1228 rc = req_capsule_server_pack(mti->mti_pill);
1230 RETURN(err_serious(rc));
1232 rep = req_capsule_server_get(mti->mti_pill, &RMF_DLM_REP);
1236 mbo = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1240 fid_extract_from_res_name(fid, &res->lr_name);
1241 mo = mdt_object_find(mti->mti_env, mti->mti_mdt, fid);
1242 if (unlikely(IS_ERR(mo)))
1243 RETURN(PTR_ERR(mo));
1245 if (!mdt_object_exists(mo))
1246 GOTO(out, rc = -ENOENT);
1248 if (mdt_object_remote(mo))
1249 GOTO(out, rc = -EPROTO);
1251 /* Get lock from request for possible resent case. */
1252 mdt_intent_fixup_resent(mti, *lockp, lhc, flags);
1254 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1255 mdt_lock_handle_init(lhc);
1256 mdt_lh_reg_init(lhc, *lockp);
1258 /* This will block MDT thread but it should be fine until
1259 * client caches small amount of data for DoM, which should be
1260 * smaller than one BRW RPC and should be able to be
1261 * piggybacked by lock cancel RPC.
1262 * If the client could hold the lock too long, this code can be
1263 * revised to call mdt_object_lock_try(). And if fails, it will
1264 * return ELDLM_OK here and fall back into normal lock enqueue
1267 rc = mdt_object_lock(mti, mo, lhc, MDS_INODELOCK_DOM);
1272 if (!mdt_dom_lvb_is_valid(res)) {
1273 rc = mdt_dom_lvb_alloc(res);
1276 mdt_dom_disk_lvbo_update(mti->mti_env, mo, res, false);
1278 mdt_lvb2reply(res, mbo, NULL);
1280 rep->lock_policy_res2 = clear_serious(rc);
1281 if (rep->lock_policy_res2) {
1282 lhc->mlh_reg_lh.cookie = 0ull;
1283 GOTO(out, rc = ELDLM_LOCK_ABORTED);
1286 rc = mdt_intent_lock_replace(mti, lockp, lhc, flags, rc);
1289 lhc->mlh_reg_lh.cookie = 0ull;
1290 mdt_object_put(mti->mti_env, mo);
1294 /* check if client has already DoM lock for given resource */
1295 bool mdt_dom_client_has_lock(struct mdt_thread_info *info,
1296 const struct lu_fid *fid)
1298 struct mdt_device *mdt = info->mti_mdt;
1299 union ldlm_policy_data *policy = &info->mti_policy;
1300 struct ldlm_res_id *res_id = &info->mti_res_id;
1301 __u64 open_flags = info->mti_spec.sp_cr_flags;
1302 struct lustre_handle lockh;
1303 enum ldlm_mode mode;
1304 struct ldlm_lock *lock;
1308 policy->l_inodebits.bits = MDS_INODELOCK_DOM;
1309 fid_build_reg_res_name(fid, res_id);
1312 lm = (open_flags & MDS_FMODE_WRITE) ? LCK_PW : LCK_PR | LCK_PW;
1313 mode = ldlm_lock_match(mdt->mdt_namespace, LDLM_FL_BLOCK_GRANTED |
1314 LDLM_FL_TEST_LOCK, res_id, LDLM_IBITS, policy,
1317 /* There is no other PW lock on this object; finished. */
1321 lock = ldlm_handle2lock(&lockh);
1325 /* check if lock from the same client */
1326 rc = (lock->l_export->exp_handle.h_cookie ==
1327 info->mti_exp->exp_handle.h_cookie);
1328 LDLM_LOCK_PUT(lock);
1333 * MDT request handler for OST_GETATTR RPC.
1335 * This is data-specific request to get object and layout versions under
1336 * IO lock. It is reliable only for Data-on-MDT files.
1338 * \param[in] tsi target session environment for this request
1340 * \retval 0 if successful
1341 * \retval negative value on error
1343 int mdt_data_version_get(struct tgt_session_info *tsi)
1345 struct mdt_thread_info *mti = mdt_th_info(tsi->tsi_env);
1346 struct mdt_device *mdt = mti->mti_mdt;
1347 struct mdt_body *repbody;
1348 struct mdt_object *mo = mti->mti_object;
1349 struct lov_comp_md_v1 *comp;
1350 struct lustre_handle lh = { 0 };
1353 enum ldlm_mode lock_mode = LCK_PR;
1359 req_capsule_set_size(tsi->tsi_pill, &RMF_MDT_MD, RCL_SERVER, 0);
1360 req_capsule_set_size(tsi->tsi_pill, &RMF_ACL, RCL_SERVER, 0);
1361 rc = req_capsule_server_pack(tsi->tsi_pill);
1362 if (unlikely(rc != 0))
1363 RETURN(err_serious(rc));
1365 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1366 if (repbody == NULL)
1369 srvlock = tsi->tsi_mdt_body->mbo_valid & OBD_MD_FLFLAGS &&
1370 tsi->tsi_mdt_body->mbo_flags & OBD_FL_SRVLOCK;
1373 if (unlikely(tsi->tsi_mdt_body->mbo_flags & OBD_FL_FLUSH))
1376 fid_build_reg_res_name(&tsi->tsi_fid, &tsi->tsi_resid);
1377 rc = tgt_mdt_data_lock(mdt->mdt_namespace, &tsi->tsi_resid,
1378 &lh, lock_mode, &flags);
1383 if (!mdt_object_exists(mo))
1384 GOTO(out, rc = -ENOENT);
1385 if (mdt_object_remote(mo))
1386 GOTO(out, rc = -EREMOTE);
1387 if (!S_ISREG(lu_object_attr(&mo->mot_obj)))
1388 GOTO(out, rc = -EBADF);
1390 /* Get version first */
1391 version = dt_version_get(tsi->tsi_env, mdt_obj2dt(mo));
1392 if (version && version != -EOPNOTSUPP) {
1393 repbody->mbo_valid |= OBD_MD_FLDATAVERSION;
1394 /* re-use mbo_ioepoch to transfer version */
1395 repbody->mbo_version = version;
1398 /* Read layout to get its version */
1399 rc = mdt_big_xattr_get(mti, mo, XATTR_NAME_LOV);
1400 if (rc == -ENODATA) /* File has no layout yet */
1405 comp = mti->mti_buf.lb_buf;
1406 if (le32_to_cpu(comp->lcm_magic) != LOV_MAGIC_COMP_V1) {
1407 CDEBUG(D_INFO, DFID" has no composite layout",
1408 PFID(&tsi->tsi_fid));
1409 GOTO(out, rc = -ESTALE);
1412 CDEBUG(D_INODE, DFID": layout version: %u\n",
1413 PFID(&tsi->tsi_fid), le32_to_cpu(comp->lcm_layout_gen));
1415 repbody->mbo_valid |= OBD_MD_LAYOUT_VERSION;
1416 /* re-use mbo_rdev for that */
1417 repbody->mbo_layout_gen = le32_to_cpu(comp->lcm_layout_gen);
1421 tgt_data_unlock(&lh, lock_mode);
1423 repbody->mbo_valid |= OBD_MD_FLFLAGS;
1424 repbody->mbo_flags = OBD_FL_FLUSH;
1428 /* read file data to the buffer */
1429 int mdt_dom_read_on_open(struct mdt_thread_info *mti, struct mdt_device *mdt,
1430 struct lustre_handle *lh)
1432 const struct lu_env *env = mti->mti_env;
1433 struct tgt_session_info *tsi = tgt_ses_info(env);
1434 struct req_capsule *pill = tsi->tsi_pill;
1435 const struct lu_fid *fid;
1436 struct ptlrpc_request *req = tgt_ses_req(tsi);
1437 struct mdt_body *mbo;
1438 struct dt_device *dt = mdt->mdt_bottom;
1439 struct dt_object *mo;
1441 struct niobuf_remote *rnb = NULL;
1442 struct niobuf_local *lnb;
1445 unsigned int len, copied = 0;
1446 __u64 real_dom_size;
1447 int lnbs, nr_local, i;
1448 bool dom_lock = false;
1452 if (!req_capsule_field_present(pill, &RMF_NIOBUF_INLINE, RCL_SERVER)) {
1453 /* There is no reply buffers for this field, this means that
1454 * client has no support for data in reply.
1459 mbo = req_capsule_server_get(pill, &RMF_MDT_BODY);
1460 if (!(mbo->mbo_valid & OBD_MD_DOM_SIZE))
1463 if (!mbo->mbo_dom_size)
1466 if (lustre_handle_is_used(lh)) {
1467 struct ldlm_lock *lock;
1469 lock = ldlm_handle2lock(lh);
1471 dom_lock = ldlm_has_dom(lock) && ldlm_has_layout(lock);
1472 LDLM_LOCK_PUT(lock);
1476 /* return data along with open only along with DoM lock */
1477 if (!dom_lock || !mdt->mdt_opts.mo_dom_read_open)
1480 /* if DoM object holds encrypted content, we need to make sure we
1481 * send whole encryption units, or client will read corrupted content
1483 if (mbo->mbo_valid & LA_FLAGS && mbo->mbo_flags & LUSTRE_ENCRYPT_FL &&
1484 mbo->mbo_dom_size & ~LUSTRE_ENCRYPTION_MASK)
1485 real_dom_size = (mbo->mbo_dom_size & LUSTRE_ENCRYPTION_MASK) +
1486 LUSTRE_ENCRYPTION_UNIT_SIZE;
1488 real_dom_size = mbo->mbo_dom_size;
1490 CDEBUG(D_INFO, "File size %llu, reply sizes %d/%d\n",
1491 real_dom_size, req->rq_reqmsg->lm_repsize, req->rq_replen);
1492 len = req->rq_reqmsg->lm_repsize - req->rq_replen;
1494 /* NB: at this moment we have the following sizes:
1495 * - req->rq_replen: used data in reply
1496 * - req->rq_reqmsg->lm_repsize: total allocated reply buffer at client
1498 * Ideal case when file size fits in allocated reply buffer,
1499 * that mean we can return whole data in reply. We can also fit more
1500 * data up to max_reply_size in total reply size, but this will cause
1501 * re-allocation on client and resend with larger buffer. This is still
1502 * faster than separate READ IO.
1503 * Third case if file is too big to fit even in maximum size, in that
1504 * case we return just tail to optimize possible append.
1506 * At the moment the following strategy is used:
1507 * 1) try to fit into the buffer we have
1508 * 2) return just file tail otherwise.
1510 if (real_dom_size <= len) {
1511 /* can fit whole data */
1512 len = real_dom_size;
1514 } else if (real_dom_size <
1515 mdt_lmm_dom_stripesize(mti->mti_attr.ma_lmm)) {
1518 /* File tail offset must be aligned with larger page size
1519 * between client and server, so the maximum page size is
1520 * used here to align offset.
1522 * NB: DOM feature was introduced when server supports pagebits
1523 * already, so it should be always non-zero value. Report error
1524 * if it is not for some reason.
1526 if (!req->rq_export->exp_target_data.ted_pagebits) {
1527 CERROR("%s: client page bits are not saved on server\n",
1531 pgbits = max_t(int, PAGE_SHIFT,
1532 req->rq_export->exp_target_data.ted_pagebits);
1533 tail = real_dom_size % (1 << pgbits);
1535 /* no partial tail or tail can't fit in reply */
1536 if (tail == 0 || len < tail)
1540 offset = real_dom_size - len;
1542 /* DOM stripe is fully written, so don't expect its tail
1543 * will be used by append.
1548 LASSERT((offset & ~PAGE_MASK) == 0);
1549 rc = req_capsule_server_grow(pill, &RMF_NIOBUF_INLINE,
1550 sizeof(*rnb) + len);
1552 /* failed to grow data buffer, just exit */
1553 GOTO(out, rc = -E2BIG);
1556 /* re-take MDT_BODY and NIOBUF_INLINE buffers after the buffer grow */
1557 mbo = req_capsule_server_get(pill, &RMF_MDT_BODY);
1558 fid = &mbo->mbo_fid1;
1559 if (!fid_is_sane(fid))
1560 GOTO(out, rc = -EINVAL);
1562 rnb = req_capsule_server_get(tsi->tsi_pill, &RMF_NIOBUF_INLINE);
1564 GOTO(out, rc = -EPROTO);
1566 buf = (char *)rnb + sizeof(*rnb);
1568 rnb->rnb_offset = offset;
1570 mo = dt_locate(env, dt, fid);
1572 GOTO(out_rnb, rc = PTR_ERR(mo));
1573 LASSERT(mo != NULL);
1575 dt_read_lock(env, mo, 0);
1576 if (!dt_object_exists(mo))
1577 GOTO(unlock, rc = -ENOENT);
1579 /* parse remote buffers to local buffers and prepare the latter */
1580 lnbs = (len >> PAGE_SHIFT) + 1;
1581 OBD_ALLOC_PTR_ARRAY(lnb, lnbs);
1583 GOTO(unlock, rc = -ENOMEM);
1585 rc = dt_bufs_get(env, mo, rnb, lnb, lnbs, 0);
1586 if (unlikely(rc < 0))
1588 LASSERT(rc <= lnbs);
1590 rc = dt_read_prep(env, mo, lnb, nr_local);
1593 /* copy data to the buffer finally */
1594 for (i = 0; i < nr_local; i++) {
1595 char *p = kmap(lnb[i].lnb_page);
1598 LASSERT(lnb[i].lnb_page_offset == 0);
1599 off = lnb[i].lnb_len & ~PAGE_MASK;
1601 memset(p + off, 0, PAGE_SIZE - off);
1603 memcpy(buf + (i << PAGE_SHIFT), p, lnb[i].lnb_len);
1604 kunmap(lnb[i].lnb_page);
1605 copied += lnb[i].lnb_len;
1608 CDEBUG(D_INFO, "Read %i (wanted %u) bytes from %llu\n", copied,
1611 CWARN("%s: read %i bytes for "DFID
1612 " but wanted %u, is size wrong?\n",
1613 tsi->tsi_exp->exp_obd->obd_name, copied,
1614 PFID(&tsi->tsi_fid), len);
1615 /* Ignore partially copied data */
1620 dt_bufs_put(env, mo, lnb, nr_local);
1622 OBD_FREE_PTR_ARRAY(lnb, lnbs);
1624 dt_read_unlock(env, mo);
1625 lu_object_put(env, &mo->do_lu);
1627 rnb->rnb_len = copied;
1629 /* Don't fail OPEN request if read-on-open is failed, but drop
1630 * a message in log about the error.
1633 CDEBUG(D_INFO, "Read-on-open is failed, rc = %d", rc);
1639 * Completion AST for DOM discard locks:
1641 * CP AST an DOM discard lock is called always right after enqueue or from
1642 * reprocess if lock was blocked, in the latest case l_ast_data is set to
1643 * the mdt_object which is kept while there are pending locks on it.
1645 int ldlm_dom_discard_cp_ast(struct ldlm_lock *lock, __u64 flags, void *data)
1647 struct mdt_object *mo;
1648 struct lustre_handle dom_lh;
1653 /* l_ast_data is set when lock was not granted immediately
1654 * in mdt_dom_discard_data() below but put into waiting list,
1655 * so this CP callback means we are finished and corresponding
1656 * MDT object should be released finally as well as lock itself.
1658 lock_res_and_lock(lock);
1659 if (!lock->l_ast_data) {
1660 unlock_res_and_lock(lock);
1664 mo = lock->l_ast_data;
1665 lock->l_ast_data = NULL;
1666 unlock_res_and_lock(lock);
1668 ldlm_lock2handle(lock, &dom_lh);
1669 ldlm_lock_decref(&dom_lh, LCK_PW);
1671 env = lu_env_find();
1673 mdt_object_put(env, mo);
1678 void mdt_dom_discard_data(struct mdt_thread_info *info,
1679 struct mdt_object *mo)
1681 struct ptlrpc_request *req = mdt_info_req(info);
1682 struct mdt_device *mdt = mdt_dev(mo->mot_obj.lo_dev);
1683 union ldlm_policy_data policy;
1684 struct ldlm_res_id res_id;
1685 struct lustre_handle dom_lh;
1686 struct ldlm_lock *lock;
1687 __u64 flags = LDLM_FL_AST_DISCARD_DATA;
1693 if (req && req_is_replay(req))
1696 policy.l_inodebits.bits = MDS_INODELOCK_DOM;
1697 policy.l_inodebits.try_bits = 0;
1698 fid_build_reg_res_name(mdt_object_fid(mo), &res_id);
1700 /* Keep blocking version of discard for an old client to avoid
1701 * crashes on non-patched clients. LU-11359.
1703 old_client = req && !(exp_connect_flags2(req->rq_export) &
1704 OBD_CONNECT2_ASYNC_DISCARD);
1706 /* Tell the clients that the object is gone now and that they should
1707 * throw away any cached pages. */
1708 rc = ldlm_cli_enqueue_local(info->mti_env, mdt->mdt_namespace, &res_id,
1709 LDLM_IBITS, &policy, LCK_PW, &flags,
1710 ldlm_blocking_ast, old_client ?
1711 ldlm_completion_ast :
1712 ldlm_dom_discard_cp_ast,
1713 NULL, NULL, 0, LVB_T_NONE, NULL, &dom_lh);
1714 if (rc != ELDLM_OK) {
1716 "Failed to issue discard lock, rc = %d\n", rc);
1720 lock = ldlm_handle2lock(&dom_lh);
1721 lock_res_and_lock(lock);
1722 /* if lock is not granted then there are BL ASTs in progress and
1723 * lock will be granted in result of reprocessing with CP callback
1724 * notifying about that. The mdt object has to be kept until that and
1725 * it is saved in l_ast_data of the lock. Lock reference is kept too
1726 * until that to prevent it from canceling.
1728 if (!is_granted_or_cancelled_nolock(lock)) {
1729 mdt_object_get(info->mti_env, mo);
1730 lock->l_ast_data = mo;
1731 unlock_res_and_lock(lock);
1733 unlock_res_and_lock(lock);
1734 ldlm_lock_decref_and_cancel(&dom_lh, LCK_PW);
1736 LDLM_LOCK_PUT(lock);