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) 2013, 2014, Intel Corporation.
26 * lustre/osp/osp_md_object.c
28 * OST/MDT proxy device (OSP) Metadata methods
30 * This file implements methods for remote MD object, which include
31 * dt_object_operations, dt_index_operations and dt_body_operations.
33 * If there are multiple MDTs in one filesystem, one operation might
34 * include modifications in several MDTs. In such cases, clients
35 * send the RPC to the master MDT, then the operation is decomposed into
36 * object updates which will be dispatched to OSD or OSP. The local updates
37 * go to local OSD and the remote updates go to OSP. In OSP, these remote
38 * object updates will be packed into an update RPC, sent to the remote MDT
39 * and handled by Object Update Target (OUT).
41 * In DNE phase I, because of missing complete recovery solution, updates
42 * will be executed in order and synchronously.
43 * 1. The transaction is created.
44 * 2. In transaction declare, it collects and packs remote
45 * updates (in osp_md_declare_xxx()).
46 * 3. In transaction start, it sends these remote updates
47 * to remote MDTs, which will execute these updates synchronously.
48 * 4. In transaction execute phase, the local updates will be executed
51 * Author: Di Wang <di.wang@intel.com>
54 #define DEBUG_SUBSYSTEM S_MDS
56 #include <lustre_log.h>
57 #include "osp_internal.h"
59 static const char dot[] = ".";
60 static const char dotdot[] = "..";
63 * Add OUT_CREATE sub-request into the OUT RPC.
65 * Note: if the object has already been created, we must add object
66 * destroy sub-request ahead of the create, so it will destroy then
67 * re-create the object.
69 * \param[in] env execution environment
70 * \param[in] dt object to be created
71 * \param[in] attr attribute of the created object
72 * \param[in] hint creation hint
73 * \param[in] dof creation format information
74 * \param[in] th the transaction handle
76 * \retval only return 0 for now
78 static int __osp_md_declare_object_create(const struct lu_env *env,
81 struct dt_allocation_hint *hint,
82 struct dt_object_format *dof,
85 struct dt_update_request *update;
88 update = dt_update_request_find_or_create(th, dt);
90 CERROR("%s: Get OSP update buf failed: rc = %d\n",
91 dt->do_lu.lo_dev->ld_obd->obd_name,
92 (int)PTR_ERR(update));
93 return PTR_ERR(update);
96 if (lu_object_exists(&dt->do_lu)) {
97 /* If the object already exists, we needs to destroy
98 * this orphan object first.
100 * The scenario might happen in this case
102 * 1. client send remote create to MDT0.
103 * 2. MDT0 send create update to MDT1.
104 * 3. MDT1 finished create synchronously.
105 * 4. MDT0 failed and reboot.
106 * 5. client resend remote create to MDT0.
107 * 6. MDT0 tries to resend create update to MDT1,
108 * but find the object already exists
110 CDEBUG(D_HA, "%s: object "DFID" exists, destroy this orphan\n",
111 dt->do_lu.lo_dev->ld_obd->obd_name,
112 PFID(lu_object_fid(&dt->do_lu)));
114 rc = out_ref_del_pack(env, &update->dur_buf,
115 lu_object_fid(&dt->do_lu),
116 update->dur_batchid);
120 if (S_ISDIR(lu_object_attr(&dt->do_lu))) {
121 /* decrease for ".." */
122 rc = out_ref_del_pack(env, &update->dur_buf,
123 lu_object_fid(&dt->do_lu),
124 update->dur_batchid);
129 rc = out_object_destroy_pack(env, &update->dur_buf,
130 lu_object_fid(&dt->do_lu),
131 update->dur_batchid);
135 dt->do_lu.lo_header->loh_attr &= ~LOHA_EXISTS;
136 /* Increase batchid to add this orphan object deletion
137 * to separate transaction */
138 update_inc_batchid(update);
141 rc = out_create_pack(env, &update->dur_buf,
142 lu_object_fid(&dt->do_lu), attr, hint, dof,
143 update->dur_batchid);
148 CERROR("%s: Insert update error: rc = %d\n",
149 dt->do_lu.lo_dev->ld_obd->obd_name, rc);
155 * Implementation of dt_object_operations::do_declare_create
157 * For non-remote transaction, it will add an OUT_CREATE sub-request
158 * into the OUT RPC that will be flushed when the transaction start.
160 * \param[in] env execution environment
161 * \param[in] dt remote object to be created
162 * \param[in] attr attribute of the created object
163 * \param[in] hint creation hint
164 * \param[in] dof creation format information
165 * \param[in] th the transaction handle
167 * \retval 0 if the insertion succeeds.
168 * \retval negative errno if the insertion fails.
170 int osp_md_declare_object_create(const struct lu_env *env,
171 struct dt_object *dt,
172 struct lu_attr *attr,
173 struct dt_allocation_hint *hint,
174 struct dt_object_format *dof,
179 if (!is_only_remote_trans(th)) {
180 rc = __osp_md_declare_object_create(env, dt, attr, hint,
183 CDEBUG(D_INFO, "declare create md_object "DFID": rc = %d\n",
184 PFID(&dt->do_lu.lo_header->loh_fid), rc);
191 * Implementation of dt_object_operations::do_create
193 * For remote transaction, it will add an OUT_CREATE sub-request into
194 * the OUT RPC that will be flushed when the transaction stop.
196 * It sets necessary flags for created object. In DNE phase I,
197 * remote updates are actually executed during transaction start,
198 * i.e. the object has already been created when calling this method.
200 * \param[in] env execution environment
201 * \param[in] dt object to be created
202 * \param[in] attr attribute of the created object
203 * \param[in] hint creation hint
204 * \param[in] dof creation format information
205 * \param[in] th the transaction handle
207 * \retval only return 0 for now
209 int osp_md_object_create(const struct lu_env *env, struct dt_object *dt,
210 struct lu_attr *attr, struct dt_allocation_hint *hint,
211 struct dt_object_format *dof, struct thandle *th)
215 if (is_only_remote_trans(th)) {
216 rc = __osp_md_declare_object_create(env, dt, attr, hint,
219 CDEBUG(D_INFO, "create md_object "DFID": rc = %d\n",
220 PFID(&dt->do_lu.lo_header->loh_fid), rc);
224 dt->do_lu.lo_header->loh_attr |= LOHA_EXISTS |
225 (attr->la_mode & S_IFMT);
226 dt2osp_obj(dt)->opo_non_exist = 0;
233 * Add OUT_REF_DEL sub-request into the OUT RPC.
235 * \param[in] env execution environment
236 * \param[in] dt object to decrease the reference count.
237 * \param[in] th the transaction handle of refcount decrease.
239 * \retval 0 if the insertion succeeds.
240 * \retval negative errno if the insertion fails.
242 static int __osp_md_ref_del(const struct lu_env *env, struct dt_object *dt,
245 struct dt_update_request *update;
248 update = dt_update_request_find_or_create(th, dt);
249 if (IS_ERR(update)) {
250 CERROR("%s: Get OSP update buf failed: rc = %d\n",
251 dt->do_lu.lo_dev->ld_obd->obd_name,
252 (int)PTR_ERR(update));
253 return PTR_ERR(update);
256 rc = out_ref_del_pack(env, &update->dur_buf,
257 lu_object_fid(&dt->do_lu),
258 update->dur_batchid);
263 * Implementation of dt_object_operations::do_declare_ref_del
265 * For non-remote transaction, it will add an OUT_REF_DEL sub-request
266 * into the OUT RPC that will be flushed when the transaction start.
268 * \param[in] env execution environment
269 * \param[in] dt object to decrease the reference count.
270 * \param[in] th the transaction handle of refcount decrease.
272 * \retval 0 if the insertion succeeds.
273 * \retval negative errno if the insertion fails.
275 static int osp_md_declare_ref_del(const struct lu_env *env,
276 struct dt_object *dt, struct thandle *th)
280 if (!is_only_remote_trans(th)) {
281 rc = __osp_md_ref_del(env, dt, th);
283 CDEBUG(D_INFO, "declare ref del "DFID": rc = %d\n",
284 PFID(&dt->do_lu.lo_header->loh_fid), rc);
291 * Implementation of dt_object_operations::do_ref_del
293 * For remote transaction, it will add an OUT_REF_DEL sub-request into
294 * the OUT RPC that will be flushed when the transaction stop.
296 * \param[in] env execution environment
297 * \param[in] dt object to decrease the reference count
298 * \param[in] th the transaction handle
300 * \retval only return 0 for now
302 static int osp_md_ref_del(const struct lu_env *env, struct dt_object *dt,
307 if (is_only_remote_trans(th)) {
308 rc = __osp_md_ref_del(env, dt, th);
310 CDEBUG(D_INFO, "ref del "DFID": rc = %d\n",
311 PFID(&dt->do_lu.lo_header->loh_fid), rc);
318 * Add OUT_REF_ADD sub-request into the OUT RPC.
320 * \param[in] env execution environment
321 * \param[in] dt object on which to increase the reference count.
322 * \param[in] th the transaction handle.
324 * \retval 0 if the insertion succeeds.
325 * \retval negative errno if the insertion fails.
327 static int __osp_md_ref_add(const struct lu_env *env, struct dt_object *dt,
330 struct dt_update_request *update;
333 update = dt_update_request_find_or_create(th, dt);
334 if (IS_ERR(update)) {
335 CERROR("%s: Get OSP update buf failed: rc = %d\n",
336 dt->do_lu.lo_dev->ld_obd->obd_name,
337 (int)PTR_ERR(update));
338 return PTR_ERR(update);
341 rc = out_ref_add_pack(env, &update->dur_buf,
342 lu_object_fid(&dt->do_lu),
343 update->dur_batchid);
349 * Implementation of dt_object_operations::do_declare_ref_del
351 * For non-remote transaction, it will add an OUT_REF_ADD sub-request
352 * into the OUT RPC that will be flushed when the transaction start.
354 * \param[in] env execution environment
355 * \param[in] dt object on which to increase the reference count.
356 * \param[in] th the transaction handle.
358 * \retval 0 if the insertion succeeds.
359 * \retval negative errno if the insertion fails.
361 static int osp_md_declare_ref_add(const struct lu_env *env,
362 struct dt_object *dt, struct thandle *th)
366 if (!is_only_remote_trans(th)) {
367 rc = __osp_md_ref_add(env, dt, th);
369 CDEBUG(D_INFO, "declare ref add "DFID": rc = %d\n",
370 PFID(&dt->do_lu.lo_header->loh_fid), rc);
377 * Implementation of dt_object_operations::do_ref_add
379 * For remote transaction, it will add an OUT_REF_ADD sub-request into
380 * the OUT RPC that will be flushed when the transaction stop.
382 * \param[in] env execution environment
383 * \param[in] dt object on which to increase the reference count
384 * \param[in] th the transaction handle
386 * \retval only return 0 for now
388 static int osp_md_ref_add(const struct lu_env *env, struct dt_object *dt,
393 if (is_only_remote_trans(th)) {
394 rc = __osp_md_ref_add(env, dt, th);
396 CDEBUG(D_INFO, "ref add "DFID": rc = %d\n",
397 PFID(&dt->do_lu.lo_header->loh_fid), rc);
404 * Implementation of dt_object_operations::do_ah_init
406 * Initialize the allocation hint for object creation, which is usually called
407 * before the creation, and these hints (parent and child mode) will be sent to
408 * the remote Object Update Target (OUT) and used in the object create process,
409 * same as OSD object creation.
411 * \param[in] env execution environment
412 * \param[in] ah the hint to be initialized
413 * \param[in] parent the parent of the object
414 * \param[in] child the object to be created
415 * \param[in] child_mode the mode of the created object
417 static void osp_md_ah_init(const struct lu_env *env,
418 struct dt_allocation_hint *ah,
419 struct dt_object *parent,
420 struct dt_object *child,
425 ah->dah_parent = parent;
426 ah->dah_mode = child_mode;
430 * Add OUT_ATTR_SET sub-request into the OUT RPC.
432 * \param[in] env execution environment
433 * \param[in] dt object on which to set attributes
434 * \param[in] attr attributes to be set
435 * \param[in] th the transaction handle
437 * \retval 0 if the insertion succeeds.
438 * \retval negative errno if the insertion fails.
440 int __osp_md_attr_set(const struct lu_env *env, struct dt_object *dt,
441 const struct lu_attr *attr, struct thandle *th)
443 struct dt_update_request *update;
446 update = dt_update_request_find_or_create(th, dt);
447 if (IS_ERR(update)) {
448 CERROR("%s: Get OSP update buf failed: %d\n",
449 dt->do_lu.lo_dev->ld_obd->obd_name,
450 (int)PTR_ERR(update));
451 return PTR_ERR(update);
454 rc = out_attr_set_pack(env, &update->dur_buf,
455 lu_object_fid(&dt->do_lu), attr,
456 update->dur_batchid);
462 * Implementation of dt_object_operations::do_declare_attr_get
464 * Declare setting attributes to the specified remote object.
466 * If the transaction is a non-remote transaction, then add the OUT_ATTR_SET
467 * sub-request into the OUT RPC that will be flushed when the transaction start.
469 * \param[in] env execution environment
470 * \param[in] dt object on which to set attributes
471 * \param[in] attr attributes to be set
472 * \param[in] th the transaction handle
474 * \retval 0 if the insertion succeeds.
475 * \retval negative errno if the insertion fails.
477 int osp_md_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
478 const struct lu_attr *attr, struct thandle *th)
482 if (!is_only_remote_trans(th)) {
483 rc = __osp_md_attr_set(env, dt, attr, th);
485 CDEBUG(D_INFO, "declare attr set md_object "DFID": rc = %d\n",
486 PFID(&dt->do_lu.lo_header->loh_fid), rc);
493 * Implementation of dt_object_operations::do_attr_set
495 * Set attributes to the specified remote object.
497 * If the transaction is a remote transaction, then add the OUT_ATTR_SET
498 * sub-request into the OUT RPC that will be flushed when the transaction stop.
500 * \param[in] env execution environment
501 * \param[in] dt object to set attributes
502 * \param[in] attr attributes to be set
503 * \param[in] th the transaction handle
505 * \retval only return 0 for now
507 int osp_md_attr_set(const struct lu_env *env, struct dt_object *dt,
508 const struct lu_attr *attr, struct thandle *th)
512 if (is_only_remote_trans(th)) {
513 rc = __osp_md_attr_set(env, dt, attr, th);
515 CDEBUG(D_INFO, "attr set md_object "DFID": rc = %d\n",
516 PFID(&dt->do_lu.lo_header->loh_fid), rc);
523 * Implementation of dt_object_operations::do_read_lock
525 * osp_md_object_{read,write}_lock() will only lock the remote object in the
526 * local cache, which uses the semaphore (opo_sem) inside the osp_object to
527 * lock the object. Note: it will not lock the object in the whole cluster,
528 * which relies on the LDLM lock.
530 * \param[in] env execution environment
531 * \param[in] dt object to be locked
532 * \param[in] role lock role from MDD layer, see mdd_object_role().
534 static void osp_md_object_read_lock(const struct lu_env *env,
535 struct dt_object *dt, unsigned role)
537 struct osp_object *obj = dt2osp_obj(dt);
539 LASSERT(obj->opo_owner != env);
540 down_read_nested(&obj->opo_sem, role);
542 LASSERT(obj->opo_owner == NULL);
546 * Implementation of dt_object_operations::do_write_lock
548 * Lock the remote object in write mode.
550 * \param[in] env execution environment
551 * \param[in] dt object to be locked
552 * \param[in] role lock role from MDD layer, see mdd_object_role().
554 static void osp_md_object_write_lock(const struct lu_env *env,
555 struct dt_object *dt, unsigned role)
557 struct osp_object *obj = dt2osp_obj(dt);
559 down_write_nested(&obj->opo_sem, role);
561 LASSERT(obj->opo_owner == NULL);
562 obj->opo_owner = env;
566 * Implementation of dt_object_operations::do_read_unlock
568 * Unlock the read lock of remote object.
570 * \param[in] env execution environment
571 * \param[in] dt object to be unlocked
573 static void osp_md_object_read_unlock(const struct lu_env *env,
574 struct dt_object *dt)
576 struct osp_object *obj = dt2osp_obj(dt);
578 up_read(&obj->opo_sem);
582 * Implementation of dt_object_operations::do_write_unlock
584 * Unlock the write lock of remote object.
586 * \param[in] env execution environment
587 * \param[in] dt object to be unlocked
589 static void osp_md_object_write_unlock(const struct lu_env *env,
590 struct dt_object *dt)
592 struct osp_object *obj = dt2osp_obj(dt);
594 LASSERT(obj->opo_owner == env);
595 obj->opo_owner = NULL;
596 up_write(&obj->opo_sem);
600 * Implementation of dt_object_operations::do_write_locked
602 * Test if the object is locked in write mode.
604 * \param[in] env execution environment
605 * \param[in] dt object to be tested
607 static int osp_md_object_write_locked(const struct lu_env *env,
608 struct dt_object *dt)
610 struct osp_object *obj = dt2osp_obj(dt);
612 return obj->opo_owner == env;
616 * Implementation of dt_index_operations::dio_lookup
618 * Look up record by key under a remote index object. It packs lookup update
619 * into RPC, sends to the remote OUT and waits for the lookup result.
621 * \param[in] env execution environment
622 * \param[in] dt index object to lookup
623 * \param[out] rec record in which to return lookup result
624 * \param[in] key key of index which will be looked up
626 * \retval 1 if the lookup succeeds.
627 * \retval negative errno if the lookup fails.
629 static int osp_md_index_lookup(const struct lu_env *env, struct dt_object *dt,
630 struct dt_rec *rec, const struct dt_key *key)
632 struct lu_buf *lbuf = &osp_env_info(env)->osi_lb2;
633 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
634 struct dt_device *dt_dev = &osp->opd_dt_dev;
635 struct dt_update_request *update;
636 struct object_update_reply *reply;
637 struct ptlrpc_request *req = NULL;
642 /* Because it needs send the update buffer right away,
643 * just create an update buffer, instead of attaching the
644 * update_remote list of the thandle.
646 update = dt_update_request_create(dt_dev);
648 RETURN(PTR_ERR(update));
650 rc = out_index_lookup_pack(env, &update->dur_buf,
651 lu_object_fid(&dt->do_lu), rec, key);
653 CERROR("%s: Insert update error: rc = %d\n",
654 dt_dev->dd_lu_dev.ld_obd->obd_name, rc);
658 rc = osp_remote_sync(env, osp, update, &req, false);
662 reply = req_capsule_server_sized_get(&req->rq_pill,
663 &RMF_OUT_UPDATE_REPLY,
664 OUT_UPDATE_REPLY_SIZE);
665 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
666 CERROR("%s: Wrong version %x expected %x: rc = %d\n",
667 dt_dev->dd_lu_dev.ld_obd->obd_name,
668 reply->ourp_magic, UPDATE_REPLY_MAGIC, -EPROTO);
669 GOTO(out, rc = -EPROTO);
672 rc = object_update_result_data_get(reply, lbuf, 0);
676 if (lbuf->lb_len != sizeof(*fid)) {
677 CERROR("%s: lookup "DFID" %s wrong size %d\n",
678 dt_dev->dd_lu_dev.ld_obd->obd_name,
679 PFID(lu_object_fid(&dt->do_lu)), (char *)key,
681 GOTO(out, rc = -EINVAL);
685 if (ptlrpc_rep_need_swab(req))
686 lustre_swab_lu_fid(fid);
687 if (!fid_is_sane(fid)) {
688 CERROR("%s: lookup "DFID" %s invalid fid "DFID"\n",
689 dt_dev->dd_lu_dev.ld_obd->obd_name,
690 PFID(lu_object_fid(&dt->do_lu)), (char *)key, PFID(fid));
691 GOTO(out, rc = -EINVAL);
694 memcpy(rec, fid, sizeof(*fid));
700 ptlrpc_req_finished(req);
702 dt_update_request_destroy(update);
708 * Add OUT_INDEX_INSERT sub-request into the OUT RPC.
710 * \param[in] env execution environment
711 * \param[in] dt object for which to insert index
712 * \param[in] rec record of the index which will be inserted
713 * \param[in] key key of the index which will be inserted
714 * \param[in] th the transaction handle
716 * \retval 0 if the insertion succeeds.
717 * \retval negative errno if the insertion fails.
719 static int __osp_md_index_insert(const struct lu_env *env,
720 struct dt_object *dt,
721 const struct dt_rec *rec,
722 const struct dt_key *key,
725 struct dt_update_request *update;
728 update = dt_update_request_find_or_create(th, dt);
729 if (IS_ERR(update)) {
730 CERROR("%s: Get OSP update buf failed: rc = %d\n",
731 dt->do_lu.lo_dev->ld_obd->obd_name,
732 (int)PTR_ERR(update));
733 return PTR_ERR(update);
736 rc = out_index_insert_pack(env, &update->dur_buf,
737 lu_object_fid(&dt->do_lu), rec, key,
738 update->dur_batchid);
743 * Implementation of dt_index_operations::dio_declare_insert
745 * For non-remote transaction, it will add an OUT_INDEX_INSERT sub-request
746 * into the OUT RPC that will be flushed when the transaction start.
748 * \param[in] env execution environment
749 * \param[in] dt object for which to insert index
750 * \param[in] rec record of the index which will be inserted
751 * \param[in] key key of the index which will be inserted
752 * \param[in] th the transaction handle
754 * \retval 0 if the insertion succeeds.
755 * \retval negative errno if the insertion fails.
757 static int osp_md_declare_index_insert(const struct lu_env *env,
758 struct dt_object *dt,
759 const struct dt_rec *rec,
760 const struct dt_key *key,
765 if (!is_only_remote_trans(th)) {
766 rc = __osp_md_index_insert(env, dt, rec, key, th);
768 CDEBUG(D_INFO, "declare index insert "DFID" key %s, rec "DFID
769 ": rc = %d\n", PFID(&dt->do_lu.lo_header->loh_fid),
771 PFID(((struct dt_insert_rec *)rec)->rec_fid), rc);
778 * Implementation of dt_index_operations::dio_insert
780 * For remote transaction, it will add an OUT_INDEX_INSERT sub-request
781 * into the OUT RPC that will be flushed when the transaction stop.
783 * \param[in] env execution environment
784 * \param[in] dt object for which to insert index
785 * \param[in] rec record of the index to be inserted
786 * \param[in] key key of the index to be inserted
787 * \param[in] th the transaction handle
788 * \param[in] ignore_quota quota enforcement for insert
790 * \retval only return 0 for now
792 static int osp_md_index_insert(const struct lu_env *env,
793 struct dt_object *dt,
794 const struct dt_rec *rec,
795 const struct dt_key *key,
801 if (is_only_remote_trans(th)) {
802 rc = __osp_md_index_insert(env, dt, rec, key, th);
804 CDEBUG(D_INFO, "index insert "DFID" key %s, rec "DFID
805 ": rc = %d\n", PFID(&dt->do_lu.lo_header->loh_fid),
807 PFID(((struct dt_insert_rec *)rec)->rec_fid), rc);
814 * Add OUT_INDEX_DELETE sub-request into the OUT RPC.
816 * \param[in] env execution environment
817 * \param[in] dt object for which to delete index
818 * \param[in] key key of the index
819 * \param[in] th the transaction handle
821 * \retval 0 if the insertion succeeds.
822 * \retval negative errno if the insertion fails.
824 static int __osp_md_index_delete(const struct lu_env *env,
825 struct dt_object *dt,
826 const struct dt_key *key,
829 struct dt_update_request *update;
832 update = dt_update_request_find_or_create(th, dt);
833 if (IS_ERR(update)) {
834 CERROR("%s: Get OSP update buf failed: rc = %d\n",
835 dt->do_lu.lo_dev->ld_obd->obd_name,
836 (int)PTR_ERR(update));
837 return PTR_ERR(update);
840 rc = out_index_delete_pack(env, &update->dur_buf,
841 lu_object_fid(&dt->do_lu), key,
842 update->dur_batchid);
847 * Implementation of dt_index_operations::dio_declare_delete
849 * For non-remote transaction, it will add an OUT_INDEX_DELETE sub-request
850 * into the OUT RPC that will be flushed when the transaction start.
852 * \param[in] env execution environment
853 * \param[in] dt object for which to delete index
854 * \param[in] key key of the index
855 * \param[in] th the transaction handle
857 * \retval 0 if the insertion succeeds.
858 * \retval negative errno if the insertion fails.
860 static int osp_md_declare_index_delete(const struct lu_env *env,
861 struct dt_object *dt,
862 const struct dt_key *key,
867 if (!is_only_remote_trans(th)) {
868 rc = __osp_md_index_delete(env, dt, key, th);
870 CDEBUG(D_INFO, "declare index delete "DFID" %s: rc = %d\n",
871 PFID(&dt->do_lu.lo_header->loh_fid), (char *)key, rc);
878 * Implementation of dt_index_operations::dio_delete
880 * For remote transaction, it will add an OUT_INDEX_DELETE sub-request
881 * into the OUT RPC that will be flushed when the transaction stop.
883 * \param[in] env execution environment
884 * \param[in] dt object for which to delete index
885 * \param[in] key key of the index which will be deleted
886 * \param[in] th the transaction handle
888 * \retval only return 0 for now
890 static int osp_md_index_delete(const struct lu_env *env,
891 struct dt_object *dt,
892 const struct dt_key *key,
897 if (is_only_remote_trans(th)) {
898 rc = __osp_md_index_delete(env, dt, key, th);
900 CDEBUG(D_INFO, "index delete "DFID" %s: rc = %d\n",
901 PFID(&dt->do_lu.lo_header->loh_fid), (char *)key, rc);
908 * Implementation of dt_index_operations::dio_it.next
910 * Advance the pointer of the iterator to the next entry. It shares a similar
911 * internal implementation with osp_orphan_it_next(), which is being used for
912 * remote orphan index object. This method will be used for remote directory.
914 * \param[in] env execution environment
915 * \param[in] di iterator of this iteration
917 * \retval 0 if the pointer is advanced successfuly.
918 * \retval 1 if it reaches to the end of the index object.
919 * \retval negative errno if the pointer cannot be advanced.
921 static int osp_md_index_it_next(const struct lu_env *env, struct dt_it *di)
923 struct osp_it *it = (struct osp_it *)di;
924 struct lu_idxpage *idxpage;
925 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
930 idxpage = it->ooi_cur_idxpage;
931 if (idxpage != NULL) {
932 if (idxpage->lip_nr == 0)
938 (struct lu_dirent *)idxpage->lip_entries;
940 } else if (le16_to_cpu(ent->lde_reclen) != 0 &&
941 it->ooi_pos_ent < idxpage->lip_nr) {
942 ent = (struct lu_dirent *)(((char *)ent) +
943 le16_to_cpu(ent->lde_reclen));
951 rc = osp_it_next_page(env, di);
959 * Implementation of dt_index_operations::dio_it.key
961 * Get the key at current iterator poisiton. These iteration methods
962 * (dio_it) will only be used for iterating the remote directory, so
963 * the key is the name of the directory entry.
965 * \param[in] env execution environment
966 * \param[in] di iterator of this iteration
968 * \retval name of the current entry
970 static struct dt_key *osp_it_key(const struct lu_env *env,
971 const struct dt_it *di)
973 struct osp_it *it = (struct osp_it *)di;
974 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
976 return (struct dt_key *)ent->lde_name;
980 * Implementation of dt_index_operations::dio_it.key_size
982 * Get the key size at current iterator poisiton. These iteration methods
983 * (dio_it) will only be used for iterating the remote directory, so the key
984 * size is the name size of the directory entry.
986 * \param[in] env execution environment
987 * \param[in] di iterator of this iteration
989 * \retval name size of the current entry
992 static int osp_it_key_size(const struct lu_env *env, const struct dt_it *di)
994 struct osp_it *it = (struct osp_it *)di;
995 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
997 return (int)le16_to_cpu(ent->lde_namelen);
1001 * Implementation of dt_index_operations::dio_it.rec
1003 * Get the record at current iterator position. These iteration methods
1004 * (dio_it) will only be used for iterating the remote directory, so it
1005 * uses lu_dirent_calc_size() to calculate the record size.
1007 * \param[in] env execution environment
1008 * \param[in] di iterator of this iteration
1009 * \param[out] rec the record to be returned
1010 * \param[in] attr attributes of the index object, so it knows
1011 * how to pack the entry.
1013 * \retval only return 0 for now
1015 static int osp_md_index_it_rec(const struct lu_env *env, const struct dt_it *di,
1016 struct dt_rec *rec, __u32 attr)
1018 struct osp_it *it = (struct osp_it *)di;
1019 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
1022 reclen = lu_dirent_calc_size(le16_to_cpu(ent->lde_namelen), attr);
1023 memcpy(rec, ent, reclen);
1028 * Implementation of dt_index_operations::dio_it.load
1030 * Locate the iteration cursor to the specified position (cookie).
1032 * \param[in] env pointer to the thread context
1033 * \param[in] di pointer to the iteration structure
1034 * \param[in] hash the specified position
1036 * \retval positive number for locating to the exactly position
1038 * \retval 0 for arriving at the end of the iteration
1039 * \retval negative error number on failure
1041 static int osp_it_load(const struct lu_env *env, const struct dt_it *di,
1044 struct osp_it *it = (struct osp_it *)di;
1047 it->ooi_next = hash;
1048 rc = osp_md_index_it_next(env, (struct dt_it *)di);
1058 const struct dt_index_operations osp_md_index_ops = {
1059 .dio_lookup = osp_md_index_lookup,
1060 .dio_declare_insert = osp_md_declare_index_insert,
1061 .dio_insert = osp_md_index_insert,
1062 .dio_declare_delete = osp_md_declare_index_delete,
1063 .dio_delete = osp_md_index_delete,
1065 .init = osp_it_init,
1066 .fini = osp_it_fini,
1069 .next = osp_md_index_it_next,
1071 .key_size = osp_it_key_size,
1072 .rec = osp_md_index_it_rec,
1073 .store = osp_it_store,
1074 .load = osp_it_load,
1075 .key_rec = osp_it_key_rec,
1080 * Implementation of dt_object_operations::do_index_try
1082 * Try to initialize the index API pointer for the given object. This
1083 * is the entry point of the index API, i.e. we must call this method
1084 * to initialize the index object before calling other index methods.
1086 * \param[in] env execution environment
1087 * \param[in] dt index object to be initialized
1088 * \param[in] feat the index feature of the object
1090 * \retval 0 if the initialization succeeds.
1091 * \retval negative errno if the initialization fails.
1093 static int osp_md_index_try(const struct lu_env *env,
1094 struct dt_object *dt,
1095 const struct dt_index_features *feat)
1097 dt->do_index_ops = &osp_md_index_ops;
1102 * Implementation of dt_object_operations::do_object_lock
1104 * Enqueue a lock (by ldlm_cli_enqueue()) of remote object on the remote MDT,
1105 * which will lock the object in the global namespace.
1107 * \param[in] env execution environment
1108 * \param[in] dt object to be locked
1109 * \param[out] lh lock handle
1110 * \param[in] einfo enqueue information
1111 * \param[in] policy lock policy
1113 * \retval ELDLM_OK if locking the object succeeds.
1114 * \retval negative errno if locking fails.
1116 static int osp_md_object_lock(const struct lu_env *env,
1117 struct dt_object *dt,
1118 struct lustre_handle *lh,
1119 struct ldlm_enqueue_info *einfo,
1120 ldlm_policy_data_t *policy)
1122 struct ldlm_res_id *res_id;
1123 struct dt_device *dt_dev = lu2dt_dev(dt->do_lu.lo_dev);
1124 struct osp_device *osp = dt2osp_dev(dt_dev);
1125 struct ptlrpc_request *req;
1130 res_id = einfo->ei_res_id;
1131 LASSERT(res_id != NULL);
1133 mode = ldlm_lock_match(osp->opd_obd->obd_namespace,
1134 LDLM_FL_BLOCK_GRANTED, res_id,
1135 einfo->ei_type, policy,
1136 einfo->ei_mode, lh, 0);
1140 req = ldlm_enqueue_pack(osp->opd_exp, 0);
1142 RETURN(PTR_ERR(req));
1144 rc = ldlm_cli_enqueue(osp->opd_exp, &req, einfo, res_id,
1145 (const ldlm_policy_data_t *)policy,
1146 &flags, NULL, 0, LVB_T_NONE, lh, 0);
1148 ptlrpc_req_finished(req);
1150 return rc == ELDLM_OK ? 0 : -EIO;
1154 * Implementation of dt_object_operations::do_object_unlock
1156 * Cancel a lock of a remote object.
1158 * \param[in] env execution environment
1159 * \param[in] dt object to be unlocked
1160 * \param[in] einfo lock enqueue information
1161 * \param[in] policy lock policy
1163 * \retval Only return 0 for now.
1165 static int osp_md_object_unlock(const struct lu_env *env,
1166 struct dt_object *dt,
1167 struct ldlm_enqueue_info *einfo,
1168 ldlm_policy_data_t *policy)
1170 struct lustre_handle *lockh = einfo->ei_cbdata;
1172 /* unlock finally */
1173 ldlm_lock_decref(lockh, einfo->ei_mode);
1178 struct dt_object_operations osp_md_obj_ops = {
1179 .do_read_lock = osp_md_object_read_lock,
1180 .do_write_lock = osp_md_object_write_lock,
1181 .do_read_unlock = osp_md_object_read_unlock,
1182 .do_write_unlock = osp_md_object_write_unlock,
1183 .do_write_locked = osp_md_object_write_locked,
1184 .do_declare_create = osp_md_declare_object_create,
1185 .do_create = osp_md_object_create,
1186 .do_declare_ref_add = osp_md_declare_ref_add,
1187 .do_ref_add = osp_md_ref_add,
1188 .do_declare_ref_del = osp_md_declare_ref_del,
1189 .do_ref_del = osp_md_ref_del,
1190 .do_declare_destroy = osp_declare_object_destroy,
1191 .do_destroy = osp_object_destroy,
1192 .do_ah_init = osp_md_ah_init,
1193 .do_attr_get = osp_attr_get,
1194 .do_declare_attr_set = osp_md_declare_attr_set,
1195 .do_attr_set = osp_md_attr_set,
1196 .do_xattr_get = osp_xattr_get,
1197 .do_declare_xattr_set = osp_declare_xattr_set,
1198 .do_xattr_set = osp_xattr_set,
1199 .do_declare_xattr_del = osp_declare_xattr_del,
1200 .do_xattr_del = osp_xattr_del,
1201 .do_index_try = osp_md_index_try,
1202 .do_object_lock = osp_md_object_lock,
1203 .do_object_unlock = osp_md_object_unlock,
1207 * Implementation of dt_body_operations::dbo_declare_write
1209 * Declare an object write. In DNE phase I, it will pack the write
1210 * object update into the RPC.
1212 * \param[in] env execution environment
1213 * \param[in] dt object to be written
1214 * \param[in] buf buffer to write which includes an embedded size field
1215 * \param[in] pos offet in the object to start writing at
1216 * \param[in] th transaction handle
1218 * \retval 0 if the insertion succeeds.
1219 * \retval negative errno if the insertion fails.
1221 static ssize_t osp_md_declare_write(const struct lu_env *env,
1222 struct dt_object *dt,
1223 const struct lu_buf *buf,
1224 loff_t pos, struct thandle *th)
1226 struct dt_update_request *update;
1229 update = dt_update_request_find_or_create(th, dt);
1230 if (IS_ERR(update)) {
1231 CERROR("%s: Get OSP update buf failed: rc = %d\n",
1232 dt->do_lu.lo_dev->ld_obd->obd_name,
1233 (int)PTR_ERR(update));
1234 return PTR_ERR(update);
1237 rc = out_write_pack(env, &update->dur_buf, lu_object_fid(&dt->do_lu),
1238 buf, pos, update->dur_batchid);
1245 * Implementation of dt_body_operations::dbo_write
1247 * Return the buffer size. In DNE phase I, remote updates
1248 * are actually executed during transaction start, the buffer has
1249 * already been written when this method is being called.
1251 * \param[in] env execution environment
1252 * \param[in] dt object to be written
1253 * \param[in] buf buffer to write which includes an embedded size field
1254 * \param[in] pos offet in the object to start writing at
1255 * \param[in] th transaction handle
1256 * \param[in] ignore_quota quota enforcement for this write
1258 * \retval the buffer size in bytes.
1260 static ssize_t osp_md_write(const struct lu_env *env, struct dt_object *dt,
1261 const struct lu_buf *buf, loff_t *pos,
1262 struct thandle *handle, int ignore_quota)
1264 *pos += buf->lb_len;
1268 /* These body operation will be used to write symlinks during migration etc */
1269 struct dt_body_operations osp_md_body_ops = {
1270 .dbo_declare_write = osp_md_declare_write,
1271 .dbo_write = osp_md_write,