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 /* check that we do support OBD_CONNECT_TRUNCLOCK. */
886 BUILD_BUG_ON(!(OST_CONNECT_SUPPORTED & OBD_CONNECT_TRUNCLOCK));
888 if ((oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
889 (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
890 RETURN(err_serious(-EPROTO));
892 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
894 RETURN(err_serious(-ENOMEM));
896 /* punch start,end are passed in o_size,o_blocks throught wire */
900 if (end != OBD_OBJECT_EOF) /* Only truncate is supported */
903 info = tsi2mdt_info(tsi);
904 la = &info->mti_attr.ma_attr;
905 /* standard truncate optimization: if file body is completely
906 * destroyed, don't send data back to the server. */
908 flags |= LDLM_FL_AST_DISCARD_DATA;
910 repbody->oa.o_oi = oa->o_oi;
911 repbody->oa.o_valid = OBD_MD_FLID;
913 srvlock = (exp_connect_flags(exp) & OBD_CONNECT_SRVLOCK) &&
914 oa->o_valid & OBD_MD_FLFLAGS &&
915 oa->o_flags & OBD_FL_SRVLOCK;
918 rc = tgt_mdt_data_lock(ns, &tsi->tsi_resid, &lh, LCK_PW,
924 CDEBUG(D_INODE, "calling punch for object "DFID", valid = %#llx"
925 ", start = %lld, end = %lld\n", PFID(&tsi->tsi_fid),
926 oa->o_valid, start, end);
928 mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
930 GOTO(out_unlock, rc = PTR_ERR(mo));
932 if (!mdt_object_exists(mo))
933 GOTO(out_put, rc = -ENOENT);
935 /* Shouldn't happen on dirs */
936 if (S_ISDIR(lu_object_attr(&mo->mot_obj))) {
938 CERROR("%s: Truncate on dir "DFID": rc = %d\n",
939 exp->exp_obd->obd_name, PFID(&tsi->tsi_fid), rc);
943 mdt_dom_write_lock(mo);
944 dob = mdt_obj2dt(mo);
946 la_from_obdo(la, oa, OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
948 la->la_valid |= LA_SIZE;
950 /* MDT supports FMD for Data-on-MDT needs */
951 if (la->la_valid & (LA_ATIME | LA_MTIME | LA_CTIME))
952 tgt_fmd_update(tsi->tsi_exp, &tsi->tsi_fid,
953 tgt_ses_req(tsi)->rq_xid);
955 rc = mdt_object_punch(tsi->tsi_env, mdt->mdt_bottom, dob,
957 mdt_dom_write_unlock(mo);
961 mdt_dom_obj_lvb_update(tsi->tsi_env, mo, false);
962 mdt_counter_incr(req, LPROC_MDT_IO_PUNCH,
963 ktime_us_delta(ktime_get(), kstart));
966 lu_object_put(tsi->tsi_env, &mo->mot_obj);
969 tgt_data_unlock(&lh, LCK_PW);
971 mdt_thread_info_fini(info);
976 * MDT glimpse for Data-on-MDT
978 * If there is write lock on client then function issues glimpse_ast to get
979 * an actual size from that client.
982 int mdt_do_glimpse(const struct lu_env *env, struct ldlm_namespace *ns,
983 struct ldlm_resource *res)
985 union ldlm_policy_data policy;
986 struct lustre_handle lockh;
988 struct ldlm_lock *lock;
989 struct ldlm_glimpse_work *gl_work;
995 /* There can be only one write lock covering data, try to match it. */
996 policy.l_inodebits.bits = MDS_INODELOCK_DOM;
997 mode = ldlm_lock_match(ns, LDLM_FL_TEST_LOCK,
998 &res->lr_name, LDLM_IBITS, &policy,
1001 /* There is no PW lock on this object; finished. */
1005 lock = ldlm_handle2lock(&lockh);
1010 * This check is for lock taken in mdt_reint_unlink() that does
1011 * not have l_glimpse_ast set. So the logic is: if there is a lock
1012 * with no l_glimpse_ast set, this object is being destroyed already.
1013 * Hence, if you are grabbing DLM locks on the server, always set
1014 * non-NULL glimpse_ast (e.g., ldlm_request.c::ldlm_glimpse_ast()).
1016 if (lock->l_glimpse_ast == NULL) {
1017 LDLM_DEBUG(lock, "no l_glimpse_ast");
1018 GOTO(out, rc = -ENOENT);
1021 OBD_SLAB_ALLOC_PTR_GFP(gl_work, ldlm_glimpse_work_kmem, GFP_ATOMIC);
1023 GOTO(out, rc = -ENOMEM);
1025 /* Populate the gl_work structure.
1026 * Grab additional reference on the lock which will be released in
1027 * ldlm_work_gl_ast_lock() */
1028 gl_work->gl_lock = LDLM_LOCK_GET(lock);
1029 /* The glimpse callback is sent to one single IO lock. As a result,
1030 * the gl_work list is just composed of one element */
1031 list_add_tail(&gl_work->gl_list, &gl_list);
1032 /* There is actually no need for a glimpse descriptor when glimpsing
1034 gl_work->gl_desc = NULL;
1035 /* the ldlm_glimpse_work structure is allocated on the stack */
1036 gl_work->gl_flags = LDLM_GL_WORK_SLAB_ALLOCATED;
1038 ldlm_glimpse_locks(res, &gl_list); /* this will update the LVB */
1040 /* If the list is not empty, we failed to glimpse a lock and
1041 * must clean it up. Usually due to a race with unlink.*/
1042 if (!list_empty(&gl_list)) {
1043 LDLM_LOCK_RELEASE(lock);
1044 OBD_SLAB_FREE_PTR(gl_work, ldlm_glimpse_work_kmem);
1049 LDLM_LOCK_PUT(lock);
1053 static void mdt_lvb2reply(struct ldlm_resource *res, struct mdt_body *mb,
1054 struct ost_lvb *lvb)
1056 struct ost_lvb *res_lvb;
1059 res_lvb = res->lr_lvb_data;
1064 mb->mbo_dom_size = res_lvb->lvb_size;
1065 mb->mbo_dom_blocks = res_lvb->lvb_blocks;
1066 mb->mbo_mtime = res_lvb->lvb_mtime;
1067 mb->mbo_ctime = res_lvb->lvb_ctime;
1068 mb->mbo_atime = res_lvb->lvb_atime;
1069 mb->mbo_valid |= OBD_MD_FLATIME | OBD_MD_FLCTIME |
1070 OBD_MD_FLMTIME | OBD_MD_DOM_SIZE;
1072 CDEBUG(D_DLMTRACE, "size %llu\n", res_lvb->lvb_size);
1077 * MDT glimpse for Data-on-MDT
1079 * This function is called when MDT get attributes for the DoM object.
1080 * If there is write lock on client then function issues glimpse_ast to get
1081 * an actual size from that client.
1083 int mdt_dom_object_size(const struct lu_env *env, struct mdt_device *mdt,
1084 const struct lu_fid *fid, struct mdt_body *mb,
1087 struct ldlm_res_id resid;
1088 struct ldlm_resource *res;
1093 fid_build_reg_res_name(fid, &resid);
1094 res = ldlm_resource_get(mdt->mdt_namespace, NULL, &resid,
1099 /* Update lvbo data if DoM lock returned or if LVB is not yet valid. */
1100 if (dom_lock || !mdt_dom_lvb_is_valid(res))
1101 mdt_dom_lvbo_update(res, NULL, NULL, false);
1103 mdt_lvb2reply(res, mb, NULL);
1104 ldlm_resource_putref(res);
1109 * MDT DoM lock intent policy (glimpse)
1111 * Intent policy is called when lock has an intent, for DoM file that
1112 * means glimpse lock and policy fills Lock Value Block (LVB).
1114 * If already granted lock is found it will be placed in \a lockp and
1115 * returned back to caller function.
1117 * \param[in] tsi session info
1118 * \param[in,out] lockp pointer to the lock
1119 * \param[in] flags LDLM flags
1121 * \retval ELDLM_LOCK_REPLACED if already granted lock was found
1122 * and placed in \a lockp
1123 * \retval ELDLM_LOCK_ABORTED in other cases except error
1124 * \retval negative value on error
1126 int mdt_glimpse_enqueue(struct mdt_thread_info *mti, struct ldlm_namespace *ns,
1127 struct ldlm_lock **lockp, __u64 flags)
1129 struct ldlm_lock *lock = *lockp;
1130 struct ldlm_resource *res = lock->l_resource;
1131 ldlm_processing_policy policy;
1132 struct ldlm_reply *rep;
1133 struct mdt_body *mbo;
1134 struct ost_lvb *lvb;
1135 bool old_client = !exp_connect_dom_lvb(mti->mti_exp);
1140 policy = ldlm_get_processing_policy(res);
1141 LASSERT(policy != NULL);
1143 if (unlikely(old_client)) {
1144 req_capsule_set_size(mti->mti_pill, &RMF_MDT_MD, RCL_SERVER, 0);
1145 req_capsule_set_size(mti->mti_pill, &RMF_ACL, RCL_SERVER, 0);
1147 req_capsule_set_size(mti->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
1150 rc = req_capsule_server_pack(mti->mti_pill);
1152 RETURN(err_serious(rc));
1154 rep = req_capsule_server_get(mti->mti_pill, &RMF_DLM_REP);
1156 if (unlikely(old_client)) {
1157 mbo = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1161 lvb = req_capsule_server_get(mti->mti_pill, &RMF_DLM_LVB);
1167 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
1168 * lock was found by ldlm_handle_enqueue(); if so no need to grant
1170 if (flags & LDLM_FL_RESENT) {
1171 rc = LDLM_ITER_CONTINUE;
1173 __u64 tmpflags = LDLM_FL_BLOCK_NOWAIT;
1174 enum ldlm_error err;
1176 rc = policy(lock, &tmpflags, LDLM_PROCESS_RESCAN, &err, NULL);
1177 check_res_locked(res);
1181 /* The lock met with no resistance; we're finished. */
1182 if (rc == LDLM_ITER_CONTINUE) {
1183 GOTO(fill_mbo, rc = ELDLM_LOCK_REPLACED);
1184 } else if (flags & LDLM_FL_BLOCK_NOWAIT) {
1185 /* LDLM_FL_BLOCK_NOWAIT means it is for AGL. Do not send glimpse
1186 * callback for glimpse size. The real size user will trigger
1187 * the glimpse callback when necessary. */
1188 GOTO(fill_mbo, rc = ELDLM_LOCK_ABORTED);
1191 rc = mdt_do_glimpse(mti->mti_env, ns, res);
1192 if (rc == -ENOENT) {
1193 /* We are racing with unlink(); just return -ENOENT */
1194 rep->lock_policy_res2 = ptlrpc_status_hton(-ENOENT);
1195 } else if (rc == -EINVAL) {
1196 /* this is possible is client lock has been cancelled but
1197 * still exists on server. If that lock was found on server
1198 * as only conflicting lock then the client has already
1199 * size authority and glimpse is not needed. */
1200 CDEBUG(D_DLMTRACE, "Glimpse from the client owning lock\n");
1201 } else if (rc < 0) {
1204 rc = ELDLM_LOCK_ABORTED;
1206 /* LVB can be without valid data in case of DOM */
1207 if (!mdt_dom_lvb_is_valid(res))
1208 mdt_dom_lvbo_update(res, lock, NULL, false);
1209 mdt_lvb2reply(res, mbo, lvb);
1214 int mdt_brw_enqueue(struct mdt_thread_info *mti, struct ldlm_namespace *ns,
1215 struct ldlm_lock **lockp, __u64 flags)
1217 struct tgt_session_info *tsi = tgt_ses_info(mti->mti_env);
1218 struct lu_fid *fid = &tsi->tsi_fid;
1219 struct ldlm_lock *lock = *lockp;
1220 struct ldlm_resource *res = lock->l_resource;
1221 struct ldlm_reply *rep;
1222 struct mdt_body *mbo;
1223 struct mdt_lock_handle *lhc = &mti->mti_lh[MDT_LH_RMT];
1224 struct mdt_object *mo;
1229 req_capsule_set_size(mti->mti_pill, &RMF_MDT_MD, RCL_SERVER, 0);
1230 req_capsule_set_size(mti->mti_pill, &RMF_ACL, RCL_SERVER, 0);
1231 rc = req_capsule_server_pack(mti->mti_pill);
1233 RETURN(err_serious(rc));
1235 rep = req_capsule_server_get(mti->mti_pill, &RMF_DLM_REP);
1239 mbo = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1243 fid_extract_from_res_name(fid, &res->lr_name);
1244 mo = mdt_object_find(mti->mti_env, mti->mti_mdt, fid);
1245 if (unlikely(IS_ERR(mo)))
1246 RETURN(PTR_ERR(mo));
1248 if (!mdt_object_exists(mo))
1249 GOTO(out, rc = -ENOENT);
1251 if (mdt_object_remote(mo))
1252 GOTO(out, rc = -EPROTO);
1254 /* Get lock from request for possible resent case. */
1255 mdt_intent_fixup_resent(mti, *lockp, lhc, flags);
1257 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1258 mdt_lock_handle_init(lhc);
1259 mdt_lh_reg_init(lhc, *lockp);
1261 /* This will block MDT thread but it should be fine until
1262 * client caches small amount of data for DoM, which should be
1263 * smaller than one BRW RPC and should be able to be
1264 * piggybacked by lock cancel RPC.
1265 * If the client could hold the lock too long, this code can be
1266 * revised to call mdt_object_lock_try(). And if fails, it will
1267 * return ELDLM_OK here and fall back into normal lock enqueue
1270 rc = mdt_object_lock(mti, mo, lhc, MDS_INODELOCK_DOM);
1275 if (!mdt_dom_lvb_is_valid(res)) {
1276 rc = mdt_dom_lvb_alloc(res);
1279 mdt_dom_disk_lvbo_update(mti->mti_env, mo, res, false);
1281 mdt_lvb2reply(res, mbo, NULL);
1283 rep->lock_policy_res2 = clear_serious(rc);
1284 if (rep->lock_policy_res2) {
1285 lhc->mlh_reg_lh.cookie = 0ull;
1286 GOTO(out, rc = ELDLM_LOCK_ABORTED);
1289 rc = mdt_intent_lock_replace(mti, lockp, lhc, flags, rc);
1292 lhc->mlh_reg_lh.cookie = 0ull;
1293 mdt_object_put(mti->mti_env, mo);
1297 /* check if client has already DoM lock for given resource */
1298 bool mdt_dom_client_has_lock(struct mdt_thread_info *info,
1299 const struct lu_fid *fid)
1301 struct mdt_device *mdt = info->mti_mdt;
1302 union ldlm_policy_data *policy = &info->mti_policy;
1303 struct ldlm_res_id *res_id = &info->mti_res_id;
1304 __u64 open_flags = info->mti_spec.sp_cr_flags;
1305 struct lustre_handle lockh;
1306 enum ldlm_mode mode;
1307 struct ldlm_lock *lock;
1311 policy->l_inodebits.bits = MDS_INODELOCK_DOM;
1312 fid_build_reg_res_name(fid, res_id);
1315 lm = (open_flags & MDS_FMODE_WRITE) ? LCK_PW : LCK_PR | LCK_PW;
1316 mode = ldlm_lock_match(mdt->mdt_namespace, LDLM_FL_BLOCK_GRANTED |
1317 LDLM_FL_TEST_LOCK, res_id, LDLM_IBITS, policy,
1320 /* There is no other PW lock on this object; finished. */
1324 lock = ldlm_handle2lock(&lockh);
1328 /* check if lock from the same client */
1329 rc = (lock->l_export->exp_handle.h_cookie ==
1330 info->mti_exp->exp_handle.h_cookie);
1331 LDLM_LOCK_PUT(lock);
1336 * MDT request handler for OST_GETATTR RPC.
1338 * This is data-specific request to get object and layout versions under
1339 * IO lock. It is reliable only for Data-on-MDT files.
1341 * \param[in] tsi target session environment for this request
1343 * \retval 0 if successful
1344 * \retval negative value on error
1346 int mdt_data_version_get(struct tgt_session_info *tsi)
1348 struct mdt_thread_info *mti = mdt_th_info(tsi->tsi_env);
1349 struct mdt_device *mdt = mti->mti_mdt;
1350 struct mdt_body *repbody;
1351 struct mdt_object *mo = mti->mti_object;
1352 struct lov_comp_md_v1 *comp;
1353 struct lustre_handle lh = { 0 };
1356 enum ldlm_mode lock_mode = LCK_PR;
1362 req_capsule_set_size(tsi->tsi_pill, &RMF_MDT_MD, RCL_SERVER, 0);
1363 req_capsule_set_size(tsi->tsi_pill, &RMF_ACL, RCL_SERVER, 0);
1364 rc = req_capsule_server_pack(tsi->tsi_pill);
1365 if (unlikely(rc != 0))
1366 RETURN(err_serious(rc));
1368 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1369 if (repbody == NULL)
1372 srvlock = tsi->tsi_mdt_body->mbo_valid & OBD_MD_FLFLAGS &&
1373 tsi->tsi_mdt_body->mbo_flags & OBD_FL_SRVLOCK;
1376 if (unlikely(tsi->tsi_mdt_body->mbo_flags & OBD_FL_FLUSH))
1379 fid_build_reg_res_name(&tsi->tsi_fid, &tsi->tsi_resid);
1380 rc = tgt_mdt_data_lock(mdt->mdt_namespace, &tsi->tsi_resid,
1381 &lh, lock_mode, &flags);
1386 if (!mdt_object_exists(mo))
1387 GOTO(out, rc = -ENOENT);
1388 if (mdt_object_remote(mo))
1389 GOTO(out, rc = -EREMOTE);
1390 if (!S_ISREG(lu_object_attr(&mo->mot_obj)))
1391 GOTO(out, rc = -EBADF);
1393 /* Get version first */
1394 version = dt_version_get(tsi->tsi_env, mdt_obj2dt(mo));
1395 if (version && version != -EOPNOTSUPP) {
1396 repbody->mbo_valid |= OBD_MD_FLDATAVERSION;
1397 /* re-use mbo_ioepoch to transfer version */
1398 repbody->mbo_version = version;
1401 /* Read layout to get its version */
1402 rc = mdt_big_xattr_get(mti, mo, XATTR_NAME_LOV);
1403 if (rc == -ENODATA) /* File has no layout yet */
1408 comp = mti->mti_buf.lb_buf;
1409 if (le32_to_cpu(comp->lcm_magic) != LOV_MAGIC_COMP_V1) {
1410 CDEBUG(D_INFO, DFID" has no composite layout",
1411 PFID(&tsi->tsi_fid));
1412 GOTO(out, rc = -ESTALE);
1415 CDEBUG(D_INODE, DFID": layout version: %u\n",
1416 PFID(&tsi->tsi_fid), le32_to_cpu(comp->lcm_layout_gen));
1418 repbody->mbo_valid |= OBD_MD_LAYOUT_VERSION;
1419 /* re-use mbo_rdev for that */
1420 repbody->mbo_layout_gen = le32_to_cpu(comp->lcm_layout_gen);
1424 tgt_data_unlock(&lh, lock_mode);
1426 repbody->mbo_valid |= OBD_MD_FLFLAGS;
1427 repbody->mbo_flags = OBD_FL_FLUSH;
1431 /* read file data to the buffer */
1432 int mdt_dom_read_on_open(struct mdt_thread_info *mti, struct mdt_device *mdt,
1433 struct lustre_handle *lh)
1435 const struct lu_env *env = mti->mti_env;
1436 struct tgt_session_info *tsi = tgt_ses_info(env);
1437 struct req_capsule *pill = tsi->tsi_pill;
1438 const struct lu_fid *fid;
1439 struct ptlrpc_request *req = tgt_ses_req(tsi);
1440 struct mdt_body *mbo;
1441 struct dt_device *dt = mdt->mdt_bottom;
1442 struct dt_object *mo;
1444 struct niobuf_remote *rnb = NULL;
1445 struct niobuf_local *lnb;
1448 unsigned int len, copied = 0;
1449 __u64 real_dom_size;
1450 int lnbs, nr_local, i;
1451 bool dom_lock = false;
1455 if (!req_capsule_field_present(pill, &RMF_NIOBUF_INLINE, RCL_SERVER)) {
1456 /* There is no reply buffers for this field, this means that
1457 * client has no support for data in reply.
1462 mbo = req_capsule_server_get(pill, &RMF_MDT_BODY);
1463 if (!(mbo->mbo_valid & OBD_MD_DOM_SIZE))
1466 if (!mbo->mbo_dom_size)
1469 if (lustre_handle_is_used(lh)) {
1470 struct ldlm_lock *lock;
1472 lock = ldlm_handle2lock(lh);
1474 dom_lock = ldlm_has_dom(lock) && ldlm_has_layout(lock);
1475 LDLM_LOCK_PUT(lock);
1479 /* return data along with open only along with DoM lock */
1480 if (!dom_lock || !mdt->mdt_opts.mo_dom_read_open)
1483 /* if DoM object holds encrypted content, we need to make sure we
1484 * send whole encryption units, or client will read corrupted content
1486 if (mbo->mbo_valid & LA_FLAGS && mbo->mbo_flags & LUSTRE_ENCRYPT_FL &&
1487 mbo->mbo_dom_size & ~LUSTRE_ENCRYPTION_MASK)
1488 real_dom_size = (mbo->mbo_dom_size & LUSTRE_ENCRYPTION_MASK) +
1489 LUSTRE_ENCRYPTION_UNIT_SIZE;
1491 real_dom_size = mbo->mbo_dom_size;
1493 CDEBUG(D_INFO, "File size %llu, reply sizes %d/%d\n",
1494 real_dom_size, req->rq_reqmsg->lm_repsize, req->rq_replen);
1495 len = req->rq_reqmsg->lm_repsize - req->rq_replen;
1497 /* NB: at this moment we have the following sizes:
1498 * - req->rq_replen: used data in reply
1499 * - req->rq_reqmsg->lm_repsize: total allocated reply buffer at client
1501 * Ideal case when file size fits in allocated reply buffer,
1502 * that mean we can return whole data in reply. We can also fit more
1503 * data up to max_reply_size in total reply size, but this will cause
1504 * re-allocation on client and resend with larger buffer. This is still
1505 * faster than separate READ IO.
1506 * Third case if file is too big to fit even in maximum size, in that
1507 * case we return just tail to optimize possible append.
1509 * At the moment the following strategy is used:
1510 * 1) try to fit into the buffer we have
1511 * 2) return just file tail otherwise.
1513 if (real_dom_size <= len) {
1514 /* can fit whole data */
1515 len = real_dom_size;
1517 } else if (real_dom_size <
1518 mdt_lmm_dom_stripesize(mti->mti_attr.ma_lmm)) {
1521 /* File tail offset must be aligned with larger page size
1522 * between client and server, so the maximum page size is
1523 * used here to align offset.
1525 * NB: DOM feature was introduced when server supports pagebits
1526 * already, so it should be always non-zero value. Report error
1527 * if it is not for some reason.
1529 if (!req->rq_export->exp_target_data.ted_pagebits) {
1530 CERROR("%s: client page bits are not saved on server\n",
1534 pgbits = max_t(int, PAGE_SHIFT,
1535 req->rq_export->exp_target_data.ted_pagebits);
1536 tail = real_dom_size % (1 << pgbits);
1538 /* no partial tail or tail can't fit in reply */
1539 if (tail == 0 || len < tail)
1543 offset = real_dom_size - len;
1545 /* DOM stripe is fully written, so don't expect its tail
1546 * will be used by append.
1551 LASSERT((offset & ~PAGE_MASK) == 0);
1552 rc = req_capsule_server_grow(pill, &RMF_NIOBUF_INLINE,
1553 sizeof(*rnb) + len);
1555 /* failed to grow data buffer, just exit */
1556 GOTO(out, rc = -E2BIG);
1559 /* re-take MDT_BODY and NIOBUF_INLINE buffers after the buffer grow */
1560 mbo = req_capsule_server_get(pill, &RMF_MDT_BODY);
1561 fid = &mbo->mbo_fid1;
1562 if (!fid_is_sane(fid))
1563 GOTO(out, rc = -EINVAL);
1565 rnb = req_capsule_server_get(tsi->tsi_pill, &RMF_NIOBUF_INLINE);
1567 GOTO(out, rc = -EPROTO);
1569 buf = (char *)rnb + sizeof(*rnb);
1571 rnb->rnb_offset = offset;
1573 mo = dt_locate(env, dt, fid);
1575 GOTO(out_rnb, rc = PTR_ERR(mo));
1576 LASSERT(mo != NULL);
1578 dt_read_lock(env, mo, 0);
1579 if (!dt_object_exists(mo))
1580 GOTO(unlock, rc = -ENOENT);
1582 /* parse remote buffers to local buffers and prepare the latter */
1583 lnbs = (len >> PAGE_SHIFT) + 1;
1584 OBD_ALLOC_PTR_ARRAY(lnb, lnbs);
1586 GOTO(unlock, rc = -ENOMEM);
1588 rc = dt_bufs_get(env, mo, rnb, lnb, lnbs, 0);
1589 if (unlikely(rc < 0))
1591 LASSERT(rc <= lnbs);
1593 rc = dt_read_prep(env, mo, lnb, nr_local);
1596 /* copy data to the buffer finally */
1597 for (i = 0; i < nr_local; i++) {
1598 char *p = kmap(lnb[i].lnb_page);
1601 LASSERT(lnb[i].lnb_page_offset == 0);
1602 off = lnb[i].lnb_len & ~PAGE_MASK;
1604 memset(p + off, 0, PAGE_SIZE - off);
1606 memcpy(buf + (i << PAGE_SHIFT), p, lnb[i].lnb_len);
1607 kunmap(lnb[i].lnb_page);
1608 copied += lnb[i].lnb_len;
1611 CDEBUG(D_INFO, "Read %i (wanted %u) bytes from %llu\n", copied,
1614 CWARN("%s: read %i bytes for "DFID
1615 " but wanted %u, is size wrong?\n",
1616 tsi->tsi_exp->exp_obd->obd_name, copied,
1617 PFID(&tsi->tsi_fid), len);
1618 /* Ignore partially copied data */
1623 dt_bufs_put(env, mo, lnb, nr_local);
1625 OBD_FREE_PTR_ARRAY(lnb, lnbs);
1627 dt_read_unlock(env, mo);
1628 lu_object_put(env, &mo->do_lu);
1630 rnb->rnb_len = copied;
1632 /* Don't fail OPEN request if read-on-open is failed, but drop
1633 * a message in log about the error.
1636 CDEBUG(D_INFO, "Read-on-open is failed, rc = %d", rc);
1642 * Completion AST for DOM discard locks:
1644 * CP AST an DOM discard lock is called always right after enqueue or from
1645 * reprocess if lock was blocked, in the latest case l_ast_data is set to
1646 * the mdt_object which is kept while there are pending locks on it.
1648 int ldlm_dom_discard_cp_ast(struct ldlm_lock *lock, __u64 flags, void *data)
1650 struct mdt_object *mo;
1651 struct lustre_handle dom_lh;
1656 /* l_ast_data is set when lock was not granted immediately
1657 * in mdt_dom_discard_data() below but put into waiting list,
1658 * so this CP callback means we are finished and corresponding
1659 * MDT object should be released finally as well as lock itself.
1661 lock_res_and_lock(lock);
1662 if (!lock->l_ast_data) {
1663 unlock_res_and_lock(lock);
1667 mo = lock->l_ast_data;
1668 lock->l_ast_data = NULL;
1669 unlock_res_and_lock(lock);
1671 ldlm_lock2handle(lock, &dom_lh);
1672 ldlm_lock_decref(&dom_lh, LCK_PW);
1674 env = lu_env_find();
1676 mdt_object_put(env, mo);
1681 void mdt_dom_discard_data(struct mdt_thread_info *info,
1682 struct mdt_object *mo)
1684 struct ptlrpc_request *req = mdt_info_req(info);
1685 struct mdt_device *mdt = mdt_dev(mo->mot_obj.lo_dev);
1686 union ldlm_policy_data policy;
1687 struct ldlm_res_id res_id;
1688 struct lustre_handle dom_lh;
1689 struct ldlm_lock *lock;
1690 __u64 flags = LDLM_FL_AST_DISCARD_DATA;
1696 if (req && req_is_replay(req))
1699 policy.l_inodebits.bits = MDS_INODELOCK_DOM;
1700 policy.l_inodebits.try_bits = 0;
1701 fid_build_reg_res_name(mdt_object_fid(mo), &res_id);
1703 /* Keep blocking version of discard for an old client to avoid
1704 * crashes on non-patched clients. LU-11359.
1706 old_client = req && !(exp_connect_flags2(req->rq_export) &
1707 OBD_CONNECT2_ASYNC_DISCARD);
1709 /* Tell the clients that the object is gone now and that they should
1710 * throw away any cached pages. */
1711 rc = ldlm_cli_enqueue_local(info->mti_env, mdt->mdt_namespace, &res_id,
1712 LDLM_IBITS, &policy, LCK_PW, &flags,
1713 ldlm_blocking_ast, old_client ?
1714 ldlm_completion_ast :
1715 ldlm_dom_discard_cp_ast,
1716 NULL, NULL, 0, LVB_T_NONE, NULL, &dom_lh);
1717 if (rc != ELDLM_OK) {
1719 "Failed to issue discard lock, rc = %d\n", rc);
1723 lock = ldlm_handle2lock(&dom_lh);
1724 lock_res_and_lock(lock);
1725 /* if lock is not granted then there are BL ASTs in progress and
1726 * lock will be granted in result of reprocessing with CP callback
1727 * notifying about that. The mdt object has to be kept until that and
1728 * it is saved in l_ast_data of the lock. Lock reference is kept too
1729 * until that to prevent it from canceling.
1731 if (!is_granted_or_cancelled_nolock(lock)) {
1732 mdt_object_get(info->mti_env, mo);
1733 lock->l_ast_data = mo;
1734 unlock_res_and_lock(lock);
1736 unlock_res_and_lock(lock);
1737 ldlm_lock_decref_and_cancel(&dom_lh, LCK_PW);
1739 LDLM_LOCK_PUT(lock);