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);
77 LASSERT(tth->tt_magic == TOP_THANDLE_MAGIC);
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_update_records != NULL &&
86 record_update != NULL)
87 *record_update = true;
89 RETURN(tth->tt_master_sub_thandle);
92 rc = lod_fld_lookup(env, lod, lu_object_fid(&sub_obj->do_lu),
97 if (type == LU_SEQ_RANGE_OST)
98 RETURN(tth->tt_master_sub_thandle);
100 if (tth->tt_update_records != NULL && record_update != NULL)
101 *record_update = true;
103 sub_th = thandle_get_sub(env, th, sub_obj);
109 * Declare sub-object creation.
111 * Get transaction of next layer and declare the creation of the object.
113 * \param[in] env execution environment
114 * \param[in] dt the object being created
115 * \param[in] attr the attributes of the object being created
116 * \param[in] hint the hint of the creation
117 * \param[in] dof the object format of the creation
118 * \param[th] th the transaction handle
120 * \retval 0 if the declaration succeeds
121 * \retval negative errno if the declaration fails.
123 int lod_sub_object_declare_create(const struct lu_env *env,
124 struct dt_object *dt,
125 struct lu_attr *attr,
126 struct dt_allocation_hint *hint,
127 struct dt_object_format *dof,
130 struct thandle *sub_th;
132 sub_th = lod_sub_get_thandle(env, th, dt, NULL);
134 return PTR_ERR(sub_th);
136 return dt_declare_create(env, dt, attr, hint, dof, sub_th);
142 * Get transaction of next layer, record updates if it belongs to cross-MDT
143 * operation, and create the object.
145 * \param[in] env execution environment
146 * \param[in] dt the object being created
147 * \param[in] attr the attributes of the object being created
148 * \param[in] hint the hint of the creation
149 * \param[in] dof the object format of the creation
150 * \param[th] th the transaction handle
152 * \retval 0 if the creation succeeds
153 * \retval negative errno if the creation fails.
155 int lod_sub_object_create(const struct lu_env *env, struct dt_object *dt,
156 struct lu_attr *attr,
157 struct dt_allocation_hint *hint,
158 struct dt_object_format *dof,
161 struct thandle *sub_th;
166 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
168 RETURN(PTR_ERR(sub_th));
171 rc = update_record_pack(create, th,
172 lu_object_fid(&dt->do_lu),
178 rc = dt_create(env, dt, attr, hint, dof, sub_th);
184 * Declare adding reference for the sub-object
186 * Get transaction of next layer and declare the reference adding.
188 * \param[in] env execution environment
189 * \param[in] dt dt object to add reference
190 * \param[in] th transaction handle
192 * \retval 0 if the declaration succeeds.
193 * \retval negative errno if the declaration fails.
195 int lod_sub_object_declare_ref_add(const struct lu_env *env,
196 struct dt_object *dt,
199 struct thandle *sub_th;
203 sub_th = lod_sub_get_thandle(env, th, dt, NULL);
205 RETURN(PTR_ERR(sub_th));
207 rc = dt_declare_ref_add(env, dt, sub_th);
213 * Add reference for the sub-object
215 * Get transaction of next layer, record updates if it belongs to cross-MDT
216 * operation and add reference of the object.
218 * \param[in] env execution environment
219 * \param[in] dt dt object to add reference
220 * \param[in] th transaction handle
222 * \retval 0 if it succeeds.
223 * \retval negative errno if it fails.
225 int lod_sub_object_ref_add(const struct lu_env *env, struct dt_object *dt,
228 struct thandle *sub_th;
233 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
235 RETURN(PTR_ERR(sub_th));
238 rc = update_record_pack(ref_add, th,
239 lu_object_fid(&dt->do_lu));
244 rc = dt_ref_add(env, dt, sub_th);
250 * Declare deleting reference for the sub-object
252 * Get transaction of next layer and declare the reference deleting.
254 * \param[in] env execution environment
255 * \param[in] dt dt object to delete reference
256 * \param[in] th transaction handle
258 * \retval 0 if the declaration succeeds.
259 * \retval negative errno if the declaration fails.
261 int lod_sub_object_declare_ref_del(const struct lu_env *env,
262 struct dt_object *dt,
265 struct thandle *sub_th;
269 sub_th = lod_sub_get_thandle(env, th, dt, NULL);
271 RETURN(PTR_ERR(sub_th));
273 rc = dt_declare_ref_del(env, dt, sub_th);
279 * Delete reference for the sub-object
281 * Get transaction of next layer, record updates if it belongs to cross-MDT
282 * operation and delete reference of the object.
284 * \param[in] env execution environment
285 * \param[in] dt dt object to delete reference
286 * \param[in] th transaction handle
288 * \retval 0 if it succeeds.
289 * \retval negative errno if it fails.
291 int lod_sub_object_ref_del(const struct lu_env *env, struct dt_object *dt,
294 struct thandle *sub_th;
299 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
301 RETURN(PTR_ERR(sub_th));
304 rc = update_record_pack(ref_del, th,
305 lu_object_fid(&dt->do_lu));
310 rc = dt_ref_del(env, dt, sub_th);
316 * Declare destroying sub-object
318 * Get transaction of next layer and declare the sub-object destroy.
320 * \param[in] env execution environment
321 * \param[in] dt dt object to be destroyed
322 * \param[in] th transaction handle
324 * \retval 0 if the declaration succeeds.
325 * \retval negative errno if the declaration fails.
327 int lod_sub_object_declare_destroy(const struct lu_env *env,
328 struct dt_object *dt,
331 struct thandle *sub_th;
335 sub_th = lod_sub_get_thandle(env, th, dt, NULL);
337 RETURN(PTR_ERR(sub_th));
339 rc = dt_declare_destroy(env, dt, sub_th);
347 * Get transaction of next layer, record updates if it belongs to cross-MDT
348 * operation and destroy the object.
350 * \param[in] env execution environment
351 * \param[in] dt dt object to be destroyed
352 * \param[in] th transaction handle
354 * \retval 0 if the destroy succeeds.
355 * \retval negative errno if the destroy fails.
357 int lod_sub_object_destroy(const struct lu_env *env, struct dt_object *dt,
360 struct thandle *sub_th;
365 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
367 RETURN(PTR_ERR(sub_th));
370 rc = update_record_pack(object_destroy, th,
371 lu_object_fid(&dt->do_lu));
376 rc = dt_destroy(env, dt, sub_th);
382 * Declare sub-object index insert
384 * Get transaction of next layer and declare index insert.
386 * \param[in] env execution environment
387 * \param[in] dt object for which to insert index
388 * \param[in] rec record of the index which will be inserted
389 * \param[in] key key of the index which will be inserted
390 * \param[in] th the transaction handle
392 * \retval 0 if the declaration succeeds.
393 * \retval negative errno if the declaration fails.
395 int lod_sub_object_declare_insert(const struct lu_env *env,
396 struct dt_object *dt,
397 const struct dt_rec *rec,
398 const struct dt_key *key,
401 struct thandle *sub_th;
403 sub_th = lod_sub_get_thandle(env, th, dt, NULL);
405 return PTR_ERR(sub_th);
407 return dt_declare_insert(env, dt, rec, key, sub_th);
411 * Insert index of sub object
413 * Get transaction of next layer, record updates if it belongs to cross-MDT
414 * operation, and insert the index.
416 * \param[in] env execution environment
417 * \param[in] dt object for which to insert index
418 * \param[in] rec record of the index to be inserted
419 * \param[in] key key of the index to be inserted
420 * \param[in] th the transaction handle
421 * \param[in] ign whether ignore quota
423 * \retval 0 if the insertion succeeds.
424 * \retval negative errno if the insertion fails.
426 int lod_sub_object_index_insert(const struct lu_env *env, struct dt_object *dt,
427 const struct dt_rec *rec,
428 const struct dt_key *key, struct thandle *th,
431 struct thandle *sub_th;
435 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
437 return PTR_ERR(sub_th);
440 rc = update_record_pack(index_insert, th,
441 lu_object_fid(&dt->do_lu), rec, key);
446 return dt_insert(env, dt, rec, key, sub_th, ign);
450 * Declare sub-object index delete
452 * Get transaction of next layer and declare index deletion.
454 * \param[in] env execution environment
455 * \param[in] dt object for which to delete index
456 * \param[in] key key of the index which will be deleted
457 * \param[in] th the transaction handle
459 * \retval 0 if the declaration succeeds.
460 * \retval negative errno if the declaration fails.
462 int lod_sub_object_declare_delete(const struct lu_env *env,
463 struct dt_object *dt,
464 const struct dt_key *key,
467 struct thandle *sub_th;
469 sub_th = lod_sub_get_thandle(env, th, dt, NULL);
471 return PTR_ERR(sub_th);
473 return dt_declare_delete(env, dt, key, sub_th);
477 * Delete index of sub object
479 * Get transaction of next layer, record updates if it belongs to cross-MDT
480 * operation, and delete the index.
482 * \param[in] env execution environment
483 * \param[in] dt object for which to delete index
484 * \param[in] key key of the index to be deleted
485 * \param[in] th the transaction handle
487 * \retval 0 if the deletion succeeds.
488 * \retval negative errno if the deletion fails.
490 int lod_sub_object_delete(const struct lu_env *env, struct dt_object *dt,
491 const struct dt_key *name, struct thandle *th)
493 struct thandle *sub_th;
498 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
500 RETURN(PTR_ERR(sub_th));
503 rc = update_record_pack(index_delete, th,
504 lu_object_fid(&dt->do_lu), name);
509 rc = dt_delete(env, dt, name, sub_th);
516 * Get transaction of next layer, and declare xattr set.
518 * \param[in] env execution environment
519 * \param[in] dt object on which to set xattr
520 * \param[in] buf xattr to be set
521 * \param[in] name name of the xattr
522 * \param[in] fl flag for setting xattr
524 * \retval 0 if the declaration succeeds.
525 * \retval negative errno if the declaration fails.
527 int lod_sub_object_declare_xattr_set(const struct lu_env *env,
528 struct dt_object *dt,
529 const struct lu_buf *buf,
530 const char *name, int fl,
533 struct thandle *sub_th;
537 sub_th = lod_sub_get_thandle(env, th, dt, NULL);
539 RETURN(PTR_ERR(sub_th));
541 rc = dt_declare_xattr_set(env, dt, buf, name, fl, sub_th);
549 * Get transaction of next layer, record updates if it belongs to cross-MDT
550 * operation, and set xattr to the object.
552 * \param[in] env execution environment
553 * \param[in] dt object on which to set xattr
554 * \param[in] buf xattr to be set
555 * \param[in] name name of the xattr
556 * \param[in] fl flag for setting xattr
557 * \param[in] th transaction handle
559 * \retval 0 if the xattr setting succeeds.
560 * \retval negative errno if xattr setting fails.
562 int lod_sub_object_xattr_set(const struct lu_env *env, struct dt_object *dt,
563 const struct lu_buf *buf, const char *name, int fl,
566 struct thandle *sub_th;
571 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
573 RETURN(PTR_ERR(sub_th));
576 rc = update_record_pack(xattr_set, th,
577 lu_object_fid(&dt->do_lu),
583 rc = dt_xattr_set(env, dt, buf, name, fl, sub_th);
591 * Get transaction of next layer, and declare attr set.
593 * \param[in] env execution environment
594 * \param[in] dt object on which to set attr
595 * \param[in] attr attributes to be set
596 * \param[in] th transaction handle
598 * \retval 0 if the declaration succeeds.
599 * \retval negative errno if the declaration fails.
601 int lod_sub_object_declare_attr_set(const struct lu_env *env,
602 struct dt_object *dt,
603 const struct lu_attr *attr,
606 struct thandle *sub_th;
610 sub_th = lod_sub_get_thandle(env, th, dt, NULL);
612 RETURN(PTR_ERR(sub_th));
614 rc = dt_declare_attr_set(env, dt, attr, sub_th);
622 * Get transaction of next layer, record updates if it belongs to cross-MDT
623 * operation, and set attributes to the object.
625 * \param[in] env execution environment
626 * \param[in] dt object on which to set attr
627 * \param[in] attr attrbutes to be set
628 * \param[in] th transaction handle
630 * \retval 0 if attributes setting succeeds.
631 * \retval negative errno if the attributes setting fails.
633 int lod_sub_object_attr_set(const struct lu_env *env,
634 struct dt_object *dt,
635 const struct lu_attr *attr,
639 struct thandle *sub_th;
643 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
645 RETURN(PTR_ERR(sub_th));
648 rc = update_record_pack(attr_set, th, lu_object_fid(&dt->do_lu),
654 rc = dt_attr_set(env, dt, attr, sub_th);
662 * Get transaction of next layer, and declare xattr deletion.
664 * \param[in] env execution environment
665 * \param[in] dt object on which to delete xattr
666 * \param[in] name name of the xattr to be deleted
667 * \param[in] th transaction handle
669 * \retval 0 if the declaration succeeds.
670 * \retval negative errno if the declaration fails.
672 int lod_sub_object_declare_xattr_del(const struct lu_env *env,
673 struct dt_object *dt,
677 struct thandle *sub_th;
681 sub_th = lod_sub_get_thandle(env, th, dt, NULL);
683 RETURN(PTR_ERR(sub_th));
685 rc = dt_declare_xattr_del(env, dt, name, sub_th);
691 * xattribute deletion
693 * Get transaction of next layer, record update if it belongs to cross-MDT
694 * operation and delete xattr.
696 * \param[in] env execution environment
697 * \param[in] dt object on which to delete xattr
698 * \param[in] name name of the xattr to be deleted
699 * \param[in] th transaction handle
701 * \retval 0 if the deletion succeeds.
702 * \retval negative errno if the deletion fails.
704 int lod_sub_object_xattr_del(const struct lu_env *env,
705 struct dt_object *dt,
709 struct thandle *sub_th;
714 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
716 RETURN(PTR_ERR(sub_th));
719 rc = update_record_pack(xattr_del, th,
720 lu_object_fid(&dt->do_lu), name);
725 rc = dt_xattr_del(env, dt, name, sub_th);
731 * Declare buffer write
733 * Get transaction of next layer and declare buffer write.
735 * \param[in] env execution environment
736 * \param[in] dt object to be written
737 * \param[in] buf buffer to write which includes an embedded size field
738 * \param[in] pos offet in the object to start writing at
739 * \param[in] th transaction handle
741 * \retval 0 if the insertion succeeds.
742 * \retval negative errno if the insertion fails.
744 int lod_sub_object_declare_write(const struct lu_env *env,
745 struct dt_object *dt,
746 const struct lu_buf *buf, loff_t pos,
749 struct thandle *sub_th;
753 sub_th = lod_sub_get_thandle(env, th, dt, NULL);
755 RETURN(PTR_ERR(sub_th));
757 rc = dt_declare_write(env, dt, buf, pos, sub_th);
763 * Write buffer to sub object
765 * Get transaction of next layer, records buffer write if it belongs to
766 * Cross-MDT operation, and write buffer.
768 * \param[in] env execution environment
769 * \param[in] dt object to be written
770 * \param[in] buf buffer to write which includes an embedded size field
771 * \param[in] pos offet in the object to start writing at
772 * \param[in] th transaction handle
773 * \param[in] rq enforcement for this write
775 * \retval the buffer size in bytes if it succeeds.
776 * \retval negative errno if it fails.
778 ssize_t lod_sub_object_write(const struct lu_env *env, struct dt_object *dt,
779 const struct lu_buf *buf, loff_t *pos,
780 struct thandle *th, int rq)
782 struct thandle *sub_th;
787 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
789 RETURN(PTR_ERR(sub_th));
792 rc = update_record_pack(write, th, lu_object_fid(&dt->do_lu),
798 rc = dt_write(env, dt, buf, pos, sub_th, rq);
805 * Get transaction of next layer and declare punch.
807 * \param[in] env execution environment
808 * \param[in] dt object to be written
809 * \param[in] start start offset of punch
810 * \param[in] end end offet of punch
811 * \param[in] th transaction handle
813 * \retval 0 if the insertion succeeds.
814 * \retval negative errno if the insertion fails.
816 int lod_sub_object_declare_punch(const struct lu_env *env,
817 struct dt_object *dt,
818 __u64 start, __u64 end,
821 struct thandle *sub_th;
825 sub_th = lod_sub_get_thandle(env, th, dt, NULL);
827 RETURN(PTR_ERR(sub_th));
829 rc = dt_declare_punch(env, dt, start, end, sub_th);
835 * Punch to sub object
837 * Get transaction of next layer, records buffer write if it belongs to
838 * Cross-MDT operation, and punch object.
840 * \param[in] env execution environment
841 * \param[in] dt object to be written
842 * \param[in] start start offset of punch
843 * \param[in] end end offset of punch
844 * \param[in] th transaction handle
845 * \param[in] capa capability of the write
847 * \retval the buffer size in bytes if it succeeds.
848 * \retval negative errno if it fails.
850 int lod_sub_object_punch(const struct lu_env *env, struct dt_object *dt,
851 __u64 start, __u64 end, struct thandle *th)
853 struct thandle *sub_th;
858 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
860 RETURN(PTR_ERR(sub_th));
863 rc = update_record_pack(punch, th, lu_object_fid(&dt->do_lu),
869 rc = dt_punch(env, dt, start, end, sub_th);
874 int lod_sub_prep_llog(const struct lu_env *env, struct lod_device *lod,
875 struct dt_device *dt, int index)
877 struct lod_thread_info *lti = lod_env_info(env);
878 struct llog_ctxt *ctxt;
879 struct llog_handle *lgh;
880 struct llog_catid *cid = <i->lti_cid;
881 struct lu_fid *fid = <i->lti_fid;
882 struct obd_device *obd;
886 lu_update_log_fid(fid, index);
887 fid_to_logid(fid, &cid->lci_logid);
889 obd = dt->dd_lu_dev.ld_obd;
890 ctxt = llog_get_context(obd, LLOG_UPDATELOG_ORIG_CTXT);
891 LASSERT(ctxt != NULL);
892 ctxt->loc_flags |= LLOG_CTXT_FLAG_NORMAL_FID;
894 rc = llog_open(env, ctxt, &lgh, &cid->lci_logid, NULL,
901 LASSERT(lgh != NULL);
902 ctxt->loc_handle = lgh;
904 rc = llog_cat_init_and_process(env, lgh);
906 llog_cat_close(env, ctxt->loc_handle);
907 ctxt->loc_handle = NULL;