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) 2015, 2017, 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 <uapi/linux/lustre/lustre_ver.h>
46 #include <obd_support.h>
47 #include <lprocfs_status.h>
49 #include <lustre_fid.h>
50 #include <uapi/linux/lustre/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);
77 tth->tt_master_sub_thandle->th_ignore_quota = th->th_ignore_quota;
79 /* local object must be mdt object, Note: during ost object
80 * creation, FID is not assigned until osp_create(),
81 * so if the FID of sub_obj is zero, it means OST object. */
82 if (!dt_object_remote(sub_obj) ||
83 fid_is_zero(lu_object_fid(&sub_obj->do_lu))) {
84 /* local MDT object */
85 if (fid_is_sane(lu_object_fid(&sub_obj->do_lu)) &&
86 tth->tt_multiple_thandle != NULL &&
87 record_update != NULL &&
89 *record_update = true;
91 RETURN(tth->tt_master_sub_thandle);
94 rc = lod_fld_lookup(env, lod, lu_object_fid(&sub_obj->do_lu),
99 /* th_complex means we need track all of updates for this
100 * transaction, include changes on OST */
101 if (type == LU_SEQ_RANGE_OST && !th->th_complex)
102 RETURN(tth->tt_master_sub_thandle);
104 sub_th = thandle_get_sub(env, th, sub_obj);
107 sub_th->th_ignore_quota = th->th_ignore_quota;
109 if (tth->tt_multiple_thandle != NULL && record_update != NULL &&
111 *record_update = true;
117 * Declare sub-object creation.
119 * Get transaction of next layer and declare the creation of the object.
121 * \param[in] env execution environment
122 * \param[in] dt the object being created
123 * \param[in] attr the attributes of the object being created
124 * \param[in] hint the hint of the creation
125 * \param[in] dof the object format of the creation
126 * \param[th] th the transaction handle
128 * \retval 0 if the declaration succeeds
129 * \retval negative errno if the declaration fails.
131 int lod_sub_declare_create(const struct lu_env *env, struct dt_object *dt,
132 struct lu_attr *attr,
133 struct dt_allocation_hint *hint,
134 struct dt_object_format *dof, struct thandle *th)
136 struct thandle *sub_th;
139 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
141 return PTR_ERR(sub_th);
144 update_record_size(env, create, th, lu_object_fid(&dt->do_lu),
147 return dt_declare_create(env, dt, attr, hint, dof, sub_th);
153 * Get transaction of next layer, record updates if it belongs to cross-MDT
154 * operation, and create the object.
156 * \param[in] env execution environment
157 * \param[in] dt the object being created
158 * \param[in] attr the attributes of the object being created
159 * \param[in] hint the hint of the creation
160 * \param[in] dof the object format of the creation
161 * \param[th] th the transaction handle
163 * \retval 0 if the creation succeeds
164 * \retval negative errno if the creation fails.
166 int lod_sub_create(const struct lu_env *env, struct dt_object *dt,
167 struct lu_attr *attr, struct dt_allocation_hint *hint,
168 struct dt_object_format *dof, struct thandle *th)
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_declare_ref_add(const struct lu_env *env, struct dt_object *dt,
207 struct thandle *sub_th;
212 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
214 RETURN(PTR_ERR(sub_th));
217 update_record_size(env, ref_add, th, lu_object_fid(&dt->do_lu));
219 rc = dt_declare_ref_add(env, dt, sub_th);
225 * Add reference for the sub-object
227 * Get transaction of next layer, record updates if it belongs to cross-MDT
228 * operation and add reference of the object.
230 * \param[in] env execution environment
231 * \param[in] dt dt object to add reference
232 * \param[in] th transaction handle
234 * \retval 0 if it succeeds.
235 * \retval negative errno if it fails.
237 int lod_sub_ref_add(const struct lu_env *env, struct dt_object *dt,
240 struct thandle *sub_th;
245 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
247 RETURN(PTR_ERR(sub_th));
250 rc = update_record_pack(ref_add, th,
251 lu_object_fid(&dt->do_lu));
256 rc = dt_ref_add(env, dt, sub_th);
262 * Declare deleting reference for the sub-object
264 * Get transaction of next layer and declare the reference deleting.
266 * \param[in] env execution environment
267 * \param[in] dt dt object to delete reference
268 * \param[in] th transaction handle
270 * \retval 0 if the declaration succeeds.
271 * \retval negative errno if the declaration fails.
273 int lod_sub_declare_ref_del(const struct lu_env *env, struct dt_object *dt,
276 struct thandle *sub_th;
281 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
283 RETURN(PTR_ERR(sub_th));
286 update_record_size(env, ref_del, th, lu_object_fid(&dt->do_lu));
288 rc = dt_declare_ref_del(env, dt, sub_th);
294 * Delete reference for the sub-object
296 * Get transaction of next layer, record updates if it belongs to cross-MDT
297 * operation and delete reference of the object.
299 * \param[in] env execution environment
300 * \param[in] dt dt object to delete reference
301 * \param[in] th transaction handle
303 * \retval 0 if it succeeds.
304 * \retval negative errno if it fails.
306 int lod_sub_ref_del(const struct lu_env *env, struct dt_object *dt,
309 struct thandle *sub_th;
314 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
316 RETURN(PTR_ERR(sub_th));
319 rc = update_record_pack(ref_del, th,
320 lu_object_fid(&dt->do_lu));
325 rc = dt_ref_del(env, dt, sub_th);
331 * Declare destroying sub-object
333 * Get transaction of next layer and declare the sub-object destroy.
335 * \param[in] env execution environment
336 * \param[in] dt dt object to be destroyed
337 * \param[in] th transaction handle
339 * \retval 0 if the declaration succeeds.
340 * \retval negative errno if the declaration fails.
342 int lod_sub_declare_destroy(const struct lu_env *env, struct dt_object *dt,
345 struct thandle *sub_th;
350 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
352 RETURN(PTR_ERR(sub_th));
355 update_record_size(env, destroy, th, lu_object_fid(&dt->do_lu));
357 rc = dt_declare_destroy(env, dt, sub_th);
365 * Get transaction of next layer, record updates if it belongs to cross-MDT
366 * operation and destroy the object.
368 * \param[in] env execution environment
369 * \param[in] dt dt object to be destroyed
370 * \param[in] th transaction handle
372 * \retval 0 if the destroy succeeds.
373 * \retval negative errno if the destroy fails.
375 int lod_sub_destroy(const struct lu_env *env, struct dt_object *dt,
378 struct thandle *sub_th;
383 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
385 RETURN(PTR_ERR(sub_th));
388 rc = update_record_pack(destroy, th, lu_object_fid(&dt->do_lu));
393 rc = dt_destroy(env, dt, sub_th);
399 * Declare sub-object index insert
401 * Get transaction of next layer and declare index insert.
403 * \param[in] env execution environment
404 * \param[in] dt object for which to insert index
405 * \param[in] rec record of the index which will be inserted
406 * \param[in] key key of the index which will be inserted
407 * \param[in] th the transaction handle
409 * \retval 0 if the declaration succeeds.
410 * \retval negative errno if the declaration fails.
412 int lod_sub_declare_insert(const struct lu_env *env, struct dt_object *dt,
413 const struct dt_rec *rec,
414 const struct dt_key *key, struct thandle *th)
416 struct thandle *sub_th;
419 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
421 return PTR_ERR(sub_th);
424 update_record_size(env, index_insert, th,
425 lu_object_fid(&dt->do_lu), rec, key);
427 return dt_declare_insert(env, dt, rec, key, sub_th);
431 * Insert index of sub object
433 * Get transaction of next layer, record updates if it belongs to cross-MDT
434 * operation, and insert the index.
436 * \param[in] env execution environment
437 * \param[in] dt object for which to insert index
438 * \param[in] rec record of the index to be inserted
439 * \param[in] key key of the index to be inserted
440 * \param[in] th the transaction handle
442 * \retval 0 if the insertion succeeds.
443 * \retval negative errno if the insertion fails.
445 int lod_sub_insert(const struct lu_env *env, struct dt_object *dt,
446 const struct dt_rec *rec, const struct dt_key *key,
449 struct thandle *sub_th;
453 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
455 return PTR_ERR(sub_th);
458 rc = update_record_pack(index_insert, th,
459 lu_object_fid(&dt->do_lu), rec, key);
464 return dt_insert(env, dt, rec, key, sub_th);
468 * Declare sub-object index delete
470 * Get transaction of next layer and declare index deletion.
472 * \param[in] env execution environment
473 * \param[in] dt object for which to delete index
474 * \param[in] key key of the index which will be deleted
475 * \param[in] th the transaction handle
477 * \retval 0 if the declaration succeeds.
478 * \retval negative errno if the declaration fails.
480 int lod_sub_declare_delete(const struct lu_env *env, struct dt_object *dt,
481 const struct dt_key *key, struct thandle *th)
483 struct thandle *sub_th;
486 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
488 return PTR_ERR(sub_th);
491 update_record_size(env, index_delete, th,
492 lu_object_fid(&dt->do_lu), key);
494 return dt_declare_delete(env, dt, key, sub_th);
498 * Delete index of sub object
500 * Get transaction of next layer, record updates if it belongs to cross-MDT
501 * operation, and delete the index.
503 * \param[in] env execution environment
504 * \param[in] dt object for which to delete index
505 * \param[in] key key of the index to be deleted
506 * \param[in] th the transaction handle
508 * \retval 0 if the deletion succeeds.
509 * \retval negative errno if the deletion fails.
511 int lod_sub_delete(const struct lu_env *env, struct dt_object *dt,
512 const struct dt_key *name, struct thandle *th)
514 struct thandle *sub_th;
519 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
521 RETURN(PTR_ERR(sub_th));
524 rc = update_record_pack(index_delete, th,
525 lu_object_fid(&dt->do_lu), name);
530 rc = dt_delete(env, dt, name, sub_th);
537 * Get transaction of next layer, and declare xattr set.
539 * \param[in] env execution environment
540 * \param[in] dt object on which to set xattr
541 * \param[in] buf xattr to be set
542 * \param[in] name name of the xattr
543 * \param[in] fl flag for setting xattr
545 * \retval 0 if the declaration succeeds.
546 * \retval negative errno if the declaration fails.
548 int lod_sub_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
549 const struct lu_buf *buf, const char *name,
550 int fl, struct thandle *th)
552 struct thandle *sub_th;
557 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
559 RETURN(PTR_ERR(sub_th));
562 update_record_size(env, xattr_set, th,
563 lu_object_fid(&dt->do_lu),
566 rc = dt_declare_xattr_set(env, dt, buf, name, fl, sub_th);
574 * Get transaction of next layer, record updates if it belongs to cross-MDT
575 * operation, and set xattr to the object.
577 * \param[in] env execution environment
578 * \param[in] dt object on which to set xattr
579 * \param[in] buf xattr to be set
580 * \param[in] name name of the xattr
581 * \param[in] fl flag for setting xattr
582 * \param[in] th transaction handle
584 * \retval 0 if the xattr setting succeeds.
585 * \retval negative errno if xattr setting fails.
587 int lod_sub_xattr_set(const struct lu_env *env, struct dt_object *dt,
588 const struct lu_buf *buf, const char *name, int fl,
591 struct thandle *sub_th;
596 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
598 RETURN(PTR_ERR(sub_th));
601 rc = update_record_pack(xattr_set, th,
602 lu_object_fid(&dt->do_lu),
608 rc = dt_xattr_set(env, dt, buf, name, fl, sub_th);
616 * Get transaction of next layer, and declare attr set.
618 * \param[in] env execution environment
619 * \param[in] dt object on which to set attr
620 * \param[in] attr attributes to be set
621 * \param[in] th transaction handle
623 * \retval 0 if the declaration succeeds.
624 * \retval negative errno if the declaration fails.
626 int lod_sub_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
627 const struct lu_attr *attr, struct thandle *th)
629 struct thandle *sub_th;
634 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
636 RETURN(PTR_ERR(sub_th));
639 update_record_size(env, attr_set, th,
640 lu_object_fid(&dt->do_lu), attr);
642 rc = dt_declare_attr_set(env, dt, attr, sub_th);
650 * Get transaction of next layer, record updates if it belongs to cross-MDT
651 * operation, and set attributes to the object.
653 * \param[in] env execution environment
654 * \param[in] dt object on which to set attr
655 * \param[in] attr attrbutes to be set
656 * \param[in] th transaction handle
658 * \retval 0 if attributes setting succeeds.
659 * \retval negative errno if the attributes setting fails.
661 int lod_sub_attr_set(const struct lu_env *env, struct dt_object *dt,
662 const struct lu_attr *attr, struct thandle *th)
665 struct thandle *sub_th;
669 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
671 RETURN(PTR_ERR(sub_th));
674 rc = update_record_pack(attr_set, th, lu_object_fid(&dt->do_lu),
680 rc = dt_attr_set(env, dt, attr, sub_th);
688 * Get transaction of next layer, and declare xattr deletion.
690 * \param[in] env execution environment
691 * \param[in] dt object on which to delete xattr
692 * \param[in] name name of the xattr to be deleted
693 * \param[in] th transaction handle
695 * \retval 0 if the declaration succeeds.
696 * \retval negative errno if the declaration fails.
698 int lod_sub_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
699 const char *name, struct thandle *th)
701 struct thandle *sub_th;
706 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
708 RETURN(PTR_ERR(sub_th));
711 update_record_size(env, xattr_del, th,
712 lu_object_fid(&dt->do_lu),
715 rc = dt_declare_xattr_del(env, dt, name, sub_th);
721 * xattribute deletion
723 * Get transaction of next layer, record update if it belongs to cross-MDT
724 * operation and delete xattr.
726 * \param[in] env execution environment
727 * \param[in] dt object on which to delete xattr
728 * \param[in] name name of the xattr to be deleted
729 * \param[in] th transaction handle
731 * \retval 0 if the deletion succeeds.
732 * \retval negative errno if the deletion fails.
734 int lod_sub_xattr_del(const struct lu_env *env, struct dt_object *dt,
735 const char *name, struct thandle *th)
737 struct thandle *sub_th;
742 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
744 RETURN(PTR_ERR(sub_th));
747 rc = update_record_pack(xattr_del, th,
748 lu_object_fid(&dt->do_lu), name);
753 rc = dt_xattr_del(env, dt, name, sub_th);
759 * Declare buffer write
761 * Get transaction of next layer and declare buffer write.
763 * \param[in] env execution environment
764 * \param[in] dt object to be written
765 * \param[in] buf buffer to write which includes an embedded size field
766 * \param[in] pos offet in the object to start writing at
767 * \param[in] th transaction handle
769 * \retval 0 if the insertion succeeds.
770 * \retval negative errno if the insertion fails.
772 int lod_sub_declare_write(const struct lu_env *env, struct dt_object *dt,
773 const struct lu_buf *buf, loff_t pos,
776 struct thandle *sub_th;
781 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
783 RETURN(PTR_ERR(sub_th));
786 update_record_size(env, write, th,
787 lu_object_fid(&dt->do_lu),
790 rc = dt_declare_write(env, dt, buf, pos, sub_th);
796 * Write buffer to sub object
798 * Get transaction of next layer, records buffer write if it belongs to
799 * Cross-MDT operation, and write buffer.
801 * \param[in] env execution environment
802 * \param[in] dt object to be written
803 * \param[in] buf buffer to write which includes an embedded size field
804 * \param[in] pos offet in the object to start writing at
805 * \param[in] th transaction handle
807 * \retval the buffer size in bytes if it succeeds.
808 * \retval negative errno if it fails.
810 ssize_t lod_sub_write(const struct lu_env *env, struct dt_object *dt,
811 const struct lu_buf *buf, loff_t *pos,
814 struct thandle *sub_th;
819 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
821 RETURN(PTR_ERR(sub_th));
824 rc = update_record_pack(write, th, lu_object_fid(&dt->do_lu),
830 rc = dt_write(env, dt, buf, pos, sub_th);
837 * Get transaction of next layer and declare punch.
839 * \param[in] env execution environment
840 * \param[in] dt object to be written
841 * \param[in] start start offset of punch
842 * \param[in] end end offet of punch
843 * \param[in] th transaction handle
845 * \retval 0 if the insertion succeeds.
846 * \retval negative errno if the insertion fails.
848 int lod_sub_declare_punch(const struct lu_env *env, struct dt_object *dt,
849 __u64 start, __u64 end, struct thandle *th)
851 struct thandle *sub_th;
856 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
858 RETURN(PTR_ERR(sub_th));
861 update_record_size(env, punch, th,
862 lu_object_fid(&dt->do_lu),
865 rc = dt_declare_punch(env, dt, start, end, sub_th);
871 * Punch to sub object
873 * Get transaction of next layer, records buffer write if it belongs to
874 * Cross-MDT operation, and punch object.
876 * \param[in] env execution environment
877 * \param[in] dt object to be written
878 * \param[in] start start offset of punch
879 * \param[in] end end offset of punch
880 * \param[in] th transaction handle
881 * \param[in] capa capability of the write
883 * \retval the buffer size in bytes if it succeeds.
884 * \retval negative errno if it fails.
886 int lod_sub_punch(const struct lu_env *env, struct dt_object *dt,
887 __u64 start, __u64 end, struct thandle *th)
889 struct thandle *sub_th;
894 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
896 RETURN(PTR_ERR(sub_th));
899 rc = update_record_pack(punch, th, lu_object_fid(&dt->do_lu),
905 rc = dt_punch(env, dt, start, end, sub_th);
910 int lod_sub_prep_llog(const struct lu_env *env, struct lod_device *lod,
911 struct dt_device *dt, int index)
913 struct lod_thread_info *lti = lod_env_info(env);
914 struct llog_ctxt *ctxt;
915 struct llog_handle *lgh;
916 struct llog_catid *cid = <i->lti_cid;
917 struct lu_fid *fid = <i->lti_fid;
918 struct obd_device *obd;
920 bool need_put = false;
923 lu_update_log_fid(fid, index);
925 rc = lodname2mdt_index(lod2obd(lod)->obd_name, (__u32 *)&index);
929 rc = llog_osd_get_cat_list(env, dt, index, 1, cid, fid);
931 CERROR("%s: can't get id from catalogs: rc = %d\n",
932 lod2obd(lod)->obd_name, rc);
936 obd = dt->dd_lu_dev.ld_obd;
937 ctxt = llog_get_context(obd, LLOG_UPDATELOG_ORIG_CTXT);
938 LASSERT(ctxt != NULL);
939 ctxt->loc_flags |= LLOG_CTXT_FLAG_NORMAL_FID;
940 ctxt->loc_chunk_size = LLOG_MIN_CHUNK_SIZE * 4;
941 if (likely(logid_id(&cid->lci_logid) != 0)) {
942 rc = llog_open(env, ctxt, &lgh, &cid->lci_logid, NULL,
945 /* re-create llog if it is missing */
947 logid_set_id(&cid->lci_logid, 0);
952 if (unlikely(logid_id(&cid->lci_logid) == 0)) {
953 rc = llog_open_create(env, ctxt, &lgh, NULL, NULL);
956 cid->lci_logid = lgh->lgh_id;
960 LASSERT(lgh != NULL);
962 rc = llog_init_handle(env, lgh, LLOG_F_IS_CAT, NULL);
967 rc = llog_osd_put_cat_list(env, dt, index, 1, cid, fid);
972 ctxt->loc_handle = lgh;
974 CDEBUG(D_INFO, "%s: init llog for index %d - catid "DFID":%x\n",
975 obd->obd_name, index, PFID(&cid->lci_logid.lgl_oi.oi_fid),
976 cid->lci_logid.lgl_ogen);
979 llog_cat_close(env, lgh);