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 <linux/falloc.h>
35 #include <lustre_nodemap.h>
37 #include "mdt_internal.h"
39 /* functions below are stubs for now, they will be implemented with
40 * grant support on MDT */
41 static inline void mdt_dom_read_lock(struct mdt_object *mo)
43 down_read(&mo->mot_dom_sem);
46 static inline void mdt_dom_read_unlock(struct mdt_object *mo)
48 up_read(&mo->mot_dom_sem);
51 static inline void mdt_dom_write_lock(struct mdt_object *mo)
53 down_write(&mo->mot_dom_sem);
56 static inline void mdt_dom_write_unlock(struct mdt_object *mo)
58 up_write(&mo->mot_dom_sem);
61 static void mdt_dom_resource_prolong(struct ldlm_prolong_args *arg)
63 struct ldlm_resource *res;
64 struct ldlm_lock *lock;
68 res = ldlm_resource_get(arg->lpa_export->exp_obd->obd_namespace,
69 &arg->lpa_resid, LDLM_IBITS, 0);
72 "Failed to get resource for resid %llu/%llu\n",
73 arg->lpa_resid.name[0], arg->lpa_resid.name[1]);
78 list_for_each_entry(lock, &res->lr_granted, l_res_link) {
79 if (ldlm_has_dom(lock)) {
80 LDLM_DEBUG(lock, "DOM lock to prolong ");
81 ldlm_lock_prolong_one(lock, arg);
82 /* only one PW or EX lock can be granted,
83 * no need to continue search
85 if (lock->l_granted_mode & (LCK_PW | LCK_EX))
90 ldlm_resource_putref(res);
95 static void mdt_prolong_dom_lock(struct tgt_session_info *tsi,
96 struct ldlm_prolong_args *data)
98 struct obdo *oa = &tsi->tsi_ost_body->oa;
99 struct ldlm_lock *lock;
103 data->lpa_req = tgt_ses_req(tsi);
104 data->lpa_export = tsi->tsi_exp;
105 data->lpa_resid = tsi->tsi_resid;
107 CDEBUG(D_RPCTRACE, "Prolong DOM lock for req %p with x%llu\n",
108 tgt_ses_req(tsi), tgt_ses_req(tsi)->rq_xid);
110 if (oa->o_valid & OBD_MD_FLHANDLE) {
111 /* mostly a request should be covered by only one lock, try
113 lock = ldlm_handle2lock(&oa->o_handle);
115 LASSERT(lock->l_export == data->lpa_export);
116 ldlm_lock_prolong_one(lock, data);
117 lock->l_last_used = ktime_get();
119 if (data->lpa_locks_cnt > 0)
123 mdt_dom_resource_prolong(data);
127 static int mdt_rw_hpreq_lock_match(struct ptlrpc_request *req,
128 struct ldlm_lock *lock)
130 struct obd_ioobj *ioo;
132 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
136 if (!(lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_DOM))
139 ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
140 LASSERT(ioo != NULL);
142 LASSERT(lock->l_resource != NULL);
143 if (!fid_res_name_eq(&ioo->ioo_oid.oi_fid, &lock->l_resource->lr_name))
146 /* a bulk write can only hold a reference on a PW extent lock. */
147 mode = LCK_PW | LCK_GROUP;
149 /* whereas a bulk read can be protected by either a PR or PW
153 if (!(lock->l_granted_mode & mode))
159 static int mdt_rw_hpreq_check(struct ptlrpc_request *req)
161 struct tgt_session_info *tsi;
162 struct obd_ioobj *ioo;
163 struct niobuf_remote *rnb;
165 struct ldlm_prolong_args pa = { 0 };
169 /* Don't use tgt_ses_info() to get session info, because lock_match()
170 * can be called while request has no processing thread yet. */
171 tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
174 * Use LASSERT below because malformed RPCs should have
175 * been filtered out in tgt_hpreq_handler().
177 opc = lustre_msg_get_opc(req->rq_reqmsg);
178 LASSERT(opc == OST_READ || opc == OST_WRITE);
180 ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
181 LASSERT(ioo != NULL);
183 rnb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
184 LASSERT(rnb != NULL);
185 LASSERT(!(rnb->rnb_flags & OBD_BRW_SRVLOCK));
187 pa.lpa_mode = LCK_PW | LCK_GROUP;
189 pa.lpa_mode |= LCK_PR;
191 DEBUG_REQ(D_RPCTRACE, req, "%s %s: refresh rw locks for " DFID,
192 tgt_name(tsi->tsi_tgt), current->comm, PFID(&tsi->tsi_fid));
194 mdt_prolong_dom_lock(tsi, &pa);
196 if (pa.lpa_blocks_cnt > 0) {
198 "%s: refreshed %u locks timeout for req %p\n",
199 tgt_name(tsi->tsi_tgt), pa.lpa_blocks_cnt, req);
203 RETURN(pa.lpa_locks_cnt > 0 ? 0 : -ESTALE);
206 static void mdt_rw_hpreq_fini(struct ptlrpc_request *req)
208 mdt_rw_hpreq_check(req);
211 static struct ptlrpc_hpreq_ops mdt_hpreq_rw = {
212 .hpreq_lock_match = mdt_rw_hpreq_lock_match,
213 .hpreq_check = mdt_rw_hpreq_check,
214 .hpreq_fini = mdt_rw_hpreq_fini
218 * Assign high priority operations to an IO request.
220 * Check if the incoming request is a candidate for
221 * high-priority processing. If it is, assign it a high
222 * priority operations table.
224 * \param[in] tsi target session environment for this request
226 void mdt_hp_brw(struct tgt_session_info *tsi)
228 struct niobuf_remote *rnb;
229 struct obd_ioobj *ioo;
233 ioo = req_capsule_client_get(tsi->tsi_pill, &RMF_OBD_IOOBJ);
234 LASSERT(ioo != NULL); /* must exist after request preprocessing */
235 if (ioo->ioo_bufcnt > 0) {
236 rnb = req_capsule_client_get(tsi->tsi_pill, &RMF_NIOBUF_REMOTE);
237 LASSERT(rnb != NULL); /* must exist after preprocessing */
239 /* no high priority if server lock is needed */
240 if (rnb->rnb_flags & OBD_BRW_SRVLOCK ||
241 (lustre_msg_get_flags(tgt_ses_req(tsi)->rq_reqmsg) &
245 tgt_ses_req(tsi)->rq_ops = &mdt_hpreq_rw;
248 static int mdt_punch_hpreq_lock_match(struct ptlrpc_request *req,
249 struct ldlm_lock *lock)
251 struct tgt_session_info *tsi;
256 /* Don't use tgt_ses_info() to get session info, because lock_match()
257 * can be called while request has no processing thread yet. */
258 tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
261 * Use LASSERT below because malformed RPCs should have
262 * been filtered out in tgt_hpreq_handler().
264 LASSERT(tsi->tsi_ost_body != NULL);
265 if (tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLHANDLE &&
266 tsi->tsi_ost_body->oa.o_handle.cookie == lock->l_handle.h_cookie)
269 oa = &tsi->tsi_ost_body->oa;
271 LASSERT(lock->l_resource != NULL);
272 if (!fid_res_name_eq(&oa->o_oi.oi_fid, &lock->l_resource->lr_name))
275 if (!(lock->l_granted_mode & (LCK_PW | LCK_GROUP)))
282 * Implementation of ptlrpc_hpreq_ops::hpreq_lock_check for OST_PUNCH request.
284 * High-priority queue request check for whether the given punch request
285 * (\a req) is blocking an LDLM lock cancel. Also checks whether the request is
286 * covered by an LDLM lock.
290 * \param[in] req the incoming request
292 * \retval 1 if \a req is blocking an LDLM lock cancel
293 * \retval 0 if it is not
294 * \retval -ESTALE if lock is not found
296 static int mdt_punch_hpreq_check(struct ptlrpc_request *req)
298 struct tgt_session_info *tsi;
300 struct ldlm_prolong_args pa = { 0 };
304 /* Don't use tgt_ses_info() to get session info, because lock_match()
305 * can be called while request has no processing thread yet. */
306 tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
307 LASSERT(tsi != NULL);
308 oa = &tsi->tsi_ost_body->oa;
310 LASSERT(!(oa->o_valid & OBD_MD_FLFLAGS &&
311 oa->o_flags & OBD_FL_SRVLOCK));
313 pa.lpa_mode = LCK_PW | LCK_GROUP;
315 CDEBUG(D_DLMTRACE, "%s: refresh DOM lock for "DFID"\n",
316 tgt_name(tsi->tsi_tgt), PFID(&tsi->tsi_fid));
318 mdt_prolong_dom_lock(tsi, &pa);
320 if (pa.lpa_blocks_cnt > 0) {
322 "%s: refreshed %u locks timeout for req %p.\n",
323 tgt_name(tsi->tsi_tgt), pa.lpa_blocks_cnt, req);
327 RETURN(pa.lpa_locks_cnt > 0 ? 0 : -ESTALE);
331 * Implementation of ptlrpc_hpreq_ops::hpreq_lock_fini for OST_PUNCH request.
333 * Called after the request has been handled. It refreshes lock timeout again
334 * so that client has more time to send lock cancel RPC.
336 * \param[in] req request which is being processed.
338 static void mdt_punch_hpreq_fini(struct ptlrpc_request *req)
340 mdt_punch_hpreq_check(req);
343 static struct ptlrpc_hpreq_ops mdt_hpreq_punch = {
344 .hpreq_lock_match = mdt_punch_hpreq_lock_match,
345 .hpreq_check = mdt_punch_hpreq_check,
346 .hpreq_fini = mdt_punch_hpreq_fini
349 void mdt_hp_punch(struct tgt_session_info *tsi)
351 LASSERT(tsi->tsi_ost_body != NULL); /* must exists if we are here */
352 /* no high-priority if server lock is needed */
353 if ((tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLFLAGS &&
354 tsi->tsi_ost_body->oa.o_flags & OBD_FL_SRVLOCK) ||
355 tgt_conn_flags(tsi) & OBD_CONNECT_MDS ||
356 lustre_msg_get_flags(tgt_ses_req(tsi)->rq_reqmsg) & MSG_REPLAY)
358 tgt_ses_req(tsi)->rq_ops = &mdt_hpreq_punch;
361 static int mdt_preprw_read(const struct lu_env *env, struct obd_export *exp,
362 struct mdt_device *mdt, struct mdt_object *mo,
363 struct lu_attr *la, int niocount,
364 struct niobuf_remote *rnb, int *nr_local,
365 struct niobuf_local *lnb)
367 struct dt_object *dob;
368 int i, j, rc, tot_bytes = 0;
369 int maxlnb = *nr_local;
374 mdt_dom_read_lock(mo);
376 /* the only valid case when READ can find object is missing or stale
377 * when export is just evicted and open files are closed forcefully
378 * on server while client's READ can be in progress.
379 * This should not happen on healthy export, object can't be missing
380 * or dying because both states means it was finally destroyed.
382 level = exp->exp_failed ? D_INFO : D_ERROR;
383 if (!mdt_object_exists(mo)) {
385 "%s: READ IO to missing obj "DFID": rc = %d\n",
386 exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)),
388 /* return 0 and continue with empty commit to skip such READ
389 * without more BRW errors.
393 if (lu_object_is_dying(&mo->mot_header)) {
395 "%s: READ IO to stale obj "DFID": rc = %d\n",
396 exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)),
398 /* return 0 and continue with empty commit to skip such READ
399 * without more BRW errors.
404 dob = mdt_obj2dt(mo);
405 /* parse remote buffers to local buffers and prepare the latter */
406 for (i = 0, j = 0; i < niocount; i++) {
407 rc = dt_bufs_get(env, dob, rnb + i, lnb + j, maxlnb, 0);
408 if (unlikely(rc < 0))
410 /* correct index for local buffers to continue with */
414 tot_bytes += rnb[i].rnb_len;
417 rc = dt_attr_get(env, dob, la);
421 rc = dt_read_prep(env, dob, lnb, *nr_local);
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)
439 struct dt_object *dob;
440 int i, j, k, rc = 0, tot_bytes = 0;
441 int maxlnb = *nr_local;
445 /* Process incoming grant info, set OBD_BRW_GRANTED flag and grant some
446 * space back if possible */
447 tgt_grant_prepare_write(env, exp, oa, rnb, obj->ioo_bufcnt);
449 mdt_dom_read_lock(mo);
451 /* don't report error in cases with failed export */
452 if (!mdt_object_exists(mo)) {
453 int level = exp->exp_failed ? D_INFO : D_ERROR;
457 "%s: WRITE IO to missing obj "DFID": rc = %d\n",
458 exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)),
460 /* exit with no data written, note nr_local = 0 above */
463 if (lu_object_is_dying(&mo->mot_header)) {
464 /* This is possible race between object destroy followed by
465 * discard BL AST and client cache flushing. Object is
466 * referenced until discard finish.
468 CDEBUG(D_INODE, "WRITE IO to stale object "DFID"\n",
469 PFID(mdt_object_fid(mo)));
470 /* Note: continue with no error here to don't cause BRW errors
471 * but skip transaction in commitrw silently so no data is
476 dob = mdt_obj2dt(mo);
477 /* parse remote buffers to local buffers and prepare the latter */
478 for (i = 0, j = 0; i < obj->ioo_bufcnt; i++) {
479 rc = dt_bufs_get(env, dob, rnb + i, lnb + j, maxlnb, 1);
480 if (unlikely(rc < 0))
482 /* correct index for local buffers to continue with */
483 for (k = 0; k < rc; k++) {
484 lnb[j + k].lnb_flags = rnb[i].rnb_flags;
485 if (!(rnb[i].rnb_flags & OBD_BRW_GRANTED))
486 lnb[j + k].lnb_rc = -ENOSPC;
491 tot_bytes += rnb[i].rnb_len;
494 rc = dt_write_prep(env, dob, lnb, *nr_local);
500 dt_bufs_put(env, dob, lnb, *nr_local);
502 mdt_dom_read_unlock(mo);
503 /* tgt_grant_prepare_write() was called, so we must commit */
504 tgt_grant_commit(exp, oa->o_grant_used, rc);
505 /* let's still process incoming grant information packed in the oa,
506 * but without enforcing grant since we won't proceed with the write.
507 * Just like a read request actually. */
508 tgt_grant_prepare_read(env, exp, oa);
512 int mdt_obd_preprw(const struct lu_env *env, int cmd, struct obd_export *exp,
513 struct obdo *oa, int objcount, struct obd_ioobj *obj,
514 struct niobuf_remote *rnb, int *nr_local,
515 struct niobuf_local *lnb)
517 struct tgt_session_info *tsi = tgt_ses_info(env);
518 struct mdt_thread_info *info = tsi2mdt_info(tsi);
519 struct lu_attr *la = &info->mti_attr.ma_attr;
520 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
521 struct mdt_object *mo;
524 /* The default value PTLRPC_MAX_BRW_PAGES is set in tgt_brw_write()
525 * but for MDT it is different, correct it here. */
526 if (*nr_local > MD_MAX_BRW_PAGES)
527 *nr_local = MD_MAX_BRW_PAGES;
529 if (!oa || objcount != 1 || obj->ioo_bufcnt == 0) {
530 CERROR("%s: bad parameters %p/%i/%i\n",
531 exp->exp_obd->obd_name, oa, objcount, obj->ioo_bufcnt);
535 mo = mdt_object_find(env, mdt, &tsi->tsi_fid);
537 GOTO(out, rc = PTR_ERR(mo));
539 LASSERT(info->mti_object == NULL);
540 info->mti_object = mo;
542 if (cmd == OBD_BRW_WRITE) {
543 la_from_obdo(la, oa, OBD_MD_FLGETATTR);
544 rc = mdt_preprw_write(env, exp, mdt, mo, la, oa,
545 objcount, obj, rnb, nr_local, lnb);
546 } else if (cmd == OBD_BRW_READ) {
547 tgt_grant_prepare_read(env, exp, oa);
548 rc = mdt_preprw_read(env, exp, mdt, mo, la,
549 obj->ioo_bufcnt, rnb, nr_local, lnb);
550 obdo_from_la(oa, la, LA_ATIME);
552 CERROR("%s: wrong cmd %d received!\n",
553 exp->exp_obd->obd_name, cmd);
557 lu_object_put(env, &mo->mot_obj);
558 info->mti_object = NULL;
564 static int mdt_commitrw_read(const struct lu_env *env, struct mdt_device *mdt,
565 struct mdt_object *mo, int objcount, int niocount,
566 struct niobuf_local *lnb)
568 struct dt_object *dob;
573 dob = mdt_obj2dt(mo);
576 dt_bufs_put(env, dob, lnb, niocount);
578 mdt_dom_read_unlock(mo);
582 static int mdt_commitrw_write(const struct lu_env *env, struct obd_export *exp,
583 struct mdt_device *mdt, struct mdt_object *mo,
584 struct lu_attr *la, struct obdo *oa, int objcount,
585 int niocount, struct niobuf_local *lnb,
586 unsigned long granted, int old_rc)
588 struct dt_device *dt = mdt->mdt_bottom;
589 struct dt_object *dob;
597 dob = mdt_obj2dt(mo);
600 GOTO(out, rc = old_rc);
602 la->la_valid &= LA_ATIME | LA_MTIME | LA_CTIME;
604 if (!dt_object_exists(dob))
605 GOTO(out, rc = -ENOENT);
609 DEBUG_REQ(D_WARNING, tgt_ses_req(tgt_ses_info(env)),
610 "%s: commit with no pages for "DFID": rc = %d\n",
611 exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)), rc);
615 CFS_FAIL_TIMEOUT(OBD_FAIL_MDS_COMMITRW_DELAY, cfs_fail_val);
617 th = dt_trans_create(env, dt);
619 GOTO(out, rc = PTR_ERR(th));
621 for (i = 0; i < niocount; i++) {
622 if (!(lnb[i].lnb_flags & OBD_BRW_ASYNC)) {
628 if (OBD_FAIL_CHECK(OBD_FAIL_OST_DQACQ_NET))
629 GOTO(out_stop, rc = -EINPROGRESS);
631 rc = dt_declare_write_commit(env, dob, lnb, niocount, th);
636 /* update [mac]time if needed */
637 rc = dt_declare_attr_set(env, dob, la, th);
642 tgt_vbr_obj_set(env, dob);
643 rc = dt_trans_start(env, dt, th);
647 dt_write_lock(env, dob, 0);
648 if (lu_object_is_dying(&mo->mot_header)) {
649 /* Commit to stale object can be just skipped silently. */
650 CDEBUG(D_INODE, "skip commit to stale object "DFID"\n",
651 PFID(mdt_object_fid(mo)));
652 GOTO(unlock, rc = 0);
654 rc = dt_write_commit(env, dob, lnb, niocount, th, oa->o_size);
656 restart = th->th_restart_tran;
661 rc = dt_attr_set(env, dob, la, th);
665 /* get attr to return */
666 rc = dt_attr_get(env, dob, la);
668 dt_write_unlock(env, dob);
671 /* Force commit to make the just-deleted blocks
672 * reusable. LU-456 */
677 if (rc == 0 && granted > 0) {
678 if (tgt_grant_commit_cb_add(th, exp, granted) == 0)
682 th->th_result = restart ? 0 : rc;
683 dt_trans_stop(env, dt, th);
684 if (rc == -ENOSPC && retries++ < 3) {
685 CDEBUG(D_INODE, "retry after force commit, retries:%d\n",
692 if (retries % 10000 == 0)
693 CERROR("%s: restart IO write too many times: %d\n",
694 exp->exp_obd->obd_name, retries);
695 CDEBUG(D_INODE, "retry transaction, retries:%d\n",
701 dt_bufs_put(env, dob, lnb, niocount);
702 mdt_dom_read_unlock(mo);
704 tgt_grant_commit(exp, granted, old_rc);
708 void mdt_dom_obj_lvb_update(const struct lu_env *env, struct mdt_object *mo,
709 struct obdo *oa, bool increase_only)
711 struct mdt_device *mdt = mdt_dev(mo->mot_obj.lo_dev);
712 struct ldlm_res_id resid;
713 struct ldlm_resource *res;
715 fid_build_reg_res_name(mdt_object_fid(mo), &resid);
716 res = ldlm_resource_get(mdt->mdt_namespace, &resid, LDLM_IBITS, 1);
720 /* Update lvbo data if exists. */
721 if (mdt_dom_lvb_is_valid(res)) {
722 mdt_dom_disk_lvbo_update(env, mo, res, increase_only);
724 struct ost_lvb *res_lvb = res->lr_lvb_data;
727 oa->o_valid |= OBD_MD_FLBLOCKS | OBD_MD_FLSIZE |
728 OBD_MD_FLMTIME | OBD_MD_FLATIME |
730 oa->o_blocks = res_lvb->lvb_blocks;
731 oa->o_size = res_lvb->lvb_size;
732 oa->o_atime = res_lvb->lvb_atime;
733 oa->o_mtime = res_lvb->lvb_mtime;
734 oa->o_ctime = res_lvb->lvb_ctime;
738 ldlm_resource_putref(res);
741 int mdt_obd_commitrw(const struct lu_env *env, int cmd, struct obd_export *exp,
742 struct obdo *oa, int objcount, struct obd_ioobj *obj,
743 struct niobuf_remote *rnb, int npages,
744 struct niobuf_local *lnb, int old_rc, int nob,
747 struct tgt_session_info *tsi = tgt_ses_info(env);
748 struct ptlrpc_request *req = tgt_ses_req(tsi);
749 struct mdt_thread_info *info = mdt_th_info(env);
750 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
751 struct mdt_object *mo = info->mti_object;
752 struct lu_attr *la = &info->mti_attr.ma_attr;
759 if (cmd == OBD_BRW_WRITE) {
760 struct lu_nodemap *nodemap;
761 __u32 mapped_uid, mapped_gid, mapped_projid;
763 mapped_uid = oa->o_uid;
764 mapped_gid = oa->o_gid;
765 mapped_projid = oa->o_projid;
766 nodemap = nodemap_get_from_exp(exp);
767 if (!IS_ERR(nodemap)) {
768 mapped_uid = nodemap_map_id(nodemap, NODEMAP_UID,
769 NODEMAP_FS_TO_CLIENT,
771 mapped_gid = nodemap_map_id(nodemap, NODEMAP_GID,
772 NODEMAP_FS_TO_CLIENT,
774 mapped_projid = nodemap_map_id(nodemap, NODEMAP_PROJID,
775 NODEMAP_FS_TO_CLIENT,
777 } else if (old_rc == 0) {
778 old_rc = PTR_ERR(nodemap);
781 if (!IS_ERR_OR_NULL(nodemap)) {
782 /* do not bypass quota enforcement if squashed uid */
783 if (unlikely(mapped_uid == nodemap->nm_squash_uid)) {
786 for (idx = 0; idx < npages; idx++)
787 lnb[idx].lnb_flags &=
788 ~OBD_BRW_SYS_RESOURCE;
791 nodemap_putref(nodemap);
793 /* Don't update timestamps if this write is older than a
794 * setattr which modifies the timestamps. b=10150 */
796 /* XXX when we start having persistent reservations this needs
797 * to be changed to ofd_fmd_get() to create the fmd if it
798 * doesn't already exist so we can store the reservation handle
800 valid = OBD_MD_FLUID | OBD_MD_FLGID;
801 if (tgt_fmd_check(exp, mdt_object_fid(mo),
802 mdt_info_req(info)->rq_xid))
803 valid |= OBD_MD_FLATIME | OBD_MD_FLMTIME |
806 la_from_obdo(la, oa, valid);
808 mdt_counter_incr(req, LPROC_MDT_IO_WRITE_BYTES, nob);
809 mdt_counter_incr(req, LPROC_MDT_IO_WRITE,
810 ktime_us_delta(ktime_get(), kstart));
812 rc = mdt_commitrw_write(env, exp, mdt, mo, la, oa, objcount,
813 npages, lnb, oa->o_grant_used, old_rc);
815 obdo_from_la(oa, la, VALID_FLAGS | LA_GID | LA_UID);
817 obdo_from_la(oa, la, LA_GID | LA_UID);
819 mdt_dom_obj_lvb_update(env, mo, NULL, false);
820 /* don't report overquota flag if we failed before reaching
822 if (old_rc == 0 && (rc == 0 || rc == -EDQUOT)) {
823 /* return the overquota flags to client */
824 if (lnb[0].lnb_flags & OBD_BRW_OVER_USRQUOTA) {
825 if (oa->o_valid & OBD_MD_FLFLAGS)
826 oa->o_flags |= OBD_FL_NO_USRQUOTA;
828 oa->o_flags = OBD_FL_NO_USRQUOTA;
831 if (lnb[0].lnb_flags & OBD_BRW_OVER_GRPQUOTA) {
832 if (oa->o_valid & OBD_MD_FLFLAGS)
833 oa->o_flags |= OBD_FL_NO_GRPQUOTA;
835 oa->o_flags = OBD_FL_NO_GRPQUOTA;
838 if (lnb[0].lnb_flags & OBD_BRW_OVER_PRJQUOTA) {
839 if (oa->o_valid & OBD_MD_FLFLAGS)
840 oa->o_flags |= OBD_FL_NO_PRJQUOTA;
842 oa->o_flags = OBD_FL_NO_PRJQUOTA;
845 if (lnb[0].lnb_flags & OBD_BRW_ROOT_PRJQUOTA)
846 oa->o_flags |= OBD_FL_ROOT_PRJQUOTA;
849 oa->o_flags |= OBD_FL_ROOT_SQUASH;
851 oa->o_valid |= OBD_MD_FLFLAGS | OBD_MD_FLUSRQUOTA |
852 OBD_MD_FLGRPQUOTA | OBD_MD_FLPRJQUOTA;
854 /* Convert back to client IDs. LU-9671.
855 * nodemap_get_from_exp() may fail due to nodemap deactivated,
856 * server ID will be returned back to client in that case.
858 oa->o_uid = mapped_uid;
859 oa->o_gid = mapped_gid;
860 oa->o_projid = mapped_projid;
861 } else if (cmd == OBD_BRW_READ) {
862 /* If oa != NULL then mdt_preprw_read updated the inode
863 * atime and we should update the lvb so that other glimpses
864 * will also get the updated value. bug 5972 */
866 mdt_dom_obj_lvb_update(env, mo, NULL, true);
868 mdt_counter_incr(req, LPROC_MDT_IO_READ_BYTES, nob);
869 mdt_counter_incr(req, LPROC_MDT_IO_READ,
870 ktime_us_delta(ktime_get(), kstart));
872 rc = mdt_commitrw_read(env, mdt, mo, objcount, npages, lnb);
878 mdt_thread_info_fini(info);
882 int mdt_object_fallocate(const struct lu_env *env, struct dt_device *dt,
883 struct dt_object *dob, __u64 start, __u64 end,
884 int mode, struct lu_attr *la)
891 if (!dt_object_exists(dob))
894 th = dt_trans_create(env, dt);
898 rc = dt_declare_attr_set(env, dob, la, th);
902 rc = dt_declare_fallocate(env, dob, start, end, mode, th);
906 tgt_vbr_obj_set(env, dob);
907 rc = dt_trans_start(env, dt, th);
911 dt_write_lock(env, dob, 0);
912 rc = dt_falloc(env, dob, start, end, mode, th);
915 rc = dt_attr_set(env, dob, la, th);
919 dt_write_unlock(env, dob);
922 dt_trans_stop(env, dt, th);
927 * MDT request handler for OST_FALLOCATE RPC.
929 * This is part of request processing. Validate request fields,
930 * preallocate the given MDT object and pack reply.
932 * \param[in] tsi target session environment for this request
934 * \retval 0 if successful
935 * \retval negative value on error
937 int mdt_fallocate_hdl(struct tgt_session_info *tsi)
939 struct obdo *oa = &tsi->tsi_ost_body->oa;
940 struct ptlrpc_request *req = tgt_ses_req(tsi);
941 struct ost_body *repbody;
942 struct mdt_thread_info *info;
943 struct ldlm_namespace *ns = tsi->tsi_tgt->lut_obd->obd_namespace;
944 struct obd_export *exp = tsi->tsi_exp;
945 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
946 struct mdt_object *mo;
947 struct dt_object *dob;
950 struct lustre_handle lh = { 0, };
954 ktime_t kstart = ktime_get();
956 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
958 RETURN(err_serious(-ENOMEM));
961 * fallocate() start and end are passed in o_size and o_blocks
962 * on the wire. Clients 2.15.0 and newer should always set
963 * the OBD_MD_FLSIZE and OBD_MD_FLBLOCKS valid flags, but some
964 * older client versions did not. We permit older clients to
965 * not set these flags, checking their version by proxy using
966 * the lack of OBD_CONNECT_TRUNCLOCK to imply 2.14.0 and older.
968 * Return -EOPNOTSUPP to also work with older clients not
969 * supporting newer server modes.
971 * fallocate start and end are passed in o_size, o_blocks
974 if ((oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
975 (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)
976 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 21, 53, 0)
977 && !tsi->tsi_exp->exp_old_falloc
984 CDEBUG(D_INFO, "%s: start: %llu end: %llu\n",
985 mdt->mdt_child_exp->exp_obd->obd_name, start, end);
987 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 21, 53, 0)
989 * For inter-op case with older clients (where exp_old_falloc is true)
990 * fallocate() start and end are passed in as 0 (For interior case
991 * where end offset less than file size) This is fixed later.
992 * For such cases we return -EOPNOTSUPP
994 if (tsi->tsi_exp->exp_old_falloc && start >= end)
997 /* client should already limit len >= 0 */
1001 mode = oa->o_falloc_mode;
1004 "fallocate: "DFID", mode = %#x, start = %lld, end = %lld\n",
1005 PFID(&tsi->tsi_fid), mode, start, end);
1008 * mode == 0 (which is standard prealloc) and PUNCH is supported
1009 * Rest of mode options are not supported yet.
1011 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
1012 RETURN(-EOPNOTSUPP);
1014 if (mode & FALLOC_FL_PUNCH_HOLE && !(mode & FALLOC_FL_KEEP_SIZE)) {
1015 CWARN("%s: PUNCH mode misses KEEP_SIZE flag, setting it\n",
1016 tsi->tsi_tgt->lut_obd->obd_name);
1017 mode |= FALLOC_FL_KEEP_SIZE;
1020 info = tsi2mdt_info(tsi);
1021 la = &info->mti_attr.ma_attr;
1023 repbody->oa.o_oi = oa->o_oi;
1024 repbody->oa.o_valid = OBD_MD_FLID;
1026 srvlock = oa->o_valid & OBD_MD_FLFLAGS &&
1027 oa->o_flags & OBD_FL_SRVLOCK;
1030 rc = tgt_mdt_data_lock(ns, &tsi->tsi_resid, &lh, LCK_PW,
1036 mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
1038 GOTO(out_unlock, rc = PTR_ERR(mo));
1040 if (!mdt_object_exists(mo))
1041 GOTO(out_put, rc = -ENOENT);
1043 /* Shouldn't happen on dirs */
1044 if (S_ISDIR(lu_object_attr(&mo->mot_obj))) {
1046 CERROR("%s: fallocate on dir "DFID": rc = %d\n",
1047 exp->exp_obd->obd_name, PFID(&tsi->tsi_fid), rc);
1051 la_from_obdo(la, oa, OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
1053 mdt_dom_write_lock(mo);
1054 dob = mdt_obj2dt(mo);
1056 if (la->la_valid & (LA_ATIME | LA_MTIME | LA_CTIME))
1057 tgt_fmd_update(tsi->tsi_exp, &tsi->tsi_fid,
1058 tgt_ses_req(tsi)->rq_xid);
1060 rc = mdt_object_fallocate(tsi->tsi_env, mdt->mdt_bottom, dob, start,
1062 mdt_dom_write_unlock(mo);
1066 mdt_dom_obj_lvb_update(tsi->tsi_env, mo, &repbody->oa, false);
1068 mdt_counter_incr(req, LPROC_MDT_FALLOCATE,
1069 ktime_us_delta(ktime_get(), kstart));
1073 lu_object_put(tsi->tsi_env, &mo->mot_obj);
1076 tgt_data_unlock(&lh, LCK_PW);
1078 mdt_thread_info_fini(info);
1082 int mdt_object_punch(const struct lu_env *env, struct dt_device *dt,
1083 struct dt_object *dob, __u64 start, __u64 end,
1091 /* we support truncate, not punch yet */
1092 LASSERT(end == OBD_OBJECT_EOF);
1094 if (!dt_object_exists(dob))
1097 th = dt_trans_create(env, dt);
1099 RETURN(PTR_ERR(th));
1101 rc = dt_declare_attr_set(env, dob, la, th);
1105 rc = dt_declare_punch(env, dob, start, OBD_OBJECT_EOF, th);
1109 tgt_vbr_obj_set(env, dob);
1110 rc = dt_trans_start(env, dt, th);
1114 dt_write_lock(env, dob, 0);
1115 rc = dt_punch(env, dob, start, OBD_OBJECT_EOF, th);
1118 rc = dt_attr_set(env, dob, la, th);
1122 dt_write_unlock(env, dob);
1125 dt_trans_stop(env, dt, th);
1129 int mdt_punch_hdl(struct tgt_session_info *tsi)
1131 const struct obdo *oa = &tsi->tsi_ost_body->oa;
1132 struct ptlrpc_request *req = tgt_ses_req(tsi);
1133 struct ost_body *repbody;
1134 struct mdt_thread_info *info;
1136 struct ldlm_namespace *ns = tsi->tsi_tgt->lut_obd->obd_namespace;
1137 struct obd_export *exp = tsi->tsi_exp;
1138 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
1139 struct mdt_object *mo;
1140 struct dt_object *dob;
1142 struct lustre_handle lh = { 0, };
1143 ktime_t kstart = ktime_get();
1150 if ((oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
1151 (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
1152 RETURN(err_serious(-EPROTO));
1154 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1155 if (repbody == NULL)
1156 RETURN(err_serious(-ENOMEM));
1158 /* punch start,end are passed in o_size,o_blocks throught wire */
1162 if (end != OBD_OBJECT_EOF) /* Only truncate is supported */
1165 info = tsi2mdt_info(tsi);
1166 la = &info->mti_attr.ma_attr;
1167 /* standard truncate optimization: if file body is completely
1168 * destroyed, don't send data back to the server. */
1170 flags |= LDLM_FL_AST_DISCARD_DATA;
1172 repbody->oa.o_oi = oa->o_oi;
1173 repbody->oa.o_valid = OBD_MD_FLID;
1175 srvlock = (exp_connect_flags(exp) & OBD_CONNECT_SRVLOCK) &&
1176 oa->o_valid & OBD_MD_FLFLAGS &&
1177 oa->o_flags & OBD_FL_SRVLOCK;
1180 rc = tgt_mdt_data_lock(ns, &tsi->tsi_resid, &lh, LCK_PW,
1186 CDEBUG(D_INODE, "calling punch for object "DFID", valid = %#llx"
1187 ", start = %lld, end = %lld\n", PFID(&tsi->tsi_fid),
1188 oa->o_valid, start, end);
1190 mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
1192 GOTO(out_unlock, rc = PTR_ERR(mo));
1194 if (!mdt_object_exists(mo))
1195 GOTO(out_put, rc = -ENOENT);
1197 /* Shouldn't happen on dirs */
1198 if (S_ISDIR(lu_object_attr(&mo->mot_obj))) {
1200 CERROR("%s: Truncate on dir "DFID": rc = %d\n",
1201 exp->exp_obd->obd_name, PFID(&tsi->tsi_fid), rc);
1205 mdt_dom_write_lock(mo);
1206 dob = mdt_obj2dt(mo);
1208 la_from_obdo(la, oa, OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
1209 la->la_size = start;
1210 la->la_valid |= LA_SIZE;
1212 /* MDT supports FMD for Data-on-MDT needs */
1213 if (la->la_valid & (LA_ATIME | LA_MTIME | LA_CTIME))
1214 tgt_fmd_update(tsi->tsi_exp, &tsi->tsi_fid,
1215 tgt_ses_req(tsi)->rq_xid);
1217 rc = mdt_object_punch(tsi->tsi_env, mdt->mdt_bottom, dob,
1219 mdt_dom_write_unlock(mo);
1223 mdt_dom_obj_lvb_update(tsi->tsi_env, mo, &repbody->oa, false);
1225 mdt_counter_incr(req, LPROC_MDT_IO_PUNCH,
1226 ktime_us_delta(ktime_get(), kstart));
1229 lu_object_put(tsi->tsi_env, &mo->mot_obj);
1232 tgt_data_unlock(&lh, LCK_PW);
1234 mdt_thread_info_fini(info);
1239 * MDT glimpse for Data-on-MDT
1241 * If there is write lock on client then function issues glimpse_ast to get
1242 * an actual size from that client.
1245 int mdt_do_glimpse(const struct lu_env *env, struct ldlm_namespace *ns,
1246 struct ldlm_resource *res)
1248 union ldlm_policy_data policy;
1249 struct lustre_handle lockh;
1250 enum ldlm_mode mode;
1251 struct ldlm_lock *lock;
1252 struct ldlm_glimpse_work *gl_work;
1258 /* There can be only one write lock covering data, try to match it. */
1259 policy.l_inodebits.bits = MDS_INODELOCK_DOM;
1260 mode = ldlm_lock_match(ns, LDLM_FL_TEST_LOCK,
1261 &res->lr_name, LDLM_IBITS, &policy,
1264 /* There is no PW lock on this object; finished. */
1268 lock = ldlm_handle2lock(&lockh);
1273 * This check is for lock taken in mdt_reint_unlink() that does
1274 * not have l_glimpse_ast set. So the logic is: if there is a lock
1275 * with no l_glimpse_ast set, this object is being destroyed already.
1276 * Hence, if you are grabbing DLM locks on the server, always set
1277 * non-NULL glimpse_ast (e.g., ldlm_request.c::ldlm_glimpse_ast()).
1279 if (lock->l_glimpse_ast == NULL) {
1280 LDLM_DEBUG(lock, "no l_glimpse_ast");
1281 GOTO(out, rc = -ENOENT);
1284 OBD_SLAB_ALLOC_PTR_GFP(gl_work, ldlm_glimpse_work_kmem, GFP_ATOMIC);
1286 GOTO(out, rc = -ENOMEM);
1288 /* Populate the gl_work structure.
1289 * Grab additional reference on the lock which will be released in
1290 * ldlm_work_gl_ast_lock() */
1291 gl_work->gl_lock = LDLM_LOCK_GET(lock);
1292 /* The glimpse callback is sent to one single IO lock. As a result,
1293 * the gl_work list is just composed of one element */
1294 list_add_tail(&gl_work->gl_list, &gl_list);
1295 /* There is actually no need for a glimpse descriptor when glimpsing
1297 gl_work->gl_desc = NULL;
1298 /* the ldlm_glimpse_work structure is allocated on the stack */
1299 gl_work->gl_flags = LDLM_GL_WORK_SLAB_ALLOCATED;
1301 ldlm_glimpse_locks(res, &gl_list); /* this will update the LVB */
1303 /* If the list is not empty, we failed to glimpse a lock and
1304 * must clean it up. Usually due to a race with unlink.*/
1305 if (!list_empty(&gl_list)) {
1306 LDLM_LOCK_RELEASE(lock);
1307 OBD_SLAB_FREE_PTR(gl_work, ldlm_glimpse_work_kmem);
1312 LDLM_LOCK_PUT(lock);
1316 static void mdt_lvb2reply(struct ldlm_resource *res, struct mdt_body *mb,
1317 struct ost_lvb *lvb)
1319 struct ost_lvb *res_lvb;
1322 res_lvb = res->lr_lvb_data;
1328 mb->mbo_dom_size = res_lvb->lvb_size;
1329 mb->mbo_dom_blocks = res_lvb->lvb_blocks;
1330 mb->mbo_mtime = res_lvb->lvb_mtime;
1331 mb->mbo_ctime = res_lvb->lvb_ctime;
1332 mb->mbo_atime = res_lvb->lvb_atime;
1333 mb->mbo_valid |= OBD_MD_FLATIME | OBD_MD_FLCTIME |
1334 OBD_MD_FLMTIME | OBD_MD_DOM_SIZE;
1336 CDEBUG(D_DLMTRACE, "size %llu\n", res_lvb->lvb_size);
1342 * MDT glimpse for Data-on-MDT
1344 * This function is called when MDT get attributes for the DoM object.
1345 * If there is write lock on client then function issues glimpse_ast to get
1346 * an actual size from that client.
1348 int mdt_dom_object_size(const struct lu_env *env, struct mdt_device *mdt,
1349 const struct lu_fid *fid, struct mdt_body *mb,
1352 struct ldlm_res_id resid;
1353 struct ldlm_resource *res;
1358 fid_build_reg_res_name(fid, &resid);
1359 res = ldlm_resource_get(mdt->mdt_namespace, &resid, LDLM_IBITS, 1);
1363 /* Update lvbo data if DoM lock returned or if LVB is not yet valid. */
1364 if (dom_lock || !mdt_dom_lvb_is_valid(res))
1365 mdt_dom_lvbo_update(res, NULL, NULL, false);
1367 mdt_lvb2reply(res, mb, NULL);
1368 ldlm_resource_putref(res);
1373 * MDT DoM lock intent policy (glimpse)
1375 * Intent policy is called when lock has an intent, for DoM file that
1376 * means glimpse lock and policy fills Lock Value Block (LVB).
1378 * If already granted lock is found it will be placed in \a lockp and
1379 * returned back to caller function.
1381 * \param[in] tsi session info
1382 * \param[in,out] lockp pointer to the lock
1383 * \param[in] flags LDLM flags
1385 * \retval ELDLM_LOCK_REPLACED if already granted lock was found
1386 * and placed in \a lockp
1387 * \retval ELDLM_LOCK_ABORTED in other cases except error
1388 * \retval negative value on error
1390 int mdt_glimpse_enqueue(struct mdt_thread_info *mti, struct ldlm_namespace *ns,
1391 struct ldlm_lock **lockp, __u64 flags)
1393 struct ldlm_lock *lock = *lockp;
1394 struct ldlm_resource *res = lock->l_resource;
1395 ldlm_processing_policy policy;
1396 struct ldlm_reply *rep;
1397 struct mdt_body *mbo;
1398 struct ost_lvb *lvb;
1399 bool old_client = !exp_connect_dom_lvb(mti->mti_exp);
1404 policy = ldlm_get_processing_policy(res);
1405 LASSERT(policy != NULL);
1407 if (unlikely(old_client)) {
1408 req_capsule_set_size(mti->mti_pill, &RMF_MDT_MD, RCL_SERVER, 0);
1409 req_capsule_set_size(mti->mti_pill, &RMF_ACL, RCL_SERVER, 0);
1411 req_capsule_set_size(mti->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
1414 rc = req_capsule_server_pack(mti->mti_pill);
1416 RETURN(err_serious(rc));
1418 rep = req_capsule_server_get(mti->mti_pill, &RMF_DLM_REP);
1420 if (unlikely(old_client)) {
1421 mbo = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1425 lvb = req_capsule_server_get(mti->mti_pill, &RMF_DLM_LVB);
1431 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
1432 * lock was found by ldlm_handle_enqueue(); if so no need to grant
1434 if (flags & LDLM_FL_RESENT) {
1435 rc = LDLM_ITER_CONTINUE;
1437 __u64 tmpflags = LDLM_FL_BLOCK_NOWAIT;
1438 enum ldlm_error err;
1440 rc = policy(lock, &tmpflags, LDLM_PROCESS_RESCAN, &err, NULL);
1441 check_res_locked(res);
1445 /* The lock met with no resistance; we're finished. */
1446 if (rc == LDLM_ITER_CONTINUE) {
1447 GOTO(fill_mbo, rc = ELDLM_LOCK_REPLACED);
1448 } else if (flags & LDLM_FL_BLOCK_NOWAIT) {
1449 /* LDLM_FL_BLOCK_NOWAIT means it is for AGL. Do not send glimpse
1450 * callback for glimpse size. The real size user will trigger
1451 * the glimpse callback when necessary. */
1452 GOTO(fill_mbo, rc = ELDLM_LOCK_ABORTED);
1455 rc = mdt_do_glimpse(mti->mti_env, ns, res);
1456 if (rc == -ENOENT) {
1457 /* We are racing with unlink(); just return -ENOENT */
1458 rep->lock_policy_res2 = ptlrpc_status_hton(-ENOENT);
1459 } else if (rc == -EINVAL) {
1460 /* this is possible is client lock has been cancelled but
1461 * still exists on server. If that lock was found on server
1462 * as only conflicting lock then the client has already
1463 * size authority and glimpse is not needed. */
1464 CDEBUG(D_DLMTRACE, "Glimpse from the client owning lock\n");
1465 } else if (rc < 0) {
1468 rc = ELDLM_LOCK_ABORTED;
1470 /* LVB can be without valid data in case of DOM */
1471 if (!mdt_dom_lvb_is_valid(res))
1472 mdt_dom_lvbo_update(res, lock, NULL, false);
1473 mdt_lvb2reply(res, mbo, lvb);
1478 int mdt_brw_enqueue(struct mdt_thread_info *mti, struct ldlm_namespace *ns,
1479 struct ldlm_lock **lockp, __u64 flags)
1481 struct tgt_session_info *tsi = tgt_ses_info(mti->mti_env);
1482 struct lu_fid *fid = &tsi->tsi_fid;
1483 struct ldlm_lock *lock = *lockp;
1484 struct ldlm_resource *res = lock->l_resource;
1485 struct ldlm_reply *rep;
1486 struct mdt_body *mbo;
1487 struct mdt_lock_handle *lhc = &mti->mti_lh[MDT_LH_RMT];
1488 struct mdt_object *mo;
1493 req_capsule_set_size(mti->mti_pill, &RMF_MDT_MD, RCL_SERVER, 0);
1494 req_capsule_set_size(mti->mti_pill, &RMF_ACL, RCL_SERVER, 0);
1495 rc = req_capsule_server_pack(mti->mti_pill);
1497 RETURN(err_serious(rc));
1499 rep = req_capsule_server_get(mti->mti_pill, &RMF_DLM_REP);
1503 mbo = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1507 fid_extract_from_res_name(fid, &res->lr_name);
1508 mo = mdt_object_find(mti->mti_env, mti->mti_mdt, fid);
1509 if (unlikely(IS_ERR(mo)))
1510 RETURN(PTR_ERR(mo));
1512 if (!mdt_object_exists(mo))
1513 GOTO(out, rc = -ENOENT);
1515 if (mdt_object_remote(mo))
1516 GOTO(out, rc = -EPROTO);
1518 /* Get lock from request for possible resent case. */
1519 mdt_intent_fixup_resent(mti, *lockp, lhc, flags);
1521 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1522 mdt_lock_handle_init(lhc);
1523 mdt_lh_reg_init(lhc, *lockp);
1525 /* This will block MDT thread but it should be fine until
1526 * client caches small amount of data for DoM, which should be
1527 * smaller than one BRW RPC and should be able to be
1528 * piggybacked by lock cancel RPC.
1529 * If the client could hold the lock too long, this code can be
1530 * revised to call mdt_object_lock_try(). And if fails, it will
1531 * return ELDLM_OK here and fall back into normal lock enqueue
1534 rc = mdt_object_lock(mti, mo, lhc, MDS_INODELOCK_DOM);
1539 if (!mdt_dom_lvb_is_valid(res)) {
1540 rc = mdt_dom_lvb_alloc(res);
1543 mdt_dom_disk_lvbo_update(mti->mti_env, mo, res, false);
1545 mdt_lvb2reply(res, mbo, NULL);
1547 rep->lock_policy_res2 = clear_serious(rc);
1548 if (rep->lock_policy_res2) {
1549 lhc->mlh_reg_lh.cookie = 0ull;
1550 GOTO(out, rc = ELDLM_LOCK_ABORTED);
1553 rc = mdt_intent_lock_replace(mti, lockp, lhc, flags, rc);
1556 lhc->mlh_reg_lh.cookie = 0ull;
1557 mdt_object_put(mti->mti_env, mo);
1561 /* check if client has already DoM lock for given resource */
1562 bool mdt_dom_client_has_lock(struct mdt_thread_info *info,
1563 const struct lu_fid *fid)
1565 struct mdt_device *mdt = info->mti_mdt;
1566 union ldlm_policy_data *policy = &info->mti_policy;
1567 struct ldlm_res_id *res_id = &info->mti_res_id;
1568 __u64 open_flags = info->mti_spec.sp_cr_flags;
1569 struct lustre_handle lockh;
1570 enum ldlm_mode mode;
1571 struct ldlm_lock *lock;
1575 policy->l_inodebits.bits = MDS_INODELOCK_DOM;
1576 fid_build_reg_res_name(fid, res_id);
1579 lm = (open_flags & MDS_FMODE_WRITE) ? LCK_PW : LCK_PR | LCK_PW;
1580 mode = ldlm_lock_match(mdt->mdt_namespace, LDLM_FL_BLOCK_GRANTED |
1581 LDLM_FL_TEST_LOCK, res_id, LDLM_IBITS, policy,
1584 /* There is no other PW lock on this object; finished. */
1588 lock = ldlm_handle2lock(&lockh);
1592 /* check if lock from the same client */
1593 rc = (lock->l_export->exp_handle.h_cookie ==
1594 info->mti_exp->exp_handle.h_cookie);
1595 LDLM_LOCK_PUT(lock);
1600 * MDT request handler for OST_GETATTR RPC.
1602 * This is data-specific request to get object and layout versions under
1603 * IO lock. It is reliable only for Data-on-MDT files.
1605 * \param[in] tsi target session environment for this request
1607 * \retval 0 if successful
1608 * \retval negative value on error
1610 int mdt_data_version_get(struct tgt_session_info *tsi)
1612 struct mdt_thread_info *mti = mdt_th_info(tsi->tsi_env);
1613 struct mdt_device *mdt = mti->mti_mdt;
1614 struct mdt_body *repbody;
1615 struct mdt_object *mo = mti->mti_object;
1616 struct lov_comp_md_v1 *comp;
1617 struct lustre_handle lh = { 0 };
1620 enum ldlm_mode lock_mode = LCK_PR;
1626 req_capsule_set_size(tsi->tsi_pill, &RMF_MDT_MD, RCL_SERVER, 0);
1627 req_capsule_set_size(tsi->tsi_pill, &RMF_ACL, RCL_SERVER, 0);
1628 req_capsule_set_size(tsi->tsi_pill, &RMF_FILE_ENCCTX, RCL_SERVER, 0);
1629 rc = req_capsule_server_pack(tsi->tsi_pill);
1630 if (unlikely(rc != 0))
1631 RETURN(err_serious(rc));
1633 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1634 if (repbody == NULL)
1637 srvlock = tsi->tsi_mdt_body->mbo_valid & OBD_MD_FLFLAGS &&
1638 tsi->tsi_mdt_body->mbo_flags & OBD_FL_SRVLOCK;
1641 if (unlikely(tsi->tsi_mdt_body->mbo_flags & OBD_FL_FLUSH))
1644 fid_build_reg_res_name(&tsi->tsi_fid, &tsi->tsi_resid);
1645 rc = tgt_mdt_data_lock(mdt->mdt_namespace, &tsi->tsi_resid,
1646 &lh, lock_mode, &flags);
1651 if (!mdt_object_exists(mo))
1652 GOTO(out, rc = -ENOENT);
1653 if (mdt_object_remote(mo))
1654 GOTO(out, rc = -EREMOTE);
1655 if (!S_ISREG(lu_object_attr(&mo->mot_obj)))
1656 GOTO(out, rc = -EBADF);
1658 /* Get version first */
1659 version = dt_version_get(tsi->tsi_env, mdt_obj2dt(mo));
1660 if (version && version != -EOPNOTSUPP) {
1661 repbody->mbo_valid |= OBD_MD_FLDATAVERSION;
1662 /* re-use mbo_ioepoch to transfer version */
1663 repbody->mbo_version = version;
1666 /* Read layout to get its version */
1667 rc = mdt_big_xattr_get(mti, mo, XATTR_NAME_LOV);
1668 if (rc == -ENODATA) /* File has no layout yet */
1673 comp = mti->mti_buf.lb_buf;
1674 if (le32_to_cpu(comp->lcm_magic) != LOV_MAGIC_COMP_V1) {
1675 CDEBUG(D_INFO, DFID" has no composite layout",
1676 PFID(&tsi->tsi_fid));
1677 GOTO(out, rc = -ESTALE);
1680 CDEBUG(D_INODE, DFID": layout version: %u\n",
1681 PFID(&tsi->tsi_fid), le32_to_cpu(comp->lcm_layout_gen));
1683 repbody->mbo_valid |= OBD_MD_LAYOUT_VERSION;
1684 /* re-use mbo_rdev for that */
1685 repbody->mbo_layout_gen = le32_to_cpu(comp->lcm_layout_gen);
1689 tgt_data_unlock(&lh, lock_mode);
1691 repbody->mbo_valid |= OBD_MD_FLFLAGS;
1692 repbody->mbo_flags = OBD_FL_FLUSH;
1696 /* read file data to the buffer */
1697 int mdt_dom_read_on_open(struct mdt_thread_info *mti, struct mdt_device *mdt,
1698 struct lustre_handle *lh)
1700 const struct lu_env *env = mti->mti_env;
1701 struct tgt_session_info *tsi = tgt_ses_info(env);
1702 struct req_capsule *pill = tsi->tsi_pill;
1703 const struct lu_fid *fid;
1704 struct ptlrpc_request *req = tgt_ses_req(tsi);
1705 struct mdt_body *mbo;
1706 struct dt_device *dt = mdt->mdt_bottom;
1707 struct dt_object *mo;
1709 struct niobuf_remote *rnb = NULL;
1710 struct niobuf_local *lnb;
1713 unsigned int len, copied = 0;
1714 __u64 real_dom_size;
1715 int lnbs, nr_local, i;
1716 bool dom_lock = false;
1720 if (!req_capsule_field_present(pill, &RMF_NIOBUF_INLINE, RCL_SERVER)) {
1721 /* There is no reply buffers for this field, this means that
1722 * client has no support for data in reply.
1727 mbo = req_capsule_server_get(pill, &RMF_MDT_BODY);
1728 if (!(mbo->mbo_valid & OBD_MD_DOM_SIZE))
1731 if (!mbo->mbo_dom_size)
1734 if (lustre_handle_is_used(lh)) {
1735 struct ldlm_lock *lock;
1737 lock = ldlm_handle2lock(lh);
1739 dom_lock = ldlm_has_dom(lock) && ldlm_has_layout(lock);
1740 LDLM_LOCK_PUT(lock);
1744 /* return data along with open only along with DoM lock */
1745 if (!dom_lock || !mdt->mdt_opts.mo_dom_read_open)
1748 /* if DoM object holds encrypted content, we need to make sure we
1749 * send whole encryption units, or client will read corrupted content
1751 if (mbo->mbo_valid & LA_FLAGS && mbo->mbo_flags & LUSTRE_ENCRYPT_FL &&
1752 mbo->mbo_dom_size & ~LUSTRE_ENCRYPTION_MASK)
1753 real_dom_size = (mbo->mbo_dom_size & LUSTRE_ENCRYPTION_MASK) +
1754 LUSTRE_ENCRYPTION_UNIT_SIZE;
1756 real_dom_size = mbo->mbo_dom_size;
1758 CDEBUG(D_INFO, "File size %llu, reply sizes %d/%d\n",
1759 real_dom_size, req->rq_reqmsg->lm_repsize, req->rq_replen);
1760 len = req->rq_reqmsg->lm_repsize - req->rq_replen;
1762 /* NB: at this moment we have the following sizes:
1763 * - req->rq_replen: used data in reply
1764 * - req->rq_reqmsg->lm_repsize: total allocated reply buffer at client
1766 * Ideal case when file size fits in allocated reply buffer,
1767 * that mean we can return whole data in reply. We can also fit more
1768 * data up to max_reply_size in total reply size, but this will cause
1769 * re-allocation on client and resend with larger buffer. This is still
1770 * faster than separate READ IO.
1771 * Third case if file is too big to fit even in maximum size, in that
1772 * case we return just tail to optimize possible append.
1774 * At the moment the following strategy is used:
1775 * 1) try to fit into the buffer we have
1776 * 2) return just file tail otherwise.
1778 if (real_dom_size <= len) {
1779 /* can fit whole data */
1780 len = real_dom_size;
1782 } else if (real_dom_size <
1783 mdt_lmm_dom_stripesize(mti->mti_attr.ma_lmm)) {
1786 /* File tail offset must be aligned with larger page size
1787 * between client and server, so the maximum page size is
1788 * used here to align offset.
1790 * NB: DOM feature was introduced when server supports pagebits
1791 * already, so it should be always non-zero value. Report error
1792 * if it is not for some reason.
1794 if (!req->rq_export->exp_target_data.ted_pagebits) {
1795 CERROR("%s: client page bits are not saved on server\n",
1799 pgbits = max_t(int, PAGE_SHIFT,
1800 req->rq_export->exp_target_data.ted_pagebits);
1801 tail = real_dom_size % (1 << pgbits);
1803 /* no partial tail or tail can't fit in reply */
1804 if (tail == 0 || len < tail)
1808 offset = real_dom_size - len;
1810 /* DOM stripe is fully written, so don't expect its tail
1811 * will be used by append.
1816 LASSERT((offset & ~PAGE_MASK) == 0);
1817 rc = req_capsule_server_grow(pill, &RMF_NIOBUF_INLINE,
1818 sizeof(*rnb) + len);
1820 /* failed to grow data buffer, just exit */
1821 GOTO(out, rc = -E2BIG);
1824 /* re-take MDT_BODY and NIOBUF_INLINE buffers after the buffer grow */
1825 mbo = req_capsule_server_get(pill, &RMF_MDT_BODY);
1826 fid = &mbo->mbo_fid1;
1827 if (!fid_is_sane(fid))
1828 GOTO(out, rc = -EINVAL);
1830 rnb = req_capsule_server_get(tsi->tsi_pill, &RMF_NIOBUF_INLINE);
1832 GOTO(out, rc = -EPROTO);
1834 buf = (char *)rnb + sizeof(*rnb);
1836 rnb->rnb_offset = offset;
1838 mo = dt_locate(env, dt, fid);
1840 GOTO(out_rnb, rc = PTR_ERR(mo));
1841 LASSERT(mo != NULL);
1843 dt_read_lock(env, mo, 0);
1844 if (!dt_object_exists(mo))
1845 GOTO(unlock, rc = -ENOENT);
1847 /* parse remote buffers to local buffers and prepare the latter */
1848 lnbs = (len >> PAGE_SHIFT) + 1;
1849 OBD_ALLOC_PTR_ARRAY(lnb, lnbs);
1851 GOTO(unlock, rc = -ENOMEM);
1853 rc = dt_bufs_get(env, mo, rnb, lnb, lnbs, 0);
1854 if (unlikely(rc < 0))
1856 LASSERT(rc <= lnbs);
1858 rc = dt_read_prep(env, mo, lnb, nr_local);
1861 /* copy data to the buffer finally */
1862 for (i = 0; i < nr_local; i++) {
1863 char *p = kmap(lnb[i].lnb_page);
1866 LASSERT(lnb[i].lnb_page_offset == 0);
1867 off = lnb[i].lnb_len & ~PAGE_MASK;
1869 memset(p + off, 0, PAGE_SIZE - off);
1871 memcpy(buf + (i << PAGE_SHIFT), p, lnb[i].lnb_len);
1872 kunmap(lnb[i].lnb_page);
1873 copied += lnb[i].lnb_len;
1876 CDEBUG(D_INFO, "Read %i (wanted %u) bytes from %llu\n", copied,
1879 CWARN("%s: read %i bytes for "DFID
1880 " but wanted %u, is size wrong?\n",
1881 tsi->tsi_exp->exp_obd->obd_name, copied,
1882 PFID(&tsi->tsi_fid), len);
1883 /* Ignore partially copied data */
1888 dt_bufs_put(env, mo, lnb, nr_local);
1890 OBD_FREE_PTR_ARRAY(lnb, lnbs);
1892 dt_read_unlock(env, mo);
1893 lu_object_put(env, &mo->do_lu);
1895 rnb->rnb_len = copied;
1897 /* Don't fail OPEN request if read-on-open is failed, but drop
1898 * a message in log about the error.
1901 CDEBUG(D_INFO, "Read-on-open is failed, rc = %d", rc);
1907 * Completion AST for DOM discard locks:
1909 * CP AST an DOM discard lock is called always right after enqueue or from
1910 * reprocess if lock was blocked, in the latest case l_ast_data is set to
1911 * the mdt_object which is kept while there are pending locks on it.
1913 int ldlm_dom_discard_cp_ast(struct ldlm_lock *lock, __u64 flags, void *data)
1915 struct mdt_object *mo;
1916 struct lustre_handle dom_lh;
1921 /* l_ast_data is set when lock was not granted immediately
1922 * in mdt_dom_discard_data() below but put into waiting list,
1923 * so this CP callback means we are finished and corresponding
1924 * MDT object should be released finally as well as lock itself.
1926 lock_res_and_lock(lock);
1927 if (!lock->l_ast_data) {
1928 unlock_res_and_lock(lock);
1932 mo = lock->l_ast_data;
1933 lock->l_ast_data = NULL;
1934 unlock_res_and_lock(lock);
1936 ldlm_lock2handle(lock, &dom_lh);
1937 ldlm_lock_decref(&dom_lh, LCK_PW);
1939 env = lu_env_find();
1944 mdt_object_put(env, mo);
1949 void mdt_dom_discard_data(struct mdt_thread_info *info,
1950 struct mdt_object *mo)
1952 struct ptlrpc_request *req = mdt_info_req(info);
1953 struct mdt_device *mdt = mdt_dev(mo->mot_obj.lo_dev);
1954 union ldlm_policy_data policy;
1955 struct ldlm_res_id res_id;
1956 struct lustre_handle dom_lh;
1957 struct ldlm_lock *lock;
1958 __u64 flags = LDLM_FL_AST_DISCARD_DATA;
1964 if (req && req_is_replay(req))
1967 policy.l_inodebits.bits = MDS_INODELOCK_DOM;
1968 policy.l_inodebits.try_bits = 0;
1969 fid_build_reg_res_name(mdt_object_fid(mo), &res_id);
1971 /* Keep blocking version of discard for an old client to avoid
1972 * crashes on non-patched clients. LU-11359.
1974 old_client = req && !(exp_connect_flags2(req->rq_export) &
1975 OBD_CONNECT2_ASYNC_DISCARD);
1977 /* Tell the clients that the object is gone now and that they should
1978 * throw away any cached pages. */
1979 rc = ldlm_cli_enqueue_local(info->mti_env, mdt->mdt_namespace, &res_id,
1980 LDLM_IBITS, &policy, LCK_PW, &flags,
1981 ldlm_blocking_ast, old_client ?
1982 ldlm_completion_ast :
1983 ldlm_dom_discard_cp_ast,
1984 NULL, NULL, 0, LVB_T_NONE, NULL, &dom_lh);
1985 if (rc != ELDLM_OK) {
1987 "Failed to issue discard lock, rc = %d\n", rc);
1991 lock = ldlm_handle2lock(&dom_lh);
1992 lock_res_and_lock(lock);
1993 /* if lock is not granted then there are BL ASTs in progress and
1994 * lock will be granted in result of reprocessing with CP callback
1995 * notifying about that. The mdt object has to be kept until that and
1996 * it is saved in l_ast_data of the lock. Lock reference is kept too
1997 * until that to prevent it from canceling.
1999 if (!is_granted_or_cancelled_nolock(lock)) {
2000 mdt_object_get(info->mti_env, mo);
2001 lock->l_ast_data = mo;
2002 unlock_res_and_lock(lock);
2004 unlock_res_and_lock(lock);
2005 ldlm_lock_decref_and_cancel(&dom_lh, LCK_PW);
2007 LDLM_LOCK_PUT(lock);