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,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2014, Intel Corporation.
26 * lustre/lod/lod_sub_object.c
28 * LOD sub object methods
30 * This file implements sub-object methods for LOD.
32 * LOD is Logic volume layer in the MDS stack, which will handle striping
33 * and distribute the update to different OSP/OSD. After directing the updates
34 * to one specific OSD/OSP, it also needs to do some thing before calling
35 * OSD/OSP API, for example recording updates for cross-MDT operation, get
36 * the next level transaction etc.
38 * Author: Di Wang <di.wang@intel.com>
41 #define DEBUG_SUBSYSTEM S_MDS
44 #include <obd_class.h>
45 #include <lustre_ver.h>
46 #include <obd_support.h>
47 #include <lprocfs_status.h>
49 #include <lustre_fid.h>
50 #include <lustre_param.h>
51 #include <md_object.h>
52 #include <lustre_linkea.h>
53 #include <lustre_log.h>
55 #include "lod_internal.h"
57 struct thandle *lod_sub_get_thandle(const struct lu_env *env,
59 const struct dt_object *sub_obj,
62 struct lod_device *lod = dt2lod_dev(th->th_dev);
63 struct top_thandle *tth;
64 struct thandle *sub_th;
65 int type = LU_SEQ_RANGE_ANY;
70 if (record_update != NULL)
71 *record_update = false;
73 if (th->th_top == NULL)
76 tth = container_of(th, struct top_thandle, tt_super);
78 /* local object must be mdt object, Note: during ost object
79 * creation, FID is not assigned until osp_object_create(),
80 * so if the FID of sub_obj is zero, it means OST object. */
81 if (!dt_object_remote(sub_obj) ||
82 fid_is_zero(lu_object_fid(&sub_obj->do_lu))) {
83 /* local MDT object */
84 if (fid_is_sane(lu_object_fid(&sub_obj->do_lu)) &&
85 tth->tt_multiple_thandle != NULL &&
86 record_update != NULL &&
88 *record_update = true;
90 RETURN(tth->tt_master_sub_thandle);
93 rc = lod_fld_lookup(env, lod, lu_object_fid(&sub_obj->do_lu),
98 if (type == LU_SEQ_RANGE_OST)
99 RETURN(tth->tt_master_sub_thandle);
101 sub_th = thandle_get_sub(env, th, sub_obj);
105 if (tth->tt_multiple_thandle != NULL && record_update != NULL &&
107 *record_update = true;
113 * Declare sub-object creation.
115 * Get transaction of next layer and declare the creation of the object.
117 * \param[in] env execution environment
118 * \param[in] dt the object being created
119 * \param[in] attr the attributes of the object being created
120 * \param[in] hint the hint of the creation
121 * \param[in] dof the object format of the creation
122 * \param[th] th the transaction handle
124 * \retval 0 if the declaration succeeds
125 * \retval negative errno if the declaration fails.
127 int lod_sub_object_declare_create(const struct lu_env *env,
128 struct dt_object *dt,
129 struct lu_attr *attr,
130 struct dt_allocation_hint *hint,
131 struct dt_object_format *dof,
134 struct thandle *sub_th;
137 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
139 return PTR_ERR(sub_th);
142 update_record_size(env, create, th, lu_object_fid(&dt->do_lu),
145 return dt_declare_create(env, dt, attr, hint, dof, sub_th);
151 * Get transaction of next layer, record updates if it belongs to cross-MDT
152 * operation, and create the object.
154 * \param[in] env execution environment
155 * \param[in] dt the object being created
156 * \param[in] attr the attributes of the object being created
157 * \param[in] hint the hint of the creation
158 * \param[in] dof the object format of the creation
159 * \param[th] th the transaction handle
161 * \retval 0 if the creation succeeds
162 * \retval negative errno if the creation fails.
164 int lod_sub_object_create(const struct lu_env *env, struct dt_object *dt,
165 struct lu_attr *attr,
166 struct dt_allocation_hint *hint,
167 struct dt_object_format *dof,
170 struct thandle *sub_th;
175 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
177 RETURN(PTR_ERR(sub_th));
180 rc = update_record_pack(create, th,
181 lu_object_fid(&dt->do_lu),
187 rc = dt_create(env, dt, attr, hint, dof, sub_th);
193 * Declare adding reference for the sub-object
195 * Get transaction of next layer and declare the reference adding.
197 * \param[in] env execution environment
198 * \param[in] dt dt object to add reference
199 * \param[in] th transaction handle
201 * \retval 0 if the declaration succeeds.
202 * \retval negative errno if the declaration fails.
204 int lod_sub_object_declare_ref_add(const struct lu_env *env,
205 struct dt_object *dt,
208 struct thandle *sub_th;
213 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
215 RETURN(PTR_ERR(sub_th));
218 update_record_size(env, ref_add, th, lu_object_fid(&dt->do_lu));
220 rc = dt_declare_ref_add(env, dt, sub_th);
226 * Add reference for the sub-object
228 * Get transaction of next layer, record updates if it belongs to cross-MDT
229 * operation and add reference of the object.
231 * \param[in] env execution environment
232 * \param[in] dt dt object to add reference
233 * \param[in] th transaction handle
235 * \retval 0 if it succeeds.
236 * \retval negative errno if it fails.
238 int lod_sub_object_ref_add(const struct lu_env *env, struct dt_object *dt,
241 struct thandle *sub_th;
246 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
248 RETURN(PTR_ERR(sub_th));
251 rc = update_record_pack(ref_add, th,
252 lu_object_fid(&dt->do_lu));
257 rc = dt_ref_add(env, dt, sub_th);
263 * Declare deleting reference for the sub-object
265 * Get transaction of next layer and declare the reference deleting.
267 * \param[in] env execution environment
268 * \param[in] dt dt object to delete reference
269 * \param[in] th transaction handle
271 * \retval 0 if the declaration succeeds.
272 * \retval negative errno if the declaration fails.
274 int lod_sub_object_declare_ref_del(const struct lu_env *env,
275 struct dt_object *dt,
278 struct thandle *sub_th;
283 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
285 RETURN(PTR_ERR(sub_th));
288 update_record_size(env, ref_del, th, lu_object_fid(&dt->do_lu));
290 rc = dt_declare_ref_del(env, dt, sub_th);
296 * Delete reference for the sub-object
298 * Get transaction of next layer, record updates if it belongs to cross-MDT
299 * operation and delete reference of the object.
301 * \param[in] env execution environment
302 * \param[in] dt dt object to delete reference
303 * \param[in] th transaction handle
305 * \retval 0 if it succeeds.
306 * \retval negative errno if it fails.
308 int lod_sub_object_ref_del(const struct lu_env *env, struct dt_object *dt,
311 struct thandle *sub_th;
316 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
318 RETURN(PTR_ERR(sub_th));
321 rc = update_record_pack(ref_del, th,
322 lu_object_fid(&dt->do_lu));
327 rc = dt_ref_del(env, dt, sub_th);
333 * Declare destroying sub-object
335 * Get transaction of next layer and declare the sub-object destroy.
337 * \param[in] env execution environment
338 * \param[in] dt dt object to be destroyed
339 * \param[in] th transaction handle
341 * \retval 0 if the declaration succeeds.
342 * \retval negative errno if the declaration fails.
344 int lod_sub_object_declare_destroy(const struct lu_env *env,
345 struct dt_object *dt,
348 struct thandle *sub_th;
353 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
355 RETURN(PTR_ERR(sub_th));
358 update_record_size(env, object_destroy, th,
359 lu_object_fid(&dt->do_lu));
361 rc = dt_declare_destroy(env, dt, sub_th);
369 * Get transaction of next layer, record updates if it belongs to cross-MDT
370 * operation and destroy the object.
372 * \param[in] env execution environment
373 * \param[in] dt dt object to be destroyed
374 * \param[in] th transaction handle
376 * \retval 0 if the destroy succeeds.
377 * \retval negative errno if the destroy fails.
379 int lod_sub_object_destroy(const struct lu_env *env, struct dt_object *dt,
382 struct thandle *sub_th;
387 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
389 RETURN(PTR_ERR(sub_th));
392 rc = update_record_pack(object_destroy, th,
393 lu_object_fid(&dt->do_lu));
398 rc = dt_destroy(env, dt, sub_th);
404 * Declare sub-object index insert
406 * Get transaction of next layer and declare index insert.
408 * \param[in] env execution environment
409 * \param[in] dt object for which to insert index
410 * \param[in] rec record of the index which will be inserted
411 * \param[in] key key of the index which will be inserted
412 * \param[in] th the transaction handle
414 * \retval 0 if the declaration succeeds.
415 * \retval negative errno if the declaration fails.
417 int lod_sub_object_declare_insert(const struct lu_env *env,
418 struct dt_object *dt,
419 const struct dt_rec *rec,
420 const struct dt_key *key,
423 struct thandle *sub_th;
426 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
428 return PTR_ERR(sub_th);
431 update_record_size(env, index_insert, th,
432 lu_object_fid(&dt->do_lu), rec, key);
434 return dt_declare_insert(env, dt, rec, key, sub_th);
438 * Insert index of sub object
440 * Get transaction of next layer, record updates if it belongs to cross-MDT
441 * operation, and insert the index.
443 * \param[in] env execution environment
444 * \param[in] dt object for which to insert index
445 * \param[in] rec record of the index to be inserted
446 * \param[in] key key of the index to be inserted
447 * \param[in] th the transaction handle
448 * \param[in] ign whether ignore quota
450 * \retval 0 if the insertion succeeds.
451 * \retval negative errno if the insertion fails.
453 int lod_sub_object_index_insert(const struct lu_env *env, struct dt_object *dt,
454 const struct dt_rec *rec,
455 const struct dt_key *key, struct thandle *th,
458 struct thandle *sub_th;
462 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
464 return PTR_ERR(sub_th);
467 rc = update_record_pack(index_insert, th,
468 lu_object_fid(&dt->do_lu), rec, key);
473 return dt_insert(env, dt, rec, key, sub_th, ign);
477 * Declare sub-object index delete
479 * Get transaction of next layer and declare index deletion.
481 * \param[in] env execution environment
482 * \param[in] dt object for which to delete index
483 * \param[in] key key of the index which will be deleted
484 * \param[in] th the transaction handle
486 * \retval 0 if the declaration succeeds.
487 * \retval negative errno if the declaration fails.
489 int lod_sub_object_declare_delete(const struct lu_env *env,
490 struct dt_object *dt,
491 const struct dt_key *key,
494 struct thandle *sub_th;
497 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
499 return PTR_ERR(sub_th);
502 update_record_size(env, index_delete, th,
503 lu_object_fid(&dt->do_lu), key);
505 return dt_declare_delete(env, dt, key, sub_th);
509 * Delete index of sub object
511 * Get transaction of next layer, record updates if it belongs to cross-MDT
512 * operation, and delete the index.
514 * \param[in] env execution environment
515 * \param[in] dt object for which to delete index
516 * \param[in] key key of the index to be deleted
517 * \param[in] th the transaction handle
519 * \retval 0 if the deletion succeeds.
520 * \retval negative errno if the deletion fails.
522 int lod_sub_object_delete(const struct lu_env *env, struct dt_object *dt,
523 const struct dt_key *name, struct thandle *th)
525 struct thandle *sub_th;
530 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
532 RETURN(PTR_ERR(sub_th));
535 rc = update_record_pack(index_delete, th,
536 lu_object_fid(&dt->do_lu), name);
541 rc = dt_delete(env, dt, name, sub_th);
548 * Get transaction of next layer, and declare xattr set.
550 * \param[in] env execution environment
551 * \param[in] dt object on which to set xattr
552 * \param[in] buf xattr to be set
553 * \param[in] name name of the xattr
554 * \param[in] fl flag for setting xattr
556 * \retval 0 if the declaration succeeds.
557 * \retval negative errno if the declaration fails.
559 int lod_sub_object_declare_xattr_set(const struct lu_env *env,
560 struct dt_object *dt,
561 const struct lu_buf *buf,
562 const char *name, int fl,
565 struct thandle *sub_th;
570 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
572 RETURN(PTR_ERR(sub_th));
575 update_record_size(env, xattr_set, th,
576 lu_object_fid(&dt->do_lu),
579 rc = dt_declare_xattr_set(env, dt, buf, name, fl, sub_th);
587 * Get transaction of next layer, record updates if it belongs to cross-MDT
588 * operation, and set xattr to the object.
590 * \param[in] env execution environment
591 * \param[in] dt object on which to set xattr
592 * \param[in] buf xattr to be set
593 * \param[in] name name of the xattr
594 * \param[in] fl flag for setting xattr
595 * \param[in] th transaction handle
597 * \retval 0 if the xattr setting succeeds.
598 * \retval negative errno if xattr setting fails.
600 int lod_sub_object_xattr_set(const struct lu_env *env, struct dt_object *dt,
601 const struct lu_buf *buf, const char *name, int fl,
604 struct thandle *sub_th;
609 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
611 RETURN(PTR_ERR(sub_th));
614 rc = update_record_pack(xattr_set, th,
615 lu_object_fid(&dt->do_lu),
621 rc = dt_xattr_set(env, dt, buf, name, fl, sub_th);
629 * Get transaction of next layer, and declare attr set.
631 * \param[in] env execution environment
632 * \param[in] dt object on which to set attr
633 * \param[in] attr attributes to be set
634 * \param[in] th transaction handle
636 * \retval 0 if the declaration succeeds.
637 * \retval negative errno if the declaration fails.
639 int lod_sub_object_declare_attr_set(const struct lu_env *env,
640 struct dt_object *dt,
641 const struct lu_attr *attr,
644 struct thandle *sub_th;
649 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
651 RETURN(PTR_ERR(sub_th));
654 update_record_size(env, attr_set, th,
655 lu_object_fid(&dt->do_lu), attr);
657 rc = dt_declare_attr_set(env, dt, attr, sub_th);
665 * Get transaction of next layer, record updates if it belongs to cross-MDT
666 * operation, and set attributes to the object.
668 * \param[in] env execution environment
669 * \param[in] dt object on which to set attr
670 * \param[in] attr attrbutes to be set
671 * \param[in] th transaction handle
673 * \retval 0 if attributes setting succeeds.
674 * \retval negative errno if the attributes setting fails.
676 int lod_sub_object_attr_set(const struct lu_env *env,
677 struct dt_object *dt,
678 const struct lu_attr *attr,
682 struct thandle *sub_th;
686 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
688 RETURN(PTR_ERR(sub_th));
691 rc = update_record_pack(attr_set, th, lu_object_fid(&dt->do_lu),
697 rc = dt_attr_set(env, dt, attr, sub_th);
705 * Get transaction of next layer, and declare xattr deletion.
707 * \param[in] env execution environment
708 * \param[in] dt object on which to delete xattr
709 * \param[in] name name of the xattr to be deleted
710 * \param[in] th transaction handle
712 * \retval 0 if the declaration succeeds.
713 * \retval negative errno if the declaration fails.
715 int lod_sub_object_declare_xattr_del(const struct lu_env *env,
716 struct dt_object *dt,
720 struct thandle *sub_th;
725 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
727 RETURN(PTR_ERR(sub_th));
730 update_record_size(env, xattr_del, th,
731 lu_object_fid(&dt->do_lu),
734 rc = dt_declare_xattr_del(env, dt, name, sub_th);
740 * xattribute deletion
742 * Get transaction of next layer, record update if it belongs to cross-MDT
743 * operation and delete xattr.
745 * \param[in] env execution environment
746 * \param[in] dt object on which to delete xattr
747 * \param[in] name name of the xattr to be deleted
748 * \param[in] th transaction handle
750 * \retval 0 if the deletion succeeds.
751 * \retval negative errno if the deletion fails.
753 int lod_sub_object_xattr_del(const struct lu_env *env,
754 struct dt_object *dt,
758 struct thandle *sub_th;
763 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
765 RETURN(PTR_ERR(sub_th));
768 rc = update_record_pack(xattr_del, th,
769 lu_object_fid(&dt->do_lu), name);
774 rc = dt_xattr_del(env, dt, name, sub_th);
780 * Declare buffer write
782 * Get transaction of next layer and declare buffer write.
784 * \param[in] env execution environment
785 * \param[in] dt object to be written
786 * \param[in] buf buffer to write which includes an embedded size field
787 * \param[in] pos offet in the object to start writing at
788 * \param[in] th transaction handle
790 * \retval 0 if the insertion succeeds.
791 * \retval negative errno if the insertion fails.
793 int lod_sub_object_declare_write(const struct lu_env *env,
794 struct dt_object *dt,
795 const struct lu_buf *buf, loff_t pos,
798 struct thandle *sub_th;
803 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
805 RETURN(PTR_ERR(sub_th));
808 update_record_size(env, write, th,
809 lu_object_fid(&dt->do_lu),
812 rc = dt_declare_write(env, dt, buf, pos, sub_th);
818 * Write buffer to sub object
820 * Get transaction of next layer, records buffer write if it belongs to
821 * Cross-MDT operation, and write buffer.
823 * \param[in] env execution environment
824 * \param[in] dt object to be written
825 * \param[in] buf buffer to write which includes an embedded size field
826 * \param[in] pos offet in the object to start writing at
827 * \param[in] th transaction handle
828 * \param[in] rq enforcement for this write
830 * \retval the buffer size in bytes if it succeeds.
831 * \retval negative errno if it fails.
833 ssize_t lod_sub_object_write(const struct lu_env *env, struct dt_object *dt,
834 const struct lu_buf *buf, loff_t *pos,
835 struct thandle *th, int rq)
837 struct thandle *sub_th;
842 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
844 RETURN(PTR_ERR(sub_th));
847 rc = update_record_pack(write, th, lu_object_fid(&dt->do_lu),
853 rc = dt_write(env, dt, buf, pos, sub_th, rq);
860 * Get transaction of next layer and declare punch.
862 * \param[in] env execution environment
863 * \param[in] dt object to be written
864 * \param[in] start start offset of punch
865 * \param[in] end end offet of punch
866 * \param[in] th transaction handle
868 * \retval 0 if the insertion succeeds.
869 * \retval negative errno if the insertion fails.
871 int lod_sub_object_declare_punch(const struct lu_env *env,
872 struct dt_object *dt,
873 __u64 start, __u64 end,
876 struct thandle *sub_th;
881 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
883 RETURN(PTR_ERR(sub_th));
886 update_record_size(env, punch, th,
887 lu_object_fid(&dt->do_lu),
890 rc = dt_declare_punch(env, dt, start, end, sub_th);
896 * Punch to sub object
898 * Get transaction of next layer, records buffer write if it belongs to
899 * Cross-MDT operation, and punch object.
901 * \param[in] env execution environment
902 * \param[in] dt object to be written
903 * \param[in] start start offset of punch
904 * \param[in] end end offset of punch
905 * \param[in] th transaction handle
906 * \param[in] capa capability of the write
908 * \retval the buffer size in bytes if it succeeds.
909 * \retval negative errno if it fails.
911 int lod_sub_object_punch(const struct lu_env *env, struct dt_object *dt,
912 __u64 start, __u64 end, struct thandle *th)
914 struct thandle *sub_th;
919 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
921 RETURN(PTR_ERR(sub_th));
924 rc = update_record_pack(punch, th, lu_object_fid(&dt->do_lu),
930 rc = dt_punch(env, dt, start, end, sub_th);
935 int lod_sub_prep_llog(const struct lu_env *env, struct lod_device *lod,
936 struct dt_device *dt, int index)
938 struct lod_thread_info *lti = lod_env_info(env);
939 struct llog_ctxt *ctxt;
940 struct llog_handle *lgh;
941 struct llog_catid *cid = <i->lti_cid;
942 struct lu_fid *fid = <i->lti_fid;
943 struct obd_device *obd;
945 bool need_put = false;
948 lu_update_log_fid(fid, index);
950 rc = lodname2mdt_index(lod2obd(lod)->obd_name, (__u32 *)&index);
954 rc = llog_osd_get_cat_list(env, dt, index, 1, cid, fid);
956 CERROR("%s: can't get id from catalogs: rc = %d\n",
957 lod2obd(lod)->obd_name, rc);
961 obd = dt->dd_lu_dev.ld_obd;
962 ctxt = llog_get_context(obd, LLOG_UPDATELOG_ORIG_CTXT);
963 LASSERT(ctxt != NULL);
964 ctxt->loc_flags |= LLOG_CTXT_FLAG_NORMAL_FID;
965 ctxt->loc_chunk_size = LLOG_MIN_CHUNK_SIZE * 4;
966 if (likely(logid_id(&cid->lci_logid) != 0)) {
967 rc = llog_open(env, ctxt, &lgh, &cid->lci_logid, NULL,
970 /* re-create llog if it is missing */
972 logid_set_id(&cid->lci_logid, 0);
977 if (unlikely(logid_id(&cid->lci_logid) == 0)) {
978 rc = llog_open_create(env, ctxt, &lgh, NULL, NULL);
981 cid->lci_logid = lgh->lgh_id;
985 LASSERT(lgh != NULL);
987 rc = llog_cat_init_and_process(env, lgh);
992 rc = llog_osd_put_cat_list(env, dt, index, 1, cid, fid);
997 ctxt->loc_handle = lgh;
999 CDEBUG(D_INFO, "%s: Init llog for %d - catid "DOSTID":%x\n",
1000 obd->obd_name, index, POSTID(&cid->lci_logid.lgl_oi),
1001 cid->lci_logid.lgl_ogen);
1004 llog_cat_close(env, lgh);
1006 llog_ctxt_put(ctxt);